diff --git a/lib/kitchen-ansible/print_inventory_cli.rb b/lib/kitchen-ansible/print_inventory_cli.rb index ccb148e..90750fc 100755 --- a/lib/kitchen-ansible/print_inventory_cli.rb +++ b/lib/kitchen-ansible/print_inventory_cli.rb @@ -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 @@ -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 diff --git a/lib/kitchen-ansible/util_inventory.rb b/lib/kitchen-ansible/util_inventory.rb index 783f20a..218ab35 100644 --- a/lib/kitchen-ansible/util_inventory.rb +++ b/lib/kitchen-ansible/util_inventory.rb @@ -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 @@ -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 @@ -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 } diff --git a/lib/kitchen/provisioner/ansible_push.rb b/lib/kitchen/provisioner/ansible_push.rb index 567ade1..eed7ea2 100644 --- a/lib/kitchen/provisioner/ansible_push.rb +++ b/lib/kitchen/provisioner/ansible_push.rb @@ -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] @@ -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] @@ -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 @@ -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 diff --git a/spec/kitchen/kitchen-ansible/print_inventory_cli_spec.rb b/spec/kitchen/kitchen-ansible/print_inventory_cli_spec.rb index 7f6a763..c37300b 100644 --- a/spec/kitchen/kitchen-ansible/print_inventory_cli_spec.rb +++ b/spec/kitchen/kitchen-ansible/print_inventory_cli_spec.rb @@ -4,6 +4,7 @@ describe PrintInventory do before :each do + ENV['INSTANCE_NAME'] = 'example_suite' @printinventory = PrintInventory.new end diff --git a/spec/kitchen/provisioner/ansible_push_options_spec.rb b/spec/kitchen/provisioner/ansible_push_options_spec.rb index 0a35272..57e2de1 100644 --- a/spec/kitchen/provisioner/ansible_push_options_spec.rb +++ b/spec/kitchen/provisioner/ansible_push_options_spec.rb @@ -38,7 +38,7 @@ it 'match min' do expect(provisioner.options).to eq(['--become', '--become-user=kitchen', - '--user=test', '--limit=']) + '--user=test']) end end @@ -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