Sha256: 5be1e07a0b9ea993ffcaa39fa822bea26af9073cc0e4328ee4a85f68b50050f6

Contents?: true

Size: 1.5 KB

Versions: 86

Compression:

Stored size: 1.5 KB

Contents

require_relative 'ansible/workflow'
require 'rbbt/workflow/usage'

module Ansible
  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

  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

  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

  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
end

Version data entries

86 entries across 86 versions & 1 rubygems

Version Path
rbbt-util-5.34.27 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.26 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.25 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.24 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.23 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.22 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.21 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.20 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.18 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.17 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.16 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.15 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.14 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.13 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.12 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.11 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.10 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.9 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.8 lib/rbbt/workflow/integration/ansible.rb
rbbt-util-5.34.7 lib/rbbt/workflow/integration/ansible.rb