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 :
---
1 | http://docs.ansible.com/ansible/playbooks.html |
This a typical structure of the Ansible Playbooks :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | +--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 | | | +--group1 | +--group2 | +--host_vars/ | | | +--hostname1 | +--hostname2 +--roles/ # Defines how a server is supposed to perform | | | +--common/ | | | +--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 | | | +--webservers/ | … | … | +--applicationservers/ | … | … | +--databaseservers/ | … | … |
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 :
1 | 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 -
:
1 2 3 4 5 6 7 8 | --- # saved as tasks/myplay.yml --- - hosts: all tasks: - shell: echo "hello world" |
You can save it as myplay.yml
and run this command to debug :
1 | 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 :
1 | ansible-playbook myplay.yml -i inventory.ini -u root |
You can run on localhost :
1 | 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:
1 | localhost ansible_connection=local |
Now, if you run :
1 | ansible-playbook myplay.yml |
It will work on localhost.
In each of these Playbooks, we can include another YAML
file :
1 2 3 4 5 | - name: this is a play at the top level hosts: all remote_user: root tasks: - 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.
Tagged With ansible windows playbook , windows ansible playbooks , common ansible playbooks for windows , ansible_keep_files=1 windows , ansible-playbook windows , ansible-playbook for windows , ANSIBLE WINDOWS PLAYBOOKS , ansible windows playbook to check for symantec anti virus , ansible playbooks , ansible playbook windows