Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple issues #72

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/kitchen-ansible/print_inventory_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

class PrintInventory
def initialize
temp_group_path = File.join(TEMP_INV_DIR, ENV["INSTANCE_NAME"], TEMP_GROUP_FILE)
@inventory = {}
@all = []
@groups = if File.exist?(TEMP_GROUP_FILE)
read_from_yaml TEMP_GROUP_FILE
@groups = if File.exist?(temp_group_path)
read_from_yaml temp_group_path
else
{}
end
Expand All @@ -20,7 +21,7 @@ def read_from_yaml(yaml_file)
end

def read_all_hosts
Dir.glob(TEMP_INV_DIR + '/ansiblepush_host_*.yml')
Dir.glob(File.join(TEMP_INV_DIR, ENV["INSTANCE_NAME"], 'ansiblepush_host_*.yml'))
end

def construct
Expand Down
9 changes: 6 additions & 3 deletions lib/kitchen-ansible/util_inventory.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require 'fileutils'

TEMP_INV_DIR = '.kitchen/ansiblepush'.freeze
TEMP_GROUP_FILE = "#{TEMP_INV_DIR}/ansiblepush_groups_inventory.yml".freeze
TEMP_GROUP_FILE = "ansiblepush_groups_inventory.yml".freeze

def write_var_to_yaml(yaml_file, hash_var)
Dir.mkdir TEMP_INV_DIR unless File.exist?(TEMP_INV_DIR)
base_path = File.dirname(yaml_file)
FileUtils.mkdir_p base_path unless File.exist?(base_path)
File.open(yaml_file, 'w') do |file|
file.write hash_var.to_yaml
end
Expand All @@ -18,6 +21,7 @@ def generate_instance_inventory(name, host, mygroup, instance_connection_option,
end

temp_hash = {}
temp_hash['ansible_host'] = host
temp_hash['ansible_ssh_host'] = host
temp_hash['ansible_ssh_port'] = port if port
temp_hash['ansible_ssh_private_key_file'] = keys[0] if keys
Expand All @@ -30,7 +34,6 @@ def generate_instance_inventory(name, host, mygroup, instance_connection_option,
temp_hash['ansible_winrm_server_cert_validation'] = 'ignore'
temp_hash['ansible_winrm_transport'] = 'ssl'
temp_hash['ansible_connection'] = 'winrm'
temp_hash['ansible_host'] = temp_hash['ansible_ssh_host']
temp_hash['ansible_user'] = temp_hash['ansible_ssh_user']
end
{ name => temp_hash }
Expand Down
25 changes: 12 additions & 13 deletions lib/kitchen/provisioner/ansible_push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,6 @@ def options
options << "--start-at-task=#{conf[:start_at_task]}" if conf[:start_at_task]
options << "--inventory-file=#{conf[:generate_inv_path]}" if conf[:generate_inv]
options << verbosity_argument.to_s if conf[:verbose]
# By default we limit by the current machine,
options << if conf[:limit]
"--limit=#{as_list_argument(conf[:limit])}"
else
"--limit=#{machine_name}"
end
options << "--timeout=#{conf[:timeout]}" if conf[:timeout]
options << "--force-handlers=#{conf[:force_handlers]}" if conf[:force_handlers]
options << "--step=#{conf[:step]}" if conf[:step]
Expand All @@ -180,7 +174,8 @@ def command_env
@command_env = {
'PYTHONUNBUFFERED' => '1', # Ensure Ansible output isn't buffered
'ANSIBLE_FORCE_COLOR' => 'true',
'ANSIBLE_HOST_KEY_CHECKING' => conf[:host_key_checking].to_s
'ANSIBLE_HOST_KEY_CHECKING' => conf[:host_key_checking].to_s,
'INSTANCE_NAME' => instance.name.gsub(/[<>]/, '')
}
@command_env['ANSIBLE_CONFIG'] = conf[:ansible_config] if conf[:ansible_config]

Expand Down Expand Up @@ -243,10 +238,14 @@ def run_command

def exec_ansible_command(env, command, desc)
debug("env=#{env} command=#{command}")
system(env, command.to_s)
exit_code = $CHILD_STATUS.exitstatus
debug("ansible-playbook exit code = #{exit_code}")
raise UserError, "#{desc} returned a non zero #{exit_code}. Please see the output above." if exit_code.to_i != 0
Open3.popen2e(env, command.to_s) do |stdin, stdout_and_stderr, status_thread|
stdout_and_stderr.each_line do |line|
info("[#{instance.name}]#{line}")
end
exit_code = status_thread.value
debug("ansible-playbook exit code = #{exit_code}")
raise UserError, "#{desc} returned a non zero #{exit_code}. Please see the output above." if exit_code.to_i != 0
end
end

def instance_connection_option
Expand All @@ -269,9 +268,9 @@ def prepare_inventory
debug("hostname='#{hostname}'")
# Generate hosts
hosts = generate_instance_inventory(machine_name, hostname, conf[:mygroup], instance_connection_option, conf)
write_var_to_yaml("#{TEMP_INV_DIR}/ansiblepush_host_#{machine_name}.yml", hosts)
write_var_to_yaml("#{TEMP_INV_DIR}/#{instance.name.gsub(/[<>]/, '')}/ansiblepush_host_#{machine_name}.yml", hosts)
# Generate groups (if defined)
write_var_to_yaml(TEMP_GROUP_FILE, conf[:groups]) if conf[:groups]
write_var_to_yaml("#{TEMP_INV_DIR}/#{instance.name.gsub(/[<>]/, '')}/#{TEMP_GROUP_FILE}", conf[:groups]) if conf[:groups]
end

def extra_vars_argument
Expand Down
1 change: 1 addition & 0 deletions spec/kitchen/kitchen-ansible/print_inventory_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

describe PrintInventory do
before :each do
ENV['INSTANCE_NAME'] = 'example_suite'
@printinventory = PrintInventory.new
end

Expand Down
4 changes: 2 additions & 2 deletions spec/kitchen/provisioner/ansible_push_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

it 'match min' do
expect(provisioner.options).to eq(['--become', '--become-user=kitchen',
'--user=test', '--limit='])
'--user=test'])
end
end

Expand Down Expand Up @@ -140,7 +140,7 @@
it 'match all' do
expect(provisioner.options).to eq(['--become', '--become-user=kitchen2', '--user=test2', '--become-method=sudo',
'--private-key=/tmp/rsa_key', '--diff', '--ask-vault-pass', '--start-at-task=c1',
'--limit=', '--timeout=10', '--force-handlers=true', '--step=true',
'--timeout=10', '--force-handlers=true', '--step=true',
'--module-path=/xxx', '--scp-extra-args=x', '--sftp-extra-args=y', '--ssh-common-args=z',
'--ssh-extra-args=r', '-raw'])
end
Expand Down