Ansible — Intermediate — Part 1

Raghavendra Hiremath
4 min readJul 18, 2020

Push yourself, because no one else is going to do it for you.

Today, I bring you a new free and open source automation tool by Red Hat called Ansible.

Source: https://upload.wikimedia.org/wikipedia/commons/2/24/Ansible_logo.svg

I kinda love this symbol; because any gamer would love it :-), without wasting time. Let’s dive into the concepts and practicals.

Wait!!!

What can I do with Ansible? -> Manage and control multiple servers from single machine, it is very easy to learn and script it. Good!

Why do I need it? -> Oh! I’m doing multiple updates and repetitive tasks. I can leverage Ansible to perform those tasks from centralized location comfortably . Cool!

Ansible is the best in the beast : There are other alternatives such as Chef, Puppet, and Salt.

Ansible uses YAML (Yet Another Markup Language) in its configuration and for automation jobs. YAML uses SSH protocol (22) to communicate with remote servers.

Basic Terminology:

An Inventory file: It is a file or a text file that contains list of IP address or Node names.

A Playbook: it is a configuration management scripts that tells how the tasks are going to be executed.

A Modules: Modules are unit of code defined in playbook that perform specific tasks on remote hosts.

A Play: It is an instruction which performs action on a remote servers, collection of instructions constitute a playbook.

We will discuss about Variables, Facts, Configuration Files in later parts.

Who doesn’t love Practicals ? Let’s dive in….

Learn to do from scratch. I’m using AWS, RHEL version 8 and Git Bash/Putty (Your choice)

  1. Create 5 instances:
    Control Node (Master): 1 instance.
    Managed Node (Slave): 4 instance.
Micro instance, 50 G Storage

2. Connect to Control Node using it’s public IP address

Login page

3. Update instance by running sudo dnf update (DNF: New version of YUM)

4. Install Python 2 or 3. I’m installing Python 3 on control node

5. Install Ansible using PIP3 as a user.

6. Check the Ansible version — I’m on 2.9.10 the latest version.

7. Verify SSH is running

8. Now, let us create a default inventory file for Ansible, it contains remote IP address or hosts name. Ansible uses this file to communicate with host. This default location Interview question :-)

Default location: /etc/ansible/hosts

sudo mkdir /etc/ansible && cd /etc/ansible
touch hosts

Adding private IP address of Managed Nodes. [NOTE: DO NOT USE PUBLIC IP]

Installation Part is completed

CONFIGURING PASSWORDLESS SSH CONNECTION

We have to generate rsa public and private key on control node, and then copy the public ssh content from control node and then paste it in Managed Nodes (/home/ec2-user/.ssh/authorized_keys)

  1. Generate SSH Keygen

2. Copy the public content from .pub

3. Navigate to Managed Nodes individually and then append the public content into authorized_keys.

4. Change permission on authorized_keys: chmod 600 authorized_keys

Repeat 3 and 4 on rest of the Managed Nodes. Once completed! Try ssh into all Managed Nodes, it should allow you passwordless login.

AWESOME! YOU HAVE SETUP ANSIBLE FROM SCRATCH MANUALLY — Note: Nodes are not in domain, I will update the content in future parts.

Now, it’s time to run some Ansible command.

I’m trying to ping my Managed nodes from control node.

It worked :-) -m is a module and ping is a service from configuration management.

2. I have grouped servers in hosts file:

3. Let’s ping again, this time I will ping web2. WORKED!

4. Let us find the disk space on each server under web2

-a allows you to run linux commands on Managed nodes.

I will update Part 2 as we continue automation journey…………. Part 3 will be on Windows — Interesting. Stay Safe.

--

--

Raghavendra Hiremath

WE BELIEVE IN FREE AND OPEN SOURCE. My mission is to constantly learn and share the knowledge I gain so that you can use it to empower you.