Ansible for an absolute beginner — part 3

vikas yadav
DevOps Dudes
Published in
3 min readApr 7, 2019

--

Making automation easy!

Welcome to part 3 of this series!

If you have not read part 1 and part 2, I would suggest you do it now. I explain some key concepts and set up our lab environment.

In this part, we will write our first ansible playbook.

Firstly, we need to set up our inventory file, an inventory file is a list of all managed hosts in your environment. It is called hosts By default, this is located in /etc/ansible

user@computer:/etc/ansible$ ls
ansible.cfg hosts roles
user@computer:/etc/ansible$ cat hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups

Now, if you open this file it has a lot of good information about how you can configure this file. Basically, all the servers in your infrastructure belong to a group, for example, let's say you have two environments test and prod. In this case, you will define three groups as follows

  1. test: this group will consist of all test servers in your environment
  2. prod: this group will consist of all production servers in your environment
  3. all: this group will have all servers

Here is the code you need to add in your hosts' file(modify as per your environment)

[dev]192.168.56.20[prod]192.168.56.10[all]192.168.56.10192.168.56.20

After you have saved the file, we can now run our first command against our lab, considering you have followed the setup in part 2 open a new file and name it ping.yaml; paste the following code in this file

---
- hosts: all
tasks:
- name: Test Connection
ping:
...
  • First three “- ”dashes represent the start of a YAML file and the last three “.” dots represent the end of a YAML.
  • hosts parameter is used to define the group or individual host that you want to run this playbook against.
  • tasks is used to define the task you need to execute in the next few lines
  • name specifies some explanation about the task for example, in this task we are testing our connection to remote servers, therefore, I have named it Test connection
  • ping calls the ansible ping module, this is not your ICMP ping but ansible ping which is used to test a) connectivity to a remote machine(s) and b) the presence of a configurable python.

We can run this command by typing in the following command on the terminal

ansible-playbook /path_to_playbook/ping.yamlPLAY [all] *********************************************************************TASK [setup] *******************************************************************ok: [192.168.56.10]ok: [192.168.56.20]TASK [Test Connection] ********************************************************************ok: [192.168.56.10]ok: [192.168.56.20]PLAY RECAP *********************************************************************192.168.56.20 : ok=2 changed=0 unreachable=0 failed=0192.168.56.20 : ok=2 changed=0 unreachable=0 failed=0

So, there it is, you have run your first playbook!

To know more about YAML check my story Write your first YAML.

I have a youtube channel where I upload high-quality IT-related content for free. Please feel free to have a look at my other free courses and subscribe to my channel.

Here is a link to my free 3-hour Ansible masterclass on youtube.

If you wanna learn advanced concepts of Ansible such as different types of variables and how to use them, setting your own event handling system in Ansible, etc, here is a heavily discounted coupon for my course on Udemy “Ansible for an absolute beginner”.

--

--

vikas yadav
DevOps Dudes

IT engineer with 14 years of experience in IT with recent experience in Solution design, Big data, and log analytics.