Module: Ansible::Methods

Included in:
Ansible, AdHoc
Defined in:
lib/ansible/ad_hoc.rb

Constant Summary collapse

BIN =
'ansible'

Instance Method Summary collapse

Instance Method Details

#list_hosts(cmd) ⇒ Object



13
14
15
16
# File 'lib/ansible/ad_hoc.rb', line 13

def list_hosts cmd
  output = one_off("#{cmd} --list-hosts").gsub!(/\s+hosts.*:\n/, '').strip
  output.split("\n").map(&:strip)
end

#one_off(cmd) ⇒ Object Also known as: []



8
9
10
# File 'lib/ansible/ad_hoc.rb', line 8

def one_off cmd
  `#{config.to_s "#{BIN} #{cmd}"}`
end

#parse_host_vars(host, inv_file, filter = 'hostvars[inventory_hostname]') ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ansible/ad_hoc.rb', line 18

def parse_host_vars(host, inv_file, filter = 'hostvars[inventory_hostname]')
  cmd = "all -m debug -a 'var=#{filter}' -i #{inv_file} -l #{host}"
  json = self[cmd].split(/>>|=>/).last

  # remove any colour added to console output
  # TODO move to Output module as #bleach, perhaps use term-ansicolor
  # possibly replace regexp with /\e\[(?:(?:[349]|10)[0-7]|[0-9]|[34]8;5;\d{1,3})?m/
  # possibly use ANSIBLE_NOCOLOR? or --nocolor
  json = json.strip.chomp.gsub(/\e\[[0-1][;]?(3[0-7]|90|1)?m/, '')

  hostvars = JSON.parse(json)

  hostvars[filter]
end