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 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