How to write an Ansible playbook

What is Ansible?

Ansible is an open source tool that automates the configuration and administration of systems. This ranges from simple to highly complex tasks. The tool masters modularization and requires minimum system requirements.

Why use Ansible?

Simple: Ansible uses a simple syntax written in YAML called playbooks. YAML is a human-readable data serialization language. It is extraordinarily simple. So, no special coding skills are required and even people in your IT organization, who do not know what is Ansible can likely read a playbook and understand what is happening.

Agentless:

Finally, Ansible is completely agentless. There are no agents/software or additional firewall ports that you need to install on the client systems or hosts that you want to automate. You do not have to separately set up a management infrastructure which includes managing your entire systems, network, and storage.

Powerful & Flexible:

Ansible has powerful features that can enable you to model even the most complex IT workflows. In this aspect, Ansible’s batteries included approach (This philosophy means that something is self-sufficient, comes out-of-the-box ready to use, with everything that is needed) can manage the infrastructure, networks, operating systems, and services that you are already using, as Ansible provides you with hundreds of modules to manage them

Efficient: No extra software on your servers means more resources for your applications. Also, since Ansible modules work via JSON, Ansible is extensible with modules written in a programming language you already know.

Ansible modules

Ansible introduces modules as basic building blocks for your software. Modules are small programs that do work. It mainly gets pushed to target machines. After the work is done in the target machine the module gets removed. One thing, you should keep in mind while writing a module is that they are very granular and specific. Each consists of simple specific tasks like copy a file, Start Nginx server. By default, Ansible provides a wide variety of modules. You can find it on the official website.

Ansible Playbooks

Ansible Playbooks are multiple modules grouped in a certain sequence. Given below is a sample example for an Ansible playbook with single module

- hosts: databases
  remote_user: root
   tasks:
  - name: rename table foo to bar			
   	postgresql_table:		
	table: foo			
	rename: bar			

  - name: set owner to someuser			
	postgresql_table:		
	name: foo		
	owner: someuser			

  - name: Trunate table foo		
	postgresql_tables:		
	name: foo			
	truncate: yes			

The above playbook will do the following tasks

1. Rename a PostgreSQL table

2. Set an owner for the table

3. Truncate the table

postgresql_table : Name of the module

- name : To give description about the tasks

hosts: Groups in which the tasks should be performed

remote user: Name of the user in the target machine

tasks: Modules are grouped under tasks

Below example has multiple modules in a single playbook

- hosts: webserver
  remote_user: root
   tasks:
  - name: create directory for nginx			
   	file:				
	path: /path/to/nginx/dir			
	state: bar		

  - name: Install nginx latest version
	yum:				
	name: nginx		
	state: latest			

  - name: start nginx
	service:			
	name: nginx			
	state: started			

Here the we have defined three modules for each of the tasks

file : Module for creating a directory

yum: Module for installing Nginx

service: Module for starting the service

The above playbook will do the following tasks

1. Create a directory for Nginx

2. Install Nginx using yum

3. Start the Nginx service

Another important term in playbook is variables we can pass variables for recurring values. Simply call the varibles inside a double curly braces.

Here is an example of variables in a playbook.

- hosts: databases
  remote_user: root
  vars:
    tablename: foo
    tableowner: someuser
   tasks:
  - name: rename {{tablename}} foo to bar			
   	postgresql_table:		
	table: {{tablename}}			
	rename: bar			

  - name: set owner to someuser			
	postgresql_table:		
	name: {{tablename}}		
	owner: someuser			

  - name: Trunate table 	
	postgresql_tables:		
	name: {{ tablename}}			
	truncate: yes			

The whole block defines which task should be executed from which hosts and from which user is called a play. We can have multiple plays on a playbook.

- hosts: databases
  remote_user: root
   tasks:
  - name: rename table foo to bar			
   	postgresql_table:		
	table: foo			
	rename: bar			

  - name: set owner to someuser			
	postgresql_table:		
	name: foo		
	owner: someuser			

  - name: Trunate table foo		
	postgresql_tables:		
	name: foo			
	truncate: yes

- hosts: webserver
  remote_user: root
   tasks:
  - name: create directory for nginx			
   	file:				
	path: /path/to/nginx/dir			
	state: bar		

  - name: Install nginx latest version
	yum:				
	name: nginx		
	state: latest			

  - name: start nginx
	service:			
	name: nginx			
	state: started

In above example we have a play for webserver and a play for databases. Which are completly independent. You can name each of the play with name argument.

Ansible Inventory list

Ansible Inventory list is where we are defining the target machines and groups. Ansible has hosts file which is used as the inventory list.

Given below is an example for host file.

10.100.100.100

[webservers]
10.25.0.1
102.26.13.1

[databases]
example.com
example3.com

You can either use the IP or the host name of the target machine in the hosts file.

Conclusion

Hope this article helped you the basic understanding of ansible playbooks and how playbooks works. Are you looking for cheap web hosting service providers? Then this is your last destination to end your search. We at TheStack provide premium wordpress hosting, hybrid smart server hosting, SEO dedicated server hosting and managed virtual private servers at best price. So, what are you waiting for? Get in touch with us to know more.

Related Articles