Playbooks are nothing but a Ansible’s configuration management scripts, it can be used to manage configurations of and deployments to remote machines. Playbooks contain set of policies that you want your remote systems to enforce, or a set of steps in a general IT process.
Playbooks are written and developed in a simple text language, syntax that we use in playbooks are totally different from normal commands that we used to test in the previous tutorial.
This guide is the second part of Installing Ansible on CentOS 7 / Ubuntu 14.04 / Fedora 22.
For our first example, create a playbook called “httpd.yml”, we will configure a host to run an apache web server. Each playbook is composed of one or more “plays” in a list. For each play in playbook, you get to choose which machines in your infrastructure to target and what remote user to complete the tasks.
# vi httpd.yml --- - hosts: web-servers remote_user: raj become: yes become_method: su tasks: - name: Installing Latest version of Apache yum: pkg=httpd state=latest - name: Copying the demo file template: src=/etc/ansible/index.html dest=/var/www/html owner=apache group=apache mode=0644 - name: (Enable it on System Boot) service: name=httpd enabled=yes notify: - start apache handlers: - name: start apache service: name=httpd state=started
Createdemo html file (/etc/ansible/index.html), this will be placed in the default DocumentRoot of remote hosts, as part of our tutorial.
# vi /etc/ansible/index.html <html> <head> <title>Apache is installed by Ansible</title> </head> <body> <h1>Apache is installed by Ansible</h1> <p>Now, Apache is managed through Ansible</p> </body> </html>
Now, we will go through each sections of playbook (httpd.yml) file to understand what these are means.
File starts with
All YAML files should begin with (Three dashes) “—“, this indicates the start of a document. YAML is very sensitive to space, and uses that to group different pieces of information together. Spaces must be consistent across your file to be read correctly. Items at the same level of indentation are considered sibling elements.
--- - hosts: web-servers remote_user: raj
The hosts line is a list of one or more groups or host patterns, separated by colons, along with host you can mention remote user account.
--- - hosts: web-servers remote_user: raj become: yes become_method: su
You must become root user to install any packages on system, To do that, you can use privilege escalation methods, like su. When ever you use this kind of privilege escalation methods, you have to run ansible-playbook with “–ask-become-pass” argument.
Now, we have set of tasks.
tasks: - name: Installing Latest version of Apache yum: pkg=httpd state=latest - name: Copying the demo file template: src=/etc/ansible/index.html dest=/var/www/html owner=apache group=apache mode=0644 - name: (Enable it on System Boot) service: name=httpd enabled=yes
Each play contains a list of tasks, those are executed in order, one at a time, against all machines matched by the host pattern, before moving on to the next task.
When you are running the playbook, it runs top to bottom, hosts with failed tasks are taken out of the rotation for the entire playbook.
Every task should have a name, which is included in the output from running the playbook. This is output for humans, so it is nice to have reasonably good descriptions of each task step.
First task will install latest version of apache, second will copy the demo html (/etc/ansible/index.html) to /var/www/html directory of remote hosts, third one will enable auto-start of apache service during system boot.
These ‘notify’ actions are triggered at the end of each block of tasks in a playbook, and will only be triggered once even if notified by multiple different tasks.
notify: - start apache
The “notify” item contains an item called “start apache”.This is a reference to a handler, which can perform certain functions when it is called from within a task. We will define the “start apache” handler below.
handlers: - name: start apache service: name=httpd state=started
Handlers are lists of tasks, not really any different from regular tasks. but they only run when they have been told by a task that changes have occurred on the client system.
In our case, we have a handler that starts apache service after the package is installed. This is because of the notifier notified handler about changes to the system, meaning that apache packages had to be installed and along with that demo file had to be copied to DocumentRoot.
Once you have a playbook ready, you can run it using below command.
ansible-playbook httpd.yml -f 1 --ask-become-pass
ansible-playbook – Command to run ansible playbooks.
httpd.yml – YAML file (Ansible Playbook file – that we created at start of this tutorial)
-f 1 – playbook using a parallelism level of 1
–ask-become-pass – Since we need to become root user to install packages.
By default, the above command will install apache webserver on all hosts. Since the playbook itself has information of remote hosts that it should run (“web-servers” – group that we created in last tutorial), so we do not have to specify a host to run playbook tasks.
You can also run playbook on specific host by running below command, below command runs playbook only on “192.168.12.8”
ansible-playbook -l 192.168.12.8 httpd.yml --ask-become-pass
Sample output of running playbook.
After running a playbook, open your browser and navigate to any one of the remote host mentioned in ansible inventory. In my case, URL will be http://192.168.12.8
You should get above page “Apache is installed by Ansible”, this confirms us that apache was installed by Ansible. Now, apache can be managed through ansible.
That’s All!!!, You have learnt how to create a simple playbook for automation of apache installation. You can find more information on creating playbooks here.