Ansible: Basic concepts (playbooks, modules, collections)

Let's look at playbooks, modules, and collections in Ansible. These are basic concepts, but you must know this to get started with Ansible. This guide will describe their roles in the automation process.

Playbooks

A playbook is an essential basic concept of the Ansible architecture. Moreover, playbooks lie at the core of automation.
The chances are that you will spend writing playbooks. A Playbook is a script containing the automation tasks, their order, and behavior.

The playbook is a text file with the extension .yml or .yaml written in the YAML language. The YAML structure is straightforward and clean compared to languages like Python. But, a Python script written in the procedural style may look similar.

Playbook structure

All playbooks should start with three dashes to state the beginning of the document:

---

But, it is a "cosmetic" need that does not affect the execution of a playbook.

It is a good habit to comment on your playbook—, which makes life much easier when making changes to your automation flow.

# Comments start with a hash similarly to Python or Ruby syntax.

You can declare Strings with or without quotes. But, you do need quotes in some cases (in a Variable situation, for example {{"Sample string"}}).

You declare lists in the following formats:

[Item one, item two, item three]

A more common way would be to use hyphens:

	- Item one
	- Item two
	- item three

Dictionaries are key-value pairs that you can define in a couple of formats as well:

{name: John, age: 25, country: USA}

An alternative syntax would be:

name: John
age: 25
country: USA

Every playbook contains one or more plays that usually start with a name. Plays, in turn, consist of tasks.

Please, refer to this playbook that demonstrates the structure:

		 	 	 				
					
- name: Configure server - Play One 
  hosts: servers
  tasks:								 				
     - name: install nginx
       apt: name=nginx update_cache=yes					 	
     - name: copy nginx config file
       copy: src=files/nginx.conf dest=/etc/ 			 		
- name: Configure server - Play Two 
  hosts: servers
  tasks:								 				
     - name: install nginx
       apt: name=nginx update_cache=yes					 	
     - name: copy nginx config file
       copy: src=files/nginx.conf dest=/etc/ 			 		

Usually, hosts are defined in a hosts file. However, simple playbooks may contain the target hosts in the body of a playbook.

You can run a playbook using the ansible-playbook command:

ansible-playbook auto-deployment.yml

Modules

You can use Ansible to execute various shell commands on your managed node. But what makes Ansible powerful is the collection of modules that ships with it. A module is a script that manages a specific operation. For example, you use modules to perform tasks such as installing a package. Speaking of the modules for Linux OS, these are a few examples to give you a general idea:

  • apt - Install or remove packages by using the apt package manager
  • copy - Copies a file from the local machine to the hosts
  • file - Sets the attribute of a file, symlink, or directory
  • service - Starts, stops, or restarts a standard Linux service

Here is an example of how we can use those modules:

tasks:
  - name: Install nginx
    apt: name=nginx update_cache=yes cache_valid_time=3600
  - name: create directories for ssl certificates 
    file: path=/etc/nginx/ssl state=directory
  - name: restart nginx
    service: name=nginx state=restarted

Ansible has an ansible-doc command-line tool, which shows documentation for the modules. Think of it as a Linux man tool for Ansible modules. For example, to show the documentation for the service module, run this:

ansible-doc service

There are many Ansible built-in modules. It is also possible to write your module for Ansible using Python or Ruby.

Collections and Ansible Galaxy

Although standard modules could cover most of your tasks, some features and cases are unique or vendor-specific. In this case, you might want to investigate a free gallery of collections that are available through Ansible Galaxy. It helps one to save a considerable amount of time.

An excellent example of collections usage is Network Automation when some of the inputs and outputs vary depending on the vendor (Cisco, Juniper, Aruba, Arista, etc.)

To browse through the long list of available collections, please follow this link: https://docs.ansible.com/ansible/latest/collections/index.html#list-of-collections

Wrapping up Ansible basic concepts

We explored essential components of Playbooks such as tasks, plays, dictionaries, lists, variables, and hosts. Also, we investigated modules and collections available through Ansible Galaxy.

You may also be interested in



About the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee



Leave a Reply

Your email address will not be published. Required fields are marked *