Writing simple Ansible Playbook

This article is based on Ansible Playbooks. If you are new with Ansible, I recommend you to have a look at my previous article (starting-with-ansible) to get an idea of the basic concepts of Ansible. So let us start with Ansible Playbooks.

Nethmini Romina
Nov 21 Β· 4 min read

Before beginning to write an Ansible Playbook I will go through a quick overview it. Basically Playbooks are used to enforces policies to remote hosts/servers according to the user needs. These are Ansible’s configuration, deployment, and orchestration language which written in YAML. So, it is easy to read and understand. Assume you are given a set of tasks to complete along with an instruction manual and raw materials. In this case, the instruction manual will be the playbook and raw materials will be the inventory while modules become the set of tasks. Here is a sample Playbook contains only one play.

- β€” β€” 
- name: β€œPlay 1”
hosts: localhost
roles:
β€” user

In this playbook there the host is localhost and there is only one role as user. Roles are defined to execute different tasks related to the role in reusable playbooks. (More details on roles and hosts). These are basic knowledge on Ansible playbook, so, let us try to create a simple playbook which runs with localhost.

Simple Ansible Playbook

Step 01 β€”

First, create a project folder and add the configuration files for the playbook.

mkdir ansible/cd ansible

Then create a new configuration file host and site.yml.

touch hosts site.yml

Now create a new directory called roles.

mkdir roles/

Step 02 β€”

Second, generate Ansible roles for the directory structure. For this task we are using a role named myuser. Redirect to the /roles folder.

cd roles/

Now execute the following command to generate the folder structure.

ansible-galaxy init myuseroutput: WARNING: Executing a script that is loading libcrypto in an unsafe way. This will fail in a future version of macOS. Set the LIBRESSL_REDIRECT_STUB_ABORT=1 in the environment to force this into an error.
- Role myuser was created successfully

You will find the following folder structure in your project folder

tree .
.
β”œβ”€β”€ hosts
β”œβ”€β”€ roles
β”‚ └── myuser
β”‚ β”œβ”€β”€ README.md
β”‚ β”œβ”€β”€ defaults
β”‚ β”‚ └── main.yml
β”‚ β”œβ”€β”€ files
β”‚ β”œβ”€β”€ handlers
β”‚ β”‚ └── main.yml
β”‚ β”œβ”€β”€ meta
β”‚ β”‚ └── main.yml
β”‚ β”œβ”€β”€ tasks
β”‚ β”‚ └── main.yml
β”‚ β”œβ”€β”€ templates
β”‚ β”œβ”€β”€ tests
β”‚ β”‚ β”œβ”€β”€ inventory
β”‚ β”‚ └── test.yml
β”‚ └── vars
β”‚ └── main.yml
└── site.yml
10 directories, 10 files

Let me talk you on a quick walkthrough around the files we created up to now.

  1. site.yaml β€” This is the playbook file. This contains the group of hosts that will be managed using the available roles.
  2. hosts β€” It is an inventory file which contains the details of the managed nodes (IP addresses)
  3. roles β€” A group of Ansible playbooks used to provision the managed nodes.

Step 03 β€”

The third part is to set up the hosts and the site.yml file. Open the hosts file and paste the following part. Then save and exit.

[localhost]
localhost

Same, open site.yml file, paste the following part, save and exit.

- β€” β€” 
- name: β€œPlay 1”
hosts: localhost
roles:
β€” myuser

Step 04 β€”

Part 04 is to set up the roles. In our case let’s setup myuser role. Open main.yml in /roles/myuser/tasks. This file is to define the tasks related to the role. The tasks are defined in several methods. For this task, we will make the role to create a folder. Paste the following code to the main.yml and save.

- name: Task 01
command: mkdir task01/

The tasks can be named as you want with the name: tag.

Step 05 β€”

The final step is to run the playbook.

ansible-playbook site.yml

If you check your directory structure you will find out the new folder task01.

So, this is all for writing a simple Ansible playbook. Let us get back with a more complex as multiple hosts in next article.

References:

  1. https://docs.ansible.com/ansible/latest/user_guide/playbooks.html
  2. https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#playbooks-intro
  3. https://www.howtoforge.com/ansible-guide-create-ansible-playbook-for-lemp-stack/

Follow us on Twitter 🐦 and Facebook πŸ‘₯ and join our Facebook Group πŸ’¬.

To join our community Slack πŸ—£οΈ and read our weekly Faun topics πŸ—žοΈ, click here⬇

If this post was helpful, please click the clap πŸ‘ button below a few times to show your support for the author! ⬇

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts

Nethmini Romina

Written by

πŸ‘©πŸ»β€πŸ’» Software Engineer at Chakray Consultancy - Sri Lanka πŸ‘©πŸ»β€πŸŽ“ Undergraduate at NSBM Green University πŸ“¨ nethminiromina@gmail.com

Faun

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium β€” and support writers while you’re at it. Just $5/month. Upgrade