Working with Ansible conditionals | How to Use When Statement in Ansible | linuxtopic

Linuxtopic
0


conditional is used to change a ansible play behavior based on there facts/variable. we can control which tasks/roles will be run or skipped on there targeted hosts/nodes with the help of when statement




ansible "when statement" example : we will create a condition.yml playbook




To Create yml playbook file, choose any name




cd  /etc/ansiblevi condition.yml






---


- hosts: all






  tasks:


  - name: get uptime


    command: uptime


    when: ansible_facts['os_family'] == "Debian"


    register: uptime






  - debug: var=uptime.stdout




ansible playbook, Ansible Conditionals, ansible when, ansible playbook examples, ansible playbook tutorial, ansible tutorial for beginners, ansible tasks, ansible, ansible tutorial, ansible example, ansible facts, ansible linux, ansible best practices,ansible  variables, ansible variables list, ansible global variable




add  remote hosts on inventory  Click Here...




To run ansible playbook on hosts




ansible-playbook   condition.yml








We can see that, localhost is CentOS 7 so task has been skipped and 192.168.3.104 is Debian so task executed.




Again we update condition in playbook, we add change !=




---


- hosts: all






  tasks:


  - name: get uptime


    command: uptime


    when: ansible_facts['os_family'] != "Debian"


    register: uptime






  - debug: var=uptime.stdout







conditional in ansible, ansible when, when statement, ansible tutorial, ansible playbook, ansible, jobs, ansible roles, ansible facts, ansible debug, when:
Ansible Conditionals - When Statement 





Again we run playbook




ansible-playbook   condition.yml








Now  task has been executed on  CentOS 7 and skipped on Debian.




Method 2:




Now we will use ansible facts as variable like ansible_distribution, following playbook




---


- hosts: all






  tasks:


  - name:  echo distribution - CentOS


    command: echo CentOS-PC


    when: ansible_distribution == "CentOS"


    register: centos




We can use AND condition  with following examples




---


- hosts: all






  tasks:


  - name:  echo distribution - CentOS


    command: echo CentOS-PC


    when: ansible_distribution == "CentOS"  and ansible_architecture == "x86_64"


    register: centos






 - debug: var=uptime.stdout








ansible-playbook  condition.yml








Now we will add more condition with and statement, and use loop to display all results. Our final playbook is :




---


- hosts: all






  tasks:


  - name:  echo distribution - CentOS


    command: echo CentOS-PC


    when: ansible_distribution == "CentOS"  and ansible_architecture == "x86_64"


    register: centos






  - name:  echo distribution - Ubuntu


    command: echo Ubuntu-PC


    when: ansible_distribution == "Ubuntu"  and ansible_architecture == "x86_64"


    register: ubuntu






  - debug: var={{ item }}


    loop:


      - centos.stdout


      - ubuntu.stdout








Now we will execute ansible playbook




ansible-playbook  condition.yml








Thanks

End of this ansible conditionals  tutorial,  we need your support so i request you to please comment if something missed by me, share and like this post.




www.linuxtopic.com


Post a Comment

0Comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Ok, Go it!