Module: Shelter::CLI::AnsibleHelpers

Defined in:
lib/cli/ansible_helper.rb

Overview

Mixin module for Ansible

Instance Method Summary collapse

Instance Method Details

#ansible_execute(playbook, options = {}) ⇒ Object

server_user: nil, inventory: nil, tags: [], skip: [], limit: nil



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cli/ansible_helper.rb', line 40

def ansible_execute(playbook, options = {})
  params = {
    inventory: nil, server_user: nil, tags: [], skip: [], limit: nil
  }.merge(options)
  command = [command_bin, inventory_file(params[:inventory]),
             vault_password_file]
  command += new_server_params(params[:server_user])
  command += optional_params(params)
  command << "#{App.config.ansible_directory}/#{playbook}.yaml"

  full_command = command.join(' ')
  system full_command
end

#command_binObject



19
20
21
# File 'lib/cli/ansible_helper.rb', line 19

def command_bin
  'ansible-playbook'
end

#inventory_file(inventory) ⇒ Object



14
15
16
17
# File 'lib/cli/ansible_helper.rb', line 14

def inventory_file(inventory)
  inventory ||= App.config.inventory_script
  "--inventory #{inventory}"
end

#new_server_params(server_user) ⇒ Object



23
24
25
26
27
# File 'lib/cli/ansible_helper.rb', line 23

def new_server_params(server_user)
  command = []
  command << "--user #{server_user} --ask-pass" unless server_user.nil?
  command
end

#optional_params(opts) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/cli/ansible_helper.rb', line 29

def optional_params(opts)
  command = []
  command << "--tags '#{opts[:tags].join(',')}'" unless opts[:tags].empty?
  command << "--skip-tags '#{opts[:skip].join(',')}'" unless
    opts[:skip].empty?
  command << "--limit '#{opts[:limit]}'" unless opts[:limit].nil?
  command
end

#vault_password_fileObject



7
8
9
10
11
12
# File 'lib/cli/ansible_helper.rb', line 7

def vault_password_file
  [
    '--vault-password-file',
    "#{App.config.secure_root}/ansible_vault_password"
  ].join(' ')
end