I want to conditionally add an extra parameter to an associative hash.
The existing code looks like this:
:env => {
"ANSIBLE_FORCE_COLOR" => "true",
"ANSIBLE_HOST_KEY_CHECKING" => "#{config.host_key_checking}",
# Ensure Ansible output isn't buffered so that we receive ouput
# on a task-by-task basis.
"PYTHONUNBUFFERED" => 1
},
I want to conditionally add another variable "ANSIBLE_SSH_ARGS" => "-o ForwardAgent=yes"
if config.ssh.forward_agent
is true
.
I could just copy paste, and create an if/else block but surely Ruby has something more elegant?
I solved it like so:
env = {
"ANSIBLE_FORCE_COLOR" => "true",
"ANSIBLE_HOST_KEY_CHECKING" => "#{config.host_key_checking}",
# Ensure Ansible output isn't buffered so that we receive ouput
# on a task-by-task basis.
"PYTHONUNBUFFERED" => 1
}
env["ANSIBLE_SSH_ARGS"] ="-o ForwardAgent=yes" if config.ssh.forward_agent
command << {
:env => env,
:notify => [:stdout, :stderr],
:workdir => @machine.env.root_path.to_s
}
Not sure this is idiomatic Ruby but it worked for me.