From d1faf7b09236da148add08f183f5fab172a7f13a Mon Sep 17 00:00:00 2001 From: George Kranis Date: Tue, 28 Mar 2023 19:19:20 +0000 Subject: [PATCH 1/7] Switch to ansible_host --- lib/kitchen-ansible/util_inventory.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kitchen-ansible/util_inventory.rb b/lib/kitchen-ansible/util_inventory.rb index 783f20a..0cc93e5 100644 --- a/lib/kitchen-ansible/util_inventory.rb +++ b/lib/kitchen-ansible/util_inventory.rb @@ -18,6 +18,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 +31,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 } From d912836cedcdb5c3c9e2158a15f2962c11047e27 Mon Sep 17 00:00:00 2001 From: George Kranis Date: Wed, 12 Apr 2023 13:36:37 +0000 Subject: [PATCH 2/7] Log output instead of sending to stdout directly --- lib/kitchen/provisioner/ansible_push.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/kitchen/provisioner/ansible_push.rb b/lib/kitchen/provisioner/ansible_push.rb index 567ade1..6bdb617 100644 --- a/lib/kitchen/provisioner/ansible_push.rb +++ b/lib/kitchen/provisioner/ansible_push.rb @@ -243,8 +243,8 @@ 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 + stdout_and_stderr, exit_code = Open3.capture2e(env, command.to_s) + info(stdout_and_stderr) 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 From dab197e76a67da4370dbaf69b268cc6d256256bb Mon Sep 17 00:00:00 2001 From: George Kranis Date: Wed, 12 Apr 2023 13:36:59 +0000 Subject: [PATCH 3/7] Isolate ansible inventory per instance --- lib/kitchen-ansible/print_inventory_cli.rb | 7 ++++--- lib/kitchen-ansible/util_inventory.rb | 7 +++++-- lib/kitchen/provisioner/ansible_push.rb | 7 ++++--- 3 files changed, 13 insertions(+), 8 deletions(-) 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 0cc93e5..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 diff --git a/lib/kitchen/provisioner/ansible_push.rb b/lib/kitchen/provisioner/ansible_push.rb index 6bdb617..54b2198 100644 --- a/lib/kitchen/provisioner/ansible_push.rb +++ b/lib/kitchen/provisioner/ansible_push.rb @@ -180,7 +180,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] @@ -269,9 +270,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 From 1d608b47469fabe1449cf11aedafe5d4d79890e8 Mon Sep 17 00:00:00 2001 From: George Kranis Date: Fri, 7 Apr 2023 20:24:41 +0000 Subject: [PATCH 4/7] Dont limit --- lib/kitchen/provisioner/ansible_push.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/kitchen/provisioner/ansible_push.rb b/lib/kitchen/provisioner/ansible_push.rb index 54b2198..0127a8f 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] From e26e40c068de032a93af2ef1825c07dced6197ec Mon Sep 17 00:00:00 2001 From: George Kranis Date: Fri, 21 Apr 2023 15:59:28 +0000 Subject: [PATCH 5/7] Stream output --- lib/kitchen/provisioner/ansible_push.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/kitchen/provisioner/ansible_push.rb b/lib/kitchen/provisioner/ansible_push.rb index 0127a8f..a2afea8 100644 --- a/lib/kitchen/provisioner/ansible_push.rb +++ b/lib/kitchen/provisioner/ansible_push.rb @@ -238,10 +238,14 @@ def run_command def exec_ansible_command(env, command, desc) debug("env=#{env} command=#{command}") - stdout_and_stderr, exit_code = Open3.capture2e(env, command.to_s) - info(stdout_and_stderr) - 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(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 From c97140319e322ee480232b790b36ad8174d41c05 Mon Sep 17 00:00:00 2001 From: George Kranis Date: Mon, 24 Apr 2023 15:13:36 +0000 Subject: [PATCH 6/7] Prefix ansible logs with kitchen suite name --- lib/kitchen/provisioner/ansible_push.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kitchen/provisioner/ansible_push.rb b/lib/kitchen/provisioner/ansible_push.rb index a2afea8..eed7ea2 100644 --- a/lib/kitchen/provisioner/ansible_push.rb +++ b/lib/kitchen/provisioner/ansible_push.rb @@ -240,7 +240,7 @@ def exec_ansible_command(env, command, desc) debug("env=#{env} command=#{command}") Open3.popen2e(env, command.to_s) do |stdin, stdout_and_stderr, status_thread| stdout_and_stderr.each_line do |line| - info(line) + info("[#{instance.name}]#{line}") end exit_code = status_thread.value debug("ansible-playbook exit code = #{exit_code}") From d5c3c4f03dff6b8746082b70c40691aaf1e19d4f Mon Sep 17 00:00:00 2001 From: George Kranis Date: Fri, 2 Jun 2023 16:16:41 +0000 Subject: [PATCH 7/7] Fix tests --- spec/kitchen/kitchen-ansible/print_inventory_cli_spec.rb | 1 + spec/kitchen/provisioner/ansible_push_options_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) 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