Ansible Playbooks
This How To will go over the basics of creating an Ansible playbook. Playbooks can be simple or as complicated as you make them. On this site I will just be going over the basics. The examples i use will go over two different Network Modules. I will use these modules as I show you how to format and use the correct syntax for creating playbooks. The two playbooks I create will use the ios_command and the ios_config network modules. In this How To the router I will use is VIRL Routers. I am also running an Ubuntu operating system. The network emulation application I am using is GNS3.
Instructions
These instructions will show you how to run a playbook to retrieve and save information to a text document. And also how to configure a part of the iOS router. ***NOTE*** these playbooks can be ran against iOS routers and switches. Be sure to check the Ansible Project documentation for version compatibility on your host network devices.
First let’s create our YAML file. Playbooks are always saved as YAML files, and as such follow it’s syntax rules. In VSCODE(ubuntu) it might do you well to download the YAML file extension for VSCODE.
Now let’s break this code down line by line. Again use the reference in the related links table down below for more information. YAML format is all about it’s indentations so be sure to take note of how many spaces you are putting.
Line 6: The tasks variable is the start of any tasks you want your playbook to perform.
Line 7: The name variable is simply just what you want to name the play you’re about to execute.
Line 8: The ios_command variable is the network module you want to run in this play.
Line 9: the commands variable is the command you want ran on the target host.
line 10: The register variable allows you to input a name and whatever output is returned will be assigned to that named variable.
Line 12: See section 6.
Line 18: See section 5.
Line 19: See section 6.
Line 20. The ios_config variable is the network module you want to run in this play.
Line 21: The lines variable is the commands that will be executed under global configuration mode on the router.
Now that we have dissected this code let’s run our play and look at our terminal output.
Run your playbook using the following command.
Here is the output we get. Note the ok=3 and the changed=2 output.
In the first image we run our playbook. In the second image we see it’s output. The ok=3 means our playbook ran three plays that were successful. The changed=2 means that there were changes made either to our target machine or to our host. In this case the two changes we’re the creation of the text document (as seen in the third image). And the second change was that the router got configured with a ntp server.
That’s it for your first playbook!
Related articles
Last updated