This is the second tutorial on Ansible. In the previous article of this series for getting started with Ansible, we said that we are the article before going to Playbook, Roles and Galaxy as they need separate guide. We suggest to read that article even if you have installed Ansible.
Get Started With Ansible Playbooks : How the Playbooks Works
Playbooks are the main components of Ansible from the aspect of deployment. In this Guide We Will Get Started With Ansible Playbooks With Very Easy to Understand Examples. As we have written before, Ansible Playbooks are YAML files which allows you to organize the configuration and management tasks. Each each of these playbooks contains a list of tasks known as play. Each playbook can be combined with other playbook. That is called Roles. Roles are reusable abstractions that contain a collection of features. Ansible has a community for users to share such kind of roles names Ansible Galaxy. Galaxy represent collections of ‘plays’, configuration policies which get applied to defined groups of hosts.
Here is the official documentation on Playbooks :
This a typical structure of the Ansible Playbooks :
+--site.yml # master YAML playbook file. Contains info about rest of the playbook
+--hosts # contains information about the hosts to be managed
+--group_vars/ # contains information like the hosts inventory file
+--roles/ # Defines how a server is supposed to perform
| +--files/ # contains files which will be deployed to hosts without modification
| | +--templates/
| | +--tasks/
| | +--handlers/
| | +--vars/ # Stores variables in a YAML file with high priority
| | +--defaults/ # Includes information about default variables used by this role
| | +--meta/ # Meta contains files that describe environment
Get Started With Ansible Playbooks : How to Write the Playbooks
YAML is a Syntax not be a programming language or script, it is done for easiness. In the previous guide, we used modules, not went to
Playbook Mode. Take that, we have a playbook named
site.yml, to execute that Playbook, we will run :
ansible-playbook -i hosts -k -K site.yml
How we will write a YAML file for making it Ansible Playbook? Ansible Playbook YAML files will starts with a list. Item in the list is a list of key/value pairs, this called
hash (or a
dictionary). So, we need to know how to write lists and dictionaries in YAML.
YAML files can begin with
--- and end with
..... All the members of a list beginning at the same indentation level starting with one
# saved as tasks/myplay.yml
- hosts: all
- shell: echo "hello world"
You can save it as
myplay.yml and run this command to debug :
ansible-playbook --syntax-check --list-tasks -i inventory.ini myplay.yml
There is no reason to think that, 100% part of Playbooks are handwritten, there is automation for it too. This guide is like teaching to work with BASIC programming language.
You can install
generator-ansible, but that is what we will not suggest to do if you are reading this type of guide for the first time.
Get Started With Ansible Playbooks : How to Execute the Playbooks
If the syntax is correct of the above
myplay.yml then, they run :
ansible-playbook myplay.yml -i inventory.ini -u root
You can run on localhost :
ansible-playbook -i "localhost," -c local myplay.yml
Difficult command. Let us make it easy by using the default inventory file
/etc/ansible/hosts/inventory.ini. Add an entry for localhost as given below:
Now, if you run :
It will work on localhost.
In each of these Playbooks, we can include another
YAML file :
- name: this is a play at the top level
- include: myplay.yml
After this basic guide, you really need to read the official documentation of Ansible on Playbooks. You should make yourself easy with the format and why the commands are written. It will take around a week to get easy. There are lot of examples on Galaxy and GitHub.
Follow the Author of this article :