Class: ForemanAnsible::PlaybooksImporter

Inherits:
Object
  • Object
show all
Includes:
ProxyAPI
Defined in:
app/services/foreman_ansible/playbooks_importer.rb

Overview

Imports playbooks from smart proxy

Instance Method Summary collapse

Constructor Details

#initialize(proxy = nil) ⇒ PlaybooksImporter

Returns a new instance of PlaybooksImporter.



9
10
11
# File 'app/services/foreman_ansible/playbooks_importer.rb', line 9

def initialize(proxy = nil)
  @ansible_proxy = proxy
end

Instance Method Details

#create_job_template(playbook) ⇒ Object



55
56
57
58
59
60
61
62
# File 'app/services/foreman_ansible/playbooks_importer.rb', line 55

def create_job_template(playbook)
  job_template = JobTemplate.create(name: playbook[:name], template: playbook[:playbook_content], job_category: 'Ansible Playbook - Imported', provider_type: 'Ansible')
  # TODO: Add support for creating template inputs
  job_template.organizations = Organization.unscoped.all
  job_template.locations = Location.unscoped.all
  job_template.save
  { job_template.id => job_template.name }
end

#get_vars(playbook_content) ⇒ Object



51
52
53
# File 'app/services/foreman_ansible/playbooks_importer.rb', line 51

def get_vars(playbook_content)
  YAML.safe_load(playbook_content).map { |play| play['vars'] }
end

#import_playbooks(playbooks_names) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/foreman_ansible/playbooks_importer.rb', line 13

def import_playbooks(playbooks_names)
  playbooks = playbooks(playbooks_names)
  result = { created: {}, updated: {} }
  playbooks.each do |playbook|
    parsed_playbook = parse_playbook playbook
    job_template_id = JobTemplate.where(name: parsed_playbook[:name]).pick(:id)
    if job_template_id.present?
      updated = update_job_template(job_template_id, parsed_playbook)
      result[:updated].merge!(updated) unless updated.nil?
    else
      result[:created].merge!(create_job_template(parsed_playbook))
    end
  end
  result
end

#metadata(playbook_name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/foreman_ansible/playbooks_importer.rb', line 38

def (playbook_name)
  <<~END_HEREDOC
    <%#
      name: #{playbook_name}
      snippet: false
      job_category: Ansible Playbook - Imported
      provider_type: Ansible
      kind: job_template
      model: JobTemplate
    %>
  END_HEREDOC
end

#parse_playbook(playbook) ⇒ Object



29
30
31
32
33
34
35
36
# File 'app/services/foreman_ansible/playbooks_importer.rb', line 29

def parse_playbook(playbook)
  content = playbook['playbooks_content']
  {
    name: playbook['name'],
    playbook_content: (playbook['name']) + content,
    vars: get_vars(content)
  }
end

#update_job_template(job_template_id, playbook) ⇒ Object



64
65
66
67
68
69
70
71
# File 'app/services/foreman_ansible/playbooks_importer.rb', line 64

def update_job_template(job_template_id, playbook)
  # TODO: Add support for updating template inputs
  inputs = []
  job_template = JobTemplate.find(job_template_id)
  should_update = !playbook[:playbook_content].eql?(job_template.template)
  job_template.template = playbook[:playbook_content] if should_update
  { job_template.id => job_template.name } unless inputs.empty? && !should_update
end