Conditionally define variable in Ansible

后端 未结 4 1842
情深已故
情深已故 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:07

    I believe you're after the default(omit) filter. (Reference).

    As per the example, mode will behave like it wasn't set at all for the first two items in the loop.

    - name: touch files with an optional mode
      file:
        dest: "{{item.path}}"
        state: touch
        mode: "{{item.mode|default(omit)}}"
      loop:
        - path: /tmp/foo
        - path: /tmp/bar
        - path: /tmp/baz
          mode: "0444"
    

提交回复
热议问题