Ansible – Playbook example and execution


In this example we will write a simple playbook in Ansible, that will create a new user in a managed host.

For this we need to ensure the managed host is present in the “inventory” file that will be used

[root@centos9vm ~]# cat inventory

===== ====





theUser = shiju500

===== ====

Ensure the Ansible server can SSH to the managed node without a password

Let us create a new playbook named shijuplaybook.yml

[root@centos9vm ~]# cat shijuplaybook.yml

========= ========
– – –
– name: shiju sample playbook
        – name: Add a user shiju100
                name: shiju100
                state: present

        – name: Installing httpd package
                name: httpd
                state: present
========= ========

Now let us check the syntax of the newly created yml file before actually executing the playbook

[root@centos9vm ~]# ansible-navigator run -m stdout shijuplaybook.yml –syntax-check
playbook: /root/shijuplaybook.yml

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

==== ===

PLAY [shiju sample playbook] ***************************************************

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

TASK [Add a user shiju100] *****************************************************
changed: []

TASK [Install httpd server] ****************************************************
changed: []

PLAY RECAP ********************************************************************* : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@centos9vm ~]#

==== ===

Verify is the new user got added in the node