Class: NodeSpec::Provisioning::Ansible

Inherits:
Object
  • Object
show all
Includes:
LocalCommandRunner
Defined in:
lib/nodespec/provisioning/ansible.rb

Constant Summary collapse

CUSTOM_CONFIG_FILENAME =
'nodespec_ansible_cfg'
CUSTOM_INVENTORY_FILENAME =
'nodespec_ansible_hosts'
AUTO_DISCOVERY_HOST_TEMPLATE =
<<-EOS
<%= "[" + group + "]" if group %>
<%= @node.name %> ansible_ssh_host=<%= @node.communicator.session.transport.host %> ansible_ssh_port=<%= @node.communicator.session.transport.port %>
EOS

Instance Method Summary collapse

Methods included from LocalCommandRunner

#run_command

Methods included from CommandExecution

#execute_within_timeout

Methods included from VerboseOutput

#verbose_puts

Constructor Details

#initialize(node) ⇒ Ansible

Returns a new instance of Ansible.



18
19
20
21
22
23
# File 'lib/nodespec/provisioning/ansible.rb', line 18

def initialize(node)
  @node = node
  @sudo_enabled = true
  @cmd_prefix_entries = []
  @tmp_files = []
end

Instance Method Details

#ansible_config(text) ⇒ Object



29
30
31
32
# File 'lib/nodespec/provisioning/ansible.rb', line 29

def ansible_config(text)
  file = create_temp_file(CUSTOM_CONFIG_FILENAME, text)
  @cmd_prefix_entries << "ANSIBLE_CONFIG=#{file.path.shellescape}"
end

#ansible_module(module_name, module_arguments, options = []) ⇒ Object



59
60
61
# File 'lib/nodespec/provisioning/ansible.rb', line 59

def ansible_module(module_name, module_arguments, options = [])
  build_and_run("ansible #{@node.name} -m #{module_name} -a #{module_arguments.shellescape}", options)
end

#ansible_playbook(playbook_path, options = []) ⇒ Object



55
56
57
# File 'lib/nodespec/provisioning/ansible.rb', line 55

def ansible_playbook(playbook_path, options = [])
  build_and_run("ansible-playbook #{playbook_path.shellescape} -l #{@node.name}", options)
end

#enable_host_auto_discovery(group = nil) ⇒ Object



34
35
36
37
# File 'lib/nodespec/provisioning/ansible.rb', line 34

def enable_host_auto_discovery(group = nil)
  file = create_temp_file(CUSTOM_INVENTORY_FILENAME, ERB.new(AUTO_DISCOVERY_HOST_TEMPLATE).result(binding))
  @hostfile_option = "-i #{file.path.shellescape}"
end

#run_as_sudo(enabled = true) ⇒ Object



47
48
49
# File 'lib/nodespec/provisioning/ansible.rb', line 47

def run_as_sudo(enabled = true)
  @sudo_enabled = enabled
end

#set_config_path(path) ⇒ Object



25
26
27
# File 'lib/nodespec/provisioning/ansible.rb', line 25

def set_config_path(path)
  @cmd_prefix_entries << "ANSIBLE_CONFIG=#{path.shellescape}"
end

#set_extra_vars(vars = {}) ⇒ Object



51
52
53
# File 'lib/nodespec/provisioning/ansible.rb', line 51

def set_extra_vars(vars = {})
  @extra_vars_option = "-e '#{JSON.generate(vars)}'"
end

#set_host_key_checking(enabled) ⇒ Object



43
44
45
# File 'lib/nodespec/provisioning/ansible.rb', line 43

def set_host_key_checking(enabled)
  @cmd_prefix_entries << "ANSIBLE_HOST_KEY_CHECKING=#{enabled.to_s.capitalize}"
end

#set_hostfile_path(path) ⇒ Object



39
40
41
# File 'lib/nodespec/provisioning/ansible.rb', line 39

def set_hostfile_path(path)
  @hostfile_option = "-i #{path.shellescape}"
end