Automation plays a central part in today’s information technology world. That’s why it’s essential to have reliable automation tools at your disposal. Ansible, an open-source automation tool, handles application deployment, configuration management, intra-service orchestration, and provisioning. It’s an incredibly useful tool for DevOps, since it eliminates time-consuming, repetitive tasks—freeing up teams to focus on more strategic work.
Ansible playbooks are a vital part of Ansible and the core component of every Ansible configuration. In this article, we explore all that Ansible playbooks are, how to write an Ansible playbook, its usage, playbook variables, and the Ansible playbook example.
If you’re not overly familiar with Ansible, check out this Ansible tutorial and get acquainted with the power and potential of this popular DevOps tool.
What is an Ansible Playbook?
An Ansible playbook is a file where users write Ansible code, an organized collection of scripts defining the work of a server configuration. They describe a set of steps in a general IT process or a policy for your remote systems to enforce.
Playbooks consist of one or more plays run in a particular order. A play is an ordered set of tasks run against hosts chosen from your inventory. Plays define the work to be done. Each play contains a set of hosts to configure, and a list of tasks to be executed. There are no standardized plays; an administrator must write each play.
Playbooks use YAML, a human-readable data serialization language. YAML is a recursive acronym that stands for “YAML Ain’t Markup Language.”
A Word About Ad-Hoc Commands
Ad-hoc commands automate a single task on one or more managed nodes, using the /usr/bin/ansible command-line tool. These commands are fast and easy to create, but are not reusable. They are ideal for tasks that you don’t perform very often. For instance, you can use an ad-hoc command to copy a file.
It’s necessary to master ad-hoc commands because they not only show the simplicity and versatility of Ansible, but many ad-hoc command concepts apply to playbooks.
What Are Ansible Playbooks Used For?
Ansible playbooks run multiple tasks, assign roles, and define configurations, deployment steps, and variables. If you’re using multiple servers, Ansible playbooks organize the steps between the assembled machines or servers and get them organized and running in the way the users need them to. Consider playbooks as the equivalent of instruction manuals.
Users can employ Ansible playbooks to manage deployments to and configurations of remote machines. Furthermore, playbooks can sequence a multi-tiered rollout that uses rolling updates—launching tasks either synchronously or asynchronously.
In summary, Ansible playbooks help users easily control multiple machines and have all affected units act in harmony. It is an immensely powerful resource for DevOps professionals to have under their belts.
What Kinds of Variables Do Ansible Playbooks Have?
Ansible uses variables to help users deal with the differences between systems, as no two systems are precisely alike. Variable names consist of letters, numbers, and underscores, though they should always begin with a letter. Also, variables never contain blank spaces.
You can define variables directly in playbooks by using the “vars:” command. Variables can be anything from proper nouns, ports to web servers, or a given command. For instance, you could arrange a playbook to greet people with “How’s it going, eh?” by creating a variable name “greeting,” with a value of “How’s it going, eh?” When the user executes the playbook, it displays the message on the terminals.
Variables store values, and you can create almost any kind of variable you need. There are group and host variables, inventory file variables, array variables, dictionary variables, and special variables. Special variables are built-in variables that can’t be set by the user, and Ansible always overrides them.
You can also keep variables in a separate file and, when needed, import it with the vars_files command.
Note that your playbook doesn’t have to include variables if there is no need for them. They are purely optional.
Learn how to configure network devices across Linux or Windows operating systems with our Ansible Foundation Training Course.
Some Ansible Playbook Examples
Here are a couple of examples provided by Ansible’s home website.
- hosts: webservers
serial: 5 # update 5 machines at a time
- hosts: content_servers
How to Write an Ansible Playbook?
The above examples were simple and straightforward. Ansible playbooks can potentially get quite larger and more complex. But whether you’re creating a short and sweet playbook or a sweeping epic, here is how you put one together.
Every playbook breaks down into the same standard sections:
- Playbooks begin with three hyphens. ---
The host section defines the target machines where the playbook will run. This information is based on the Ansible inventory file. The host section is, therefore, a list of devices.
The variable section is optional and includes any variables that the playbook requires. It can be as large or as small as needed, if at all.
The task section lists all tasks that the target machine must run and specifies the use of Modules. Every task gets a name, usually a small description of what the task does and is listed when the playbook runs.
Most playbooks end with three periods. …
And here are some useful pointers to remember when working with Ansible playbooks:
- Playbooks are written in the YAML format and have a .yml file extension
- Use this command to run a playbook: $ ansible-playbook <playbook.yml>
- Use this command to check the playbook for syntax errors: $ ansible-playbook <playbook.yml> --syntax-check
- Create lists. Lists define elements within playbook variables. Use the – symbol to create a list
- Include whitespaces. If you put a blank line between each task or block, it makes the playbook easier to scan
- Name your tasks. Task naming is optional, but it comes in handy. Pick names that explain what each task does
- Include the state. Although the ‘state’ parameter isn’t mandatory, you may want to use state settings such as ‘state=present’ or ‘state=absent’ for the sake of clarity
- Employ comments. Even if you name tasks and states, some instances require more information. Add a comment (starting the line with an #) to help users understand (or to remind yourself!), what a task or play is doing, how it’s doing it, and why
DevOps Tool Resources
DevOps is a commonly used software development process that has spawned many useful tools. Of course, we’ve already touched upon Ansible, but there are other equally valuable resources out there. Simplilearn has gathered a collection of these resources for you to benefit from.
For starters, check out this Ansible vs. Kubernetes article that compares these two tools and how they stack up for DevOps professionals. Ansible vs. Puppet compares and contrasts these two worthy configuration management (CM) tools, and finally, Ansible vs. Chef breaks down the differences between these two useful resources.
If you want to expand your DevOps knowledge even further after seeing the available variety of tools, check out the Post Graduate Program in DevOps. Designed in collaboration with CalTech CTME, this program sharpens your expertise in Chef, Ansible, and Puppet while helping you master the means of improving your team’s DevOps capabilities. It’s an “all in one” program that will boost your DevOps understanding and help you be a better team leader, while it familiarizes you with the most popular tools available today.
How Would You Like to Be a DevOps Engineer?
DevOps engineers are responsible for managing the IT infrastructure that handles software development. They communicate with development teams, assist in coding and scripting, and deliver the results of testing, and troubleshooting, and user feedback.
If this sounds like the type of career you’d like to get into, Simplilearn’s DevOps Engineer Master’s program will prepare you for a DevOps career that offers excitement, challenges, and the chance to work with IT teams. You will acquire expertise in the principles of continuous development and deployment, automation of configuration management, inter-team collaboration, and IT service agility, using DevOps tools such as Git, Docker, Jenkins, and more.
The program consists of seven courses, introducing you to over 40 in-demand skills and more than 15 DevOps-related tools. According to Payscale, DevOps Engineers can earn an annual average of USD 94,660, with a high mark of around USD 136,000.
If you’re not ready to tackle a massive DevOps Engineer program but would like to get acquainted with Ansible, check out Simplilearn’s Ansible Foundation Training Course. The course offers you four and a half hours of self-paced learning. It is ideal for beginners who want to understand Ansible 2.0 installation better, create their own playbooks, manage an entire cloud region, and configure network devices across Linux or Windows operating systems.
Whether you want to take those first steps in a DevOps Engineer career or are only interested in becoming proficient with Ansible, Simplilearn has everything you need to meet your goals. Check them out today!