Ansible – Modules and functions examples in playbooks


This document is indented to demonstrate various playbooks that makes use of different modules and their functionalities.

Pinging managed nodes

[root@centos9vm ~]# cat inventory

====== ======= =


===== =====
[root@centos9vm ~]# cat ping_all.yml

========= ====
– – –
– name: An example to ping all managed nodes in the inventory file
    hosts: all
        – name: Ping all

==== ==== ==
[root@centos9vm ~]# ansible-navigator run -m stdout ping_all.yml

===== ====

PLAY [An example to ping all managed nodes in the inventory file] **************

TASK [Gathering Facts] *********************************************************
fatal: []: UNREACHABLE! => {“changed”: false, “msg”: “Failed to connect to the host via ssh: root@ Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).”, “unreachable”: true}
ok: []

TASK [Ping all] ****************************************************************
ok: []

PLAY RECAP ********************************************************************* : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Please review the log for errors.

====== ====

In the above example password-less ssh login was not configured with

Running Arbitrary Commands on Managed Hosts

We can execute scripts and command present in managed nodes from an ansible controller. A use case can be where a script is copied from controller node to managed node, and then executed from the managed node itself.

Let us ensure the script of command exists in the managed node:

[root@centos9vm ~]# ssh ‘cat /root/’

===== ==
/usr/bin/touch /tmp/sam123.txt

===== ===

Let us create a playbook

[root@centos9vm ~]# cat runCommand.yml

==== ====
– – –
– name: An example to run a command in managed node
        – name: Execute a command
                cmd: /root/

===== ====

[root@centos9vm ~]# ansible-navigator run -m stdout runCommand.yml

===== ==

PLAY [An example to run a command in managed node] ****************************

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

TASK [Execute a command] *******************************************************
changed: []

PLAY RECAP ********************************************************************* : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
===== ====
[root@centos9vm ~]# ssh ‘ls -l /tmp/sam*’

===== ====
-rw-r–r–. 1 root root 0 Mar 5 17:37 /tmp/sam123.txt

Check is a website can be accessed

Let us create a playbook that installs httpd, created a webpage, and then starts the service. Finally we will verify is the site can be accessed and gets a valid response.

[root@centos9vm ~]# cat acessWebsite.yml

==== ==== ===
– – –
    – name: install webserver and access webpage
            – name: install httpd
                    name: httpd
                    state: latest

            – name: Add an index file
                    content: “Hello site\n”
                    dest: /var/www/html/index.html

            – name: start httpd
                    name: httpd
                    state: started
                    enabled: true

            – name: verify webpage
                    return_content: true
                    status_code: 200


Let us run the playbook and verify.

[root@centos9vm ~]# ansible-navigator run -m stdout acessWebsite.yml

===== ===

PLAY [install webserver and access webpage] ************************************

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

TASK [install httpd] ***********************************************************
ok: []

TASK [Add an index file] *******************************************************
changed: []

TASK [start httpd] *************************************************************
ok: []

TASK [verify webpage] **********************************************************
ok: []

PLAY RECAP *********************************************************************

==== =