Writing and executing a simple Ansible Playbook

In this article we will write a simple Playbook to install and configure Web server.

Playbook is a list of one or more Plays. It is in YAML file format with .yml extension.

Play is a list of hosts and tasks.

Hosts – it is a node or server which we have mentioned in the inventory
Tasks – it is a collection of key-value pairs which tells what modules needs to be executed in the order of sequence.

Lets create a Playbook in our Ansible master/Controller server(centos7) to Install and configure ‘httpd’ in client server named ‘centos6web’

[root@centos7 play]# pwd
[root@centos7 play]# ls
[root@centos7 play]# cat httpd.yml
- name: Installing and configuring HTTPD Web server
hosts: centos6web
- name: Installing HTTPD
name: httpd
state: latest
- name: Custom index.html
src: /root/supportfiles/index.html
dest: /var/www/html/index.html
- name: Starting and Enabling HTTPD
name: httpd
state: started
enabled: true
[root@centos7 play]#

Here ‘—‘ denotes the start of yaml file.

hosts: centos6web -> it refers that this play will be executed on server ‘centos6web’
name: -> it denotes the comments will be printed out during Playbook execution in STDOUT.

This ‘httpd.yml‘ Playbook is having three tasks each being named as below,

A) Installing HTTPD
B) Custom index.html
C) Starting and Enabling HTTPD

A -> This task uses the ‘yum’ module to install the ‘httpd’ package with latest version from repository.
B -> This task uses the ‘copy’ module to copy the ‘src’ file to ‘dest’ file.
C -> This task uses the ‘service’ module to make sure that ‘httpd’ service is started and enabled at chkconfig.

We have completed the writing of playbook named ‘httpd.yml’ for the purpose of installing and configuring ‘httpd’ in server centos6web

Now we can check the syntax of YAML file by using below command,

#ansible-playbook –syntax-check <Playbook_file_name.yml>

[root@centos7 play]# ansible-playbook --syntax-check httpd.yml

playbook: httpd.yml
[root@centos7 play]#

Please make sure the above syntax-check command is not throwing any errors. If any errors then please fix it.

Status of server ‘centos6web’, Before executing ‘httpd.yml’

[root@centos6web ~]# rpm -qa | grep -i httpd
[root@centos6web ~]# curl http://centos6web
curl: (7) couldn't connect to host
[root@centos6web ~]#

To run the Playbook, please use the below command,

# ansible-playbook <Playbook_file_name.yml>

[root@centos7 play]# ansible-playbook httpd.yml

PLAY [Installing and configuring HTTPD Web server] ***********************************************************

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

TASK [Installing HTTPD] ***************************************************************************************
changed: [centos6web]

TASK [Custom index.html] *******************************************************************************************
changed: [centos6web]

TASK [Starting and Enabling HTTPD] ********************************************************************************************
changed: [centos6web]

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

[root@centos7 play]#

Status of server ‘centos6web’, After executing ‘httpd.yml’ Playbook

[root@centos6web ~]# rpm -qa | grep -i httpd
[root@centos6web ~]# curl http://centos6web
<header><title>Hello Ansible Playbook</title></header>
Hello Ansible Playbook
[root@centos6web ~]#

So, we have installed and configured the ‘httpd’ in server ‘centos6web’ using Ansible Playbook.