Conditionally define variable in Ansible

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

    This code may help you to define a variable with condition.

    - hosts: node1
      gather_facts: yes
      tasks:
       - name: Check File
         shell: ls -ld /etc/postfix/post-install
         register: result
         ignore_errors: yes
    
       - name: Define Variable
         set_fact:
             exists: "{{ result.stdout }}"
         when: result|success
    
       - name: Display Variable
         debug: msg="{{ exists }}"
         ignore_errors: yes
    

    So here the exists will display only if the condition is true.

提交回复
热议问题