Ansible for Beginners: Playbook

 

Hello everyone. In my last post I showed you how to install and configure ansible and run ad-hoc command. In this post I will show you how you can create and run ansible playbook. If you miss the first part you can find it here. I will use the same lab setup describe in the first post.

Playbook

So what is a playbook in ansible? Playbook tells ansible what to do, in other word you can send commands to the remote devices through playbook. Playbooks are one of the core feature of ansible and tell ansible what to execute. You can reuse one playbook may times.

YAML

Playbooks are written in YAML/YML format.  The file extension is .yaml or .yml and it starts with – – – (3 dash). This is a requirement for YAML to interpret the file. For ansible the requirement is one per file and this 3 dash should only be present at the top of the file. Be careful when you write your playbook, because yaml is sensitive, tabs are not allowed and you should maintain the proper space while writing your playbook.

Each playbook can contain one or multiple play. In the play you can perform multiple task with the help of ansible module. Lets see what are module and tasks.

Module

The bread and butter of ansible is module. Modules are small python code that executes on the remote devices. You can control a remote device with ansible module. You can find the full module list provide by ansible here. Modules can execute directly on the remote host or through playbooks. If you are good in python you can write your own module.

tasks

Calling a module in the playbook is called a task. You cannot use a module in ansible playbook without a task. Inside task you can call your module and can execute that module against a particular or a group of devices. The important thing of tasks is its run sequentially.

Playbook Summary

 

If I now made a playbook summary, then it will be look like the above picture. Playbook (which is written in yml format) contains one or more then one play, Play contain tasks and tasks call module, Always remember tasks run sequentially.

First Playbook

Now let’s create our first playbook. We will write a small playbook which will simply create a directory called mydir in our server1 and server2 /home/labuser location. Create a directory on our node1 server.

mkdir lab1 && cd lab1

Now create a file called playbook.yml

touch playbook.yml

In that file type the below code. be careful about space and tabs are not allowed.

---
- name: PLAY-START
  hosts: nodes
  gather_facts: no
  
  vars:
    var1: 'mydir'

  tasks:
   - name: Create the Directory
     file:
       path: /home/labuser/{{ var1 }}
       state: directory

There are mainly three sections available in a playbook. (I use different colour for explaining the playbook so that the relationship between the component inside a playbook can easily be understand.)

Host declaration section: In this section you can tell ansible that this is the host or a group of hosts that I want to manage. By default the host list will be picked up by ansible from /etc/ansible/hosts file until unless you define your own host file. 

Variable declaration section (which is optional, can be declared inside task also): I’m declaring a variable called var1 and assigned a value mydir in that var1 variable and accessing that variable inside the task section by its name {{ var1 }}.

task declaration section: This is the place where the modules come in the scenario. Here I call the ansible  file module which will simply create the directory in our hosts.

Syntax check

You can check ansible syntax before running the playbook. type the below command to check for any syntax error in the playbook.yml file.

ansible-playbook playbook.yml --syntax-check

If you have any error in your scripts, then it will be shown as an output. If you have a clean script without error, then the name of the playbook will be displayed.

Dry run

In dry run mode ansible will show you the effect of the scripts in the system rather than actually run the script. Type the below command to check our playbook in dry run mode.

ansible-playbook playbook.yml --check

If everything is ok then you will get an output like below. Notice the PLAY RECAP section it shows you that there will be one change happen if you run the scripts.

PLAY [PLAY-START] ***********************************************************

TASK [Create the Directory] *************************************************
changed: [server1]
changed: [server2]

PLAY RECAP ******************************************************************
server1             : ok=1 changed=1 unreachable=0 failed=0
server2             : ok=1 changed=1 unreachable=0 failed=0

Run the playbook

Now it’s time to run our playbook. Type the below command to run the playbook.

ansible-playbook playbook.yml

Again, if things are ok then you will get an similar output like above.

  1. The first line is showing that the play is started. I name the play PLAY-START but you can give any name you like.
  2. Second in the TASK section, the task is executed means the module starts its work. During the creation of the task, it is important to choose a meaning full name like I choose Create the Directory, because during the execution time you will understand which part of your script is executing.
  3. And finally at the PLAY RECAP section ansible will show you a play recap summary that what are the change happen, which nodes are changed, etc. Like here its showing that the server1 and server2  both nodes are changed (changed=1). Run the script again and notice the PLAY RECAP section. this time it will show you changed=0 for server1 and server2, because ansible check those two hosts and found that the directory is already created so the script did nothing and show you the result.

Final tasks

As I already told you that playbook is reusable means if you want to do the same thing again you can use that playbook again. Let’s create another directory name mynewdir. open the file playbook.yml and change the var1 value from mydir to mynewdir. Save and exit from the file. Run the playbook and observe the output. If it runs without any error you will find another directory name mynewdir in your server1 and server2 nodes. That’s all for today, In my next post I will show you some more playbook example. Till then its goodbye.

Thanks for reading the post. If you enjoyed the post, please share it with your network and let me know your thoughts in the comments. 

About the Author: Imtiaz is working in a financial organization in Bangladesh and having experience in system, network and security administration. Feel free to contact with him on LinkedIn or Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *