Module: Shelter::CLI::Helpers::Ansible

Defined in:
lib/cli/helpers/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



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

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



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

def command_bin
  'ansible-playbook'
end

#inventory_file(inventory) ⇒ Object



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

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

#new_server_params(server_user) ⇒ Object



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

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

#optional_params(opts) ⇒ Object



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

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_execute(command, file) ⇒ Object



56
57
58
59
60
# File 'lib/cli/helpers/ansible_helper.rb', line 56

def vault_execute(command, file)
  file = "/#{file}" unless file.start_with?('/')
  file = "#{App.config.secure_root}#{file}_secret.yaml"
  system "ansible-vault #{command} #{vault_password_file} #{file}"
end

#vault_password_fileObject



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

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