Module: ForemanAnsible::HostManagedExtensions

Defined in:
app/models/concerns/foreman_ansible/host_managed_extensions.rb

Overview

Relations to make Host::Managed ‘have’ ansible roles

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/concerns/foreman_ansible/host_managed_extensions.rb', line 7

def self.prepended(base)
  base.instance_eval do
    include ::ForemanAnsible::Concerns::JobInvocationHelper

    has_many :host_ansible_roles, -> { order('host_ansible_roles.position ASC') }, :foreign_key => :host_id
    accepts_nested_attributes_for :host_ansible_roles, :allow_destroy => true
    has_many :ansible_roles,
             -> { order('host_ansible_roles.position ASC') },
             :through => :host_ansible_roles,
             :dependent => :destroy
    scoped_search :relation => :ansible_roles, :on => :name,
                  :complete_value => true, :rename => :ansible_role,
                  :only_explicit => true

    before_provision :play_ansible_roles
    audit_associations :ansible_roles
  end

  base.singleton_class.prepend ClassMethods
end

Instance Method Details

#all_ansible_rolesObject



52
53
54
# File 'app/models/concerns/foreman_ansible/host_managed_extensions.rb', line 52

def all_ansible_roles
  (inherited_ansible_roles + ansible_roles).uniq
end

#inherited_ansible_rolesObject



28
29
30
31
# File 'app/models/concerns/foreman_ansible/host_managed_extensions.rb', line 28

def inherited_ansible_roles
  return [] unless hostgroup
  hostgroup.inherited_and_own_ansible_roles
end

#play_ansible_rolesObject

This one should be fixed, disabled for the moment as we’re in a rush to get the release out



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/concerns/foreman_ansible/host_managed_extensions.rb', line 35

def play_ansible_roles
  return true unless ansible_roles.present? ||
                     inherited_ansible_roles.present?
  composer = job_composer(:ansible_run_host, self)
  composer.triggering.mode = :future
  composer.triggering.start_at = (
    Time.zone.now +
    Setting::Ansible[:ansible_post_provision_timeout].to_i.seconds
  )
  composer.trigger!
  logger.info("Task for Ansible roles on #{self} before_provision: "\
              "#{job_invocation_path(composer.job_invocation)}")
rescue Foreman::Exception => e
  logger.info("Error running Ansible roles on #{self} before_provision: "\
              "#{e.message}")
end