Module: Ansible

Defined in:
lib/rbbt/workflow/integration/ansible.rb,
lib/rbbt/workflow/integration/ansible/workflow.rb

Defined Under Namespace

Modules: AnsibleWorkflow

Class Method Summary collapse

Class Method Details

.clean_symbols(hash) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rbbt/workflow/integration/ansible.rb', line 22

def self.clean_symbols(hash)
  new = {}
  hash.each do |key,value|
    key = key.to_s
    value = case value
            when Symbol
              value.to_s
            when Hash
              self.clean_symbols(value)
            else
              value
            end
    new[key] = value
  end
  new
end

.play(playbook, inventory = nil, verbose = false) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rbbt/workflow/integration/ansible.rb', line 5

def self.play(playbook, inventory = nil, verbose = false)
  inventory = Rbbt.etc.ansible_inventory.find
  Log.with_severity 0 do
    TmpFile.with_file do |tmp|
      if Hash === playbook
        Open.write(tmp, [playbook].to_yaml)
        playbook = tmp
      end
      if verbose
        CMD.cmd_log("ansible-playbook -i #{inventory} -v #{playbook}")
      else
        CMD.cmd_log("ansible-playbook -i #{inventory} #{playbook}")
      end
    end
  end
end

.playbook(file, task = nil, options = {}) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/rbbt/workflow/integration/ansible.rb', line 50

def self.playbook(file, task = nil, options = {})
  task = 'default' if task.nil?

  workflow = Workflow === file ? file : Workflow.require_workflow(file)
  task = workflow.tasks.keys.last if workflow.tasks[task].nil?
  workflow2playbook workflow, task, options
end

.workflow2playbook(workflow, task, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/rbbt/workflow/integration/ansible.rb', line 39

def self.workflow2playbook(workflow, task, options = {})
  job_options = workflow.get_SOPT(workflow.tasks[task])

  tasks = workflow.job(task, nil, job_options).exec

  hosts = options[:hosts] || 'localhost'

  clean_tasks = tasks.collect{|task| self.clean_symbols task }
  {"hosts" => hosts, "tasks" => clean_tasks}
end