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, 2019 · 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 and raw materials will be the while 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 is localhost and there is only one as user. Roles are defined to execute different tasks related to the role in reusable playbooks. (More details on and ). 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 myuser 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
Image for post
Image for post

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

Image for post
Image for post

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/
Image for post
Image for post

🐦👥💬

🗣️ 🗞️,

Image for post
Image for post

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 Creative Developers & DevOps Enthusiasts

Sign up for FAUN

By FAUN

Medium’s largest and most followed independent DevOps publication. Join thousands of aspiring developers and DevOps enthusiasts Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Nethmini Romina

Written by

📨 nethminiromina@gmail.com — Connect on: https://www.linkedin.com/in/nethminiromina/

FAUN

FAUN

The Must-Read Publication for Creative Developers & DevOps Enthusiasts. Medium’s largest DevOps publication.

Nethmini Romina

Written by

📨 nethminiromina@gmail.com — Connect on: https://www.linkedin.com/in/nethminiromina/

FAUN

FAUN

The Must-Read Publication for Creative Developers & DevOps Enthusiasts. Medium’s largest DevOps publication.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store