Categories
ansible devops

when condition in ansible playbook

In this tutorial, we will learn about when condition in ansible playbook

We don’t have If condition here. Instead of that we can use when condition

Syntax
when: {condition}
when: inventory_hostname == "local"

In below example we are using when condition based on host

Example
---
- hosts: 127.0.0.1
  tasks:
  - name:  Make a directory  
    shell: "mkdir sample"
    when: inventory_hostname == "127.0.0.1"
    
  - name: make directory
    shell: "mkdir sample"
    when: inventory_hostname == "10.118.226.25" or inventory_hostname == "10.118.225.65"
Execute Command
ansible-playbook whenexample.yml
Output
PLAY [127.0.0.1] *************************************************************************************************************************************

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

TASK [Make a directory] ******************************************************************************************************************************
[WARNING]: Consider using the file module with state=directory rather than running 'mkdir'.  If you need to use command because file is insufficient
you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.

changed: [127.0.0.1]

TASK [make directory] ********************************************************************************************************************************
skipping: [127.0.0.1]

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

when condition matches you can see the changed: [127.0.0.1]in result.
Else it will skip the task skipping: [127.0.0.1] in result

Reference

Below is Ansible documentation for conditions https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html

Related Articles

ignore warning message in ansible

Ignore Error in Ansible playbook

Categories
ansible devops

Ignore Error in Ansible playbook

 

In this tutorial, we will learn to ignore error in ansible playbook.

While Executing play book if any error occurred in a task. Remaining tasks will get ignored

Some Cases we want to ignore the error. Ansible provide option to ignore error on Exception handling 

below is the Syntax to ignore error in ansible playbook  

Syntax
ignore_errors: yes
Sample playbook
---

- name: Network Getting Started First Playbook  
  gather_facts: false
  hosts: all
  tasks:

    - name: Make a directory    
      shell: "mkdir sample"       
	- name: Make another directory with same name  
      shell: "mkdir sample" 
      ignore_errors: yes
	- name: Make another directory with another name  
      shell: "mkdir demo" 
      ignore_errors: yes
Output
fatal: [127.0.01]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": true, "cmd": "mkdir sample", "delta": "0:00:00.008711", "end": "2020-02-21 10:23:37.305132", "msg": "non-zero return code", "rc": 1, "start": "2020-02-21 10:23:37.296421", "stderr": "mkdir: cannot create directory ‘sample’: File exists", "stderr_lines": ["mkdir: cannot create directory ‘sample’: File exists"], "stdout": "", "stdout_lines": []}
...ignoring

In the above example. we are trying to make a duplicate directory which will occur an error. So we used ignore_errors command to ignore the error and to proceed further

we can see the <span class="token punctuation">..</span>.ignoring

Reference

https://docs.ansible.com/ansible/latest/user_guide/playbooks_error_handling.html

Related Articles

ignore warning message in ansible