First YAML Script: Ansible playbook to ping all hosts in the list.

Let us start creating the ansible yaml scripts or playbooks.

The first playbook that we create involves checking if the host is alive or not i.e. ping status of all hosts in the list.

1. Ansible playbook to ping all hosts

$ cat playbooks/secondplaybook.yml 
---
 - name: "Get ping response"
   hosts: all
   tasks:
   - action: ping
     register: hello
   - debug: msg="{hello.stdout}"

Explanation
1. “—“ marks the beginning of YAML script.
2. “- name” represents a list item with key value pair “Get ping response”
3. “hosts” key represents which host group to use. “all” is the default keyword that contains all hosts mentioned in the hosts file.
4. “tasks” is a list that maps with the different key:value pairs like:
a. What action to do: ping the hosts
b. “register” is used to capture the output of a task in ansible.
debug keyword is a part of ansible register that can get us the output or error of the last task execution.


2. Output

$ ansible-playbook playbooks/secondplaybook.yml 

PLAY [Get ping response] ***********************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************
ok: [r168-host1.ngelinux.com]
ok: [r005-host1.ngelinux.com]

TASK [ping] ************************************************************************************************************************************************************************************
ok: [r168-host1.ngelinux.com]
ok: [r005-host1.ngelinux.com]

TASK [debug] ***********************************************************************************************************************************************************************************
ok: [r005-host1.ngelinux.com] => {
    "msg": "{hello.stdout}"
}
ok: [r168-host1.ngelinux.com] => {
    "msg": "{hello.stdout}"
}

PLAY RECAP *************************************************************************************************************************************************************************************
r005-host1.ngelinux.com     : ok=3    changed=0    unreachable=0    failed=0   
r168-host1.ngelinux.com     : ok=3    changed=0    unreachable=0    failed=0   
$ 

Leave a Reply

avatar
  Subscribe  
Notify of