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, ext_method: :search_by_role

    before_provision :play_ansible_roles
    audit_associations :ansible_roles
  end

  base.singleton_class.prepend ClassMethods
end

Instance Method Details

#all_ansible_rolesObject



60
61
62
# File 'app/models/concerns/foreman_ansible/host_managed_extensions.rb', line 60

def all_ansible_roles
  (inherited_ansible_roles + ansible_roles).uniq
end

#available_ansible_rolesObject



37
38
39
# File 'app/models/concerns/foreman_ansible/host_managed_extensions.rb', line 37

def available_ansible_roles
  AnsibleRole.where.not(:id => all_ansible_roles.pluck(:id))
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

#own_ansible_rolesObject



33
34
35
# File 'app/models/concerns/foreman_ansible/host_managed_extensions.rb', line 33

def own_ansible_roles
  ansible_roles.where.not(:id => inherited_ansible_roles.pluck(:id))
end

#play_ansible_rolesObject

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/concerns/foreman_ansible/host_managed_extensions.rb', line 43

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_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