Conditionally define variable in Ansible

后端 未结 4 1845
情深已故
情深已故 2021-02-01 12:35

I want to conditionally define a variable in an Ansible playbook like this:

my_var: \"{{ \'foo\' if my_condition}}\"
         


        
4条回答
  •  别那么骄傲
    2021-02-01 13:22

    You could use something like this:

    my_var: "{{ 'foo' if my_condition else '' }}"
    

    The 'else' will happen if condition not match, and in this case will set a empty value for the variable. I think this is a short, readable and elegant solution.

提交回复
热议问题