Home‎ > ‎unix/linux‎ > ‎ansible‎ > ‎

Conditionals


Check global variable, and if match do something



The playbook first collects the contents of the "ansible_domain" variable, and the debug command prints out the domain.  Then it checks to see if that 
---
# collect some data from the host, depending on the data collected, do something. 

- hosts: localhost
  gather_facts: yes
  tasks: 

    - name: printing Variable
      debug: 
        var: ansible_domain
    
    - name: check if this is in mb2
      debug: 
        msg: ">> this is in MB2 "
      when: ansible_domain == "mb2.prod.cmed.us"
    
    - name: check if this is in fhm
      debug: 
        msg: ">> this is in FHM "
      when: ansible_domain == "fhm.prod.cmed.us"

...

If we run it on one test machine that does not match either of the domains, it skips the "check" task
cm@Balsa ~/ansible $ ansible-playbook  playbooks/condition-do.yml

PLAY [localhost] ************************************************************************************

TASK [Gathering Facts] ******************************************************************************
ok: [localhost]

TASK [printing Variable] ****************************************************************************
ok: [localhost] => {
    "ansible_domain": "0.0.127.in-addr.arpa"
}

TASK [check if this is in mb2] **********************************************************************
skipping: [localhost]

TASK [check if this is in fhm] **********************************************************************
skipping: [localhost]

PLAY RECAP ******************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0

cm@Balsa ~/ansible $ 

If it runs on a host that does match, then it runs the task that matches.  
In this case, the domain output is "mb2.prod.cmed.us" which is matched in the second task.  With that match, the task prints out the message ">> this is in MB2", but you could modify that task so it would run a different module or even a role.  
[root@boss01 ansible]# ansible-playbook condition-do.yml

PLAY [localhost] ****************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************
ok: [localhost]

TASK [printing Variable] ********************************************************************************************************
ok: [localhost] => {
    "ansible_domain": "mb2.prod.cmed.us"
}

TASK [check if this is in mb2] **************************************************************************************************
ok: [localhost] => {
    "msg": ">> this is in MB2 "
}

TASK [check if this is in fhm] **************************************************************************************************
skipping: [localhost]

PLAY RECAP **********************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

[root@boss01 ansible]#


Reference: 

 



Comments