So what is an Ansible playbook? In this article we're going to define what an Ansible playbook is. We're going to differentiate the playbook from the play. We're gonna look at how Ansible documentation describes playbooks, and then we're gonna look at how a basic playbook is structured.
Ansible playbook is an ordered list of tasks saved so that you can run those tasks in that order repeatedly. It's a single YAML file containing one or more plays. So what's an Ansible play? It defines a set of tasks to be run against the host. The set of hosts or the group is defined at the play level. And the last thing to note is that the playbooks are very flexible because of their use of Ansible modules that are modular.
An Ansible playbook is an organized unit of scripts that defines work for device configuration managed by the automation tool. Ansible, the playbook, is the core component of any Ansible configuration. An Ansible playbook contains one or multiple plays, each of which define the work to be done for a configuration on a managed server or in our case, network devices. Ansible plays are written in YAML. Every play is created by an administrator with environment-specific parameters for the target machines. There are no standard plays. Ansible plays are flexible to the modules which pertain to various aspects of the managed devices.
And so here is a basic playbook example.It's one that I used earlier in the series when I was taking us through some labs. And then underneath the three dashes, where you have the dash space name, that denotes a play. So every play has a name. Also, if we wanted to add a second play to this playbook, we would just put another dash name down underneath the debug line at the bottom. And then underneath the name of the play, we have hosts and gather facts. Those are both parameters. Then we have a couple of spaces and then it has tasks. Also, each play can have more tasks and much in the same way we could add a second task. And to do so, we would just add another dash space name just like we do right here.
And then underneath the task section, much in the same way, we also name the task itself. You can have multiple tasks in this section here. We only have one. If we wanted to add a second one, we would use the same dash space name line underneath the debug here and give it another name. And then we call the raw module, which runs the
show cdp neighbors command on the devices. And then it prints the output to the register variable. For the most part, that's the basic structure. The playbook starts with the three dashes at the top, which contains plays. The plays have parameters and also define the set of hosts that we're going to be running it against. And the play also has one or more tasks in it.
All right, so in this article we defined what an Ansible playbook is. We differentiated the play from the playbook. We looked at how Ansible documentation describes playbooks. And we looked at how a basic playbook is structured.