Categories
ansible devops

ansible playbook example

In this post we will see ansible playbook example, Play book is written in yaml format. Inside that we will have multiple tasks and roles

Below is the simple ansible play book with mutiple task

---
- hosts: 10.118.225.56
  tasks:
    - name: first server
      shell: "mkdir /home/host1"	
- hosts: 10.118.225.56
  tasks:
    - name: first server
      shell: "mkdir /home/host1"
- hosts: 10.118.225.56
  tasks:
    - name: first server
      shell: "mkdir /home/host1"
- hosts: 10.118.225.56
  tasks:
    - name: first server
      shell: "mkdir /home/host1"

Related Articles

when condition in ansible playbook

Categories
ansible devops

multiple hosts in ansible playbook

In this tutorial, we will learn about using multiple hosts in ansible playbook

In below example, we configured separate host for each task.

mutiplehost.yml
---
- hosts: 10.118.225.56
  tasks:
    - name: first server
      shell: "mkdir /home/host1"	
- hosts: 10.118.225.56
  tasks:
    - name: first server
      shell: "mkdir /home/host1"
- hosts: 10.118.225.56
  tasks:
    - name: first server
      shell: "mkdir /home/host1"
- hosts: 10.118.225.56
  tasks:
    - name: first server
      shell: "mkdir /home/host1"
Command
ansible-playbook mutiplehost.yml
Related Articles

when condition in ansible playbook

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

Categories
ansible devops

ignore warning message in ansible

In this tutorial, we will learn to ignore the warning message in ansible logs

While working with ansible playbooks, we will get some annoying warning message which makes us uncomfortable.

Below is the useful code snippet to get rid of ansible warning logs

 args:
    warn: false

Example

---

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

    - name: Download java file from github using curl      
      shell:  curl https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/AddDaysToDate.java --output sample.java  
      args:
        warn: false

Output

PLAY [Network Getting Started First Playbook] ********************************************************************************************************

TASK [Download java file from github using curl] *****************************************************************************************************

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

Previous Output

PLAY [Network Getting Started First Playbook] ********************************************************************************************************

TASK [Download java file from github using curl] *****************************************************************************************************
[WARNING]: Consider using the get_url or uri module rather than running 'curl'.  If you need to use command because get_url or uri 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.

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

Command

ansible-playbook  first_playbook.yml
Related Articles

Ignore Error in Ansible playbook