有条件地为关联哈希添加额外参数

时间:2014-01-02 12:35:23

标签: ruby vagrant provisioning

我想有条件地在关联哈希中添加一个额外的参数。

现有代码如下所示:

: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
},

如果"ANSIBLE_SSH_ARGS" => "-o ForwardAgent=yes"config.ssh.forward_agent,我想有条件地添加另一个变量true

我可以复制粘贴,并创建一个if / else块但是Ruby确实有更优雅的东西吗?

1 个答案:

答案 0 :(得分:2)

我这样解决了:

   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
    }

不确定这是惯用的Ruby,但它对我有用。