Ansible – Modules and functions examples in playbooks

Ansible_Logo

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

Pinging managed nodes

[root@centos9vm ~]# cat inventory

====== ======= =
192.168.48.128
192.168.48.129

[testGRP]
192.168.48.128
192.168.48.129

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

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

==== ==== ==
[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: [192.168.48.128]: UNREACHABLE! => {“changed”: false, “msg”: “Failed to connect to the host via ssh: root@192.168.48.128: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).”, “unreachable”: true}
ok: [192.168.48.129]

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

PLAY RECAP *********************************************************************
192.168.48.128 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
192.168.48.129 : 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 192.168.48.129

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 192.168.48.129 ‘cat /root/shiju_touch.sh’

===== ==
#!/usr/bin/bash
/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
    hosts: 192.168.48.129
    tasks:
        – name: Execute a command
            ansible.builtin.command:
                cmd: /root/shiju_touch.sh

===== ====

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

===== ==

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

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

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

PLAY RECAP *********************************************************************
192.168.48.129 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
===== ====
[root@centos9vm ~]# ssh 192.168.48.129 ‘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
        hosts: 192.168.48.129
        tasks:
            – name: install httpd
                ansible.builtin.dnf:
                    name: httpd
                    state: latest

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

            – name: start httpd
                ansible.builtin.service:
                    name: httpd
                    state: started
                    enabled: true

            – name: verify webpage
                ansible.builtin.uri:
                    url: http://192.168.48.129
                    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: [192.168.48.129]

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

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

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

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

PLAY RECAP *********************************************************************
192.168.48.129

==== =