Class: Vagrant::Provisioners::Ansible

Inherits:
Base
  • Object
show all
Includes:
Util::SafeExec
Defined in:
lib/vagrant-ansible/provisioner.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

VERSION =
'0.0.5'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.config_classObject



44
45
46
# File 'lib/vagrant-ansible/provisioner.rb', line 44

def self.config_class
  Config
end

Instance Method Details

#provision!Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vagrant-ansible/provisioner.rb', line 76

def provision!
  ssh = env[:vm].config.ssh

  with_inventory_file(ssh) do |inventory_file|
    options = %W[--user=#{ssh.username}
                 --inventory-file=#{inventory_file}
                 --private-key=#{env[:vm].env.default_private_key_path}]

    options << "--ask-sudo-pass" if config.ask_sudo_pass
    options << "--sudo" if config.sudo
    options = options + config.options unless config.options.empty?

    cmd = (%w(ansible-playbook) << options << config.playbook).flatten

    safe_exec *cmd
  end
end

#with_inventory_file(ssh) ⇒ Object

This methods yield the path to a temporally created inventory file.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vagrant-ansible/provisioner.rb', line 50

def with_inventory_file(ssh)
  if not config.inventory_file.nil?
    yield config.inventory_file
  else
    begin
      forward = env[:vm].config.vm.forwarded_ports.select do |x|
        x[:guestport] == ssh.guest_port
      end.first[:hostport]
      if not config.hosts.kind_of?(Array)
        config.hosts = [config.hosts]
      end
      file = Tempfile.new('inventory')
      config.hosts.each do |host|
        file.write("[#{host}]\n")
        file.write("#{ssh.host}:#{forward}\n")
        file.write("\n")
      end
      file.fsync
      file.close
      yield file.path
    ensure
      file.unlink
    end
  end
end