How to run Ansible playbook? A quick guide
Are you looking at how to run Ansible playbook? We can help you with it.
An Ansible playbook can manage configurations of a remote machine and at the same time can interact with monitoring servers.
At Bobcares, we get requests setup Ansible playbooks, as a part of our Server Management Services.
Today, let’s see how our Support Engineers run an Ansible playbook.
What is Ansible playbook?
It’s now time to get an idea on Ansible Playbook.
Playbooks are Ansible’s configuration that describes the policy on how a remote system should work.
Ansible playbooks offer increased flexibility. A playbook uses a YAML format that is more specific about the indentation.
Basically, a playbook is a collection of tasks. We can specify tasks to multiple hosts in one playbook.
Roles can make the playbook a structured one. Moreover, it makes a playbook less complex by breaking it into multiple files.
How to run an Ansible Playbook?
Let’s see how our Support Engineers create a simple playbook and run it. Here it performs the task of installing a package in a remote host.
Initially, to manage a host we need to add it in the inventory file. The default inventory file is /etc/ansible/hosts. Hence we add the host IP in this file.
Then we need to create a playbook. We create it using the name playbook.
In this file, we need to specify the name of the task, host, tasks, etc.
For instance, the YAML code for installing a curl package is,
--- - hosts: all tasks: - name: install curl apt: pkg=curl update_cache=yes
Finally, we save the file. Then run the command to execute the playbook
The output appears as,
A common error while running an Ansible playbook
Ansible playbook is a YAML file. It is very much specific about white spaces and indentations. Hence running Ansible playbook often throw errors. One of the most common errors seen while executing a playbook is the syntax error.
A typical error message appears as,
ERROR! Syntax Error while loading YAML. mapping values are not allowed in this context
In most of the cases, the error message may show the offending line. So we check the specified line and correct the syntax.
The error can also be due to extra white spaces or missing symbols like ‘-‘ before a task.
[Need assistance in executing an Ansible playbook? – We can help you.]
So far we saw how to run an Ansible playbook. As Ansible playbook is a YAML file, we saw how our Support Engineers check the file to avoid errors while executing it.