Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a quick check, this doesn't seem to be used any more.
If this is not used, can't we remove it? Or are you going the most conservative route just in case? 🤔

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BaCaRoZzo I seem to remember that I removed it and something broke so i left it as is, but im not 100% sure

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EIther that or I thought i might want to keep the plugin's compatibility with older ansible versions

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