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
   - action: ping
     register: hello
   - debug: msg="{hello.stdout}"

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: []
ok: []

TASK [ping] ************************************************************************************************************************************************************************************
ok: []
ok: []

TASK [debug] ***********************************************************************************************************************************************************************************
ok: [] => {
    "msg": "{hello.stdout}"
ok: [] => {
    "msg": "{hello.stdout}"

PLAY RECAP *************************************************************************************************************************************************************************************     : ok=3    changed=0    unreachable=0    failed=0     : ok=3    changed=0    unreachable=0    failed=0   

Leave a Reply

Your email address will not be published.