As per the definition from Ansible, Playbooks offer a repeatable, re-usable, simple configuration management and multi-machine deployment system, one that is well suited to deploying complex applications.
Simply put, Playbooks are the files where Ansible code is written. Playbook can be as simple as running a ping command on different servers to check if all the servers are available on the network. Or it can be as complex as deploying hundreds of VMs in a Public and Private Cloud infrastructure, provisioning storage to all VMs, Setup network configuration on Private VMs, etc.
If you need to execute a task with Ansible more than once, write a playbook and put it under source control. Then you can use the playbook to push out new configuration or confirm the configuration of remote systems. Ansible playbooks are used to automate IT infrastructure, networks, security systems, and developer personas (such as Git and Red Hat CodeReady Studio).
Playbook Syntax
Playbooks are written in YAML format. It is a single YAML file which contain a number of YAML structures called plays. A play defines a set of activities to be run on a single or group of hosts. Each play executes part of the overall goal of the playbook, running one or more tasks. Each task calls an Ansible module.
If you look at the below playbook example, you will see different tags. Now let's go through these tags and see what their functions are -
Name
This tag specifies the name of the Ansible playbook. You can give any logical name to the playbook.
Hosts
This tag specifies the lists of hosts or host group against you're running the task. It tells Ansible on which hosts to run the listed tasks and hence it's a mandatory tag. The tasks can be run on the same machine or on a remote machine.
Var
Var tag lets you define the variables which you can use in your playbook.
Tasks
A playbook should contain tasks or a list of tasks to be executed. Tasks are a list of actions needed to perform. A tasks field contains the name of the task. This serves as the user's help text. It is not required, however it aids in the debugging of the playbook. Each task has an internal link to a module, which is a piece of code.
Playbook execution
Lets create a file called hostname.yml to print the hostname of the server. To do so create a file with the below contents
To run this playbook, you need to execute the 'ansible-playbook' command and specify the name of the Ansible playbook. In this case it would be
This playbook will run on a localhost and it would return an output like below and print the hostname.
That's it for this post.
For more information on the playbooks, please refer to the official document from the Ansible. To optimize the Ansible user, please refer to some of the tips and tricks.
Thank you for reading!
*** Explore | Share | Grow ***
Comments