Getting Started with Ansible

Satya Mounika Mushini
7 min readAug 10, 2023

--

Ansible is an open-source automation tool that simplifies the management of IT infrastructure and application deployment.

In simple terms, we can say it's one of the Configuration Management tools. It's a way DevOps Engineers manage the configuration of infrastructure (software/hardware/system) or servers.

Ansible is a push-mechanism model. It uses an agentless model, where you can name all the servers in the inventory file. You can just add the IP address or DNS of the servers and just have the passwordless authentication enabled.

I am sharing the knowledge that I have gained from Abhishek Veeramalla’s Zero To Hero DevOps course. I have attached the video links below as well for your reference.

Get Started:

The task is to create two Ubuntu Servers and provide password-less authentication to connect the Ansible server to Target Server. Also, we will create an Ansible playbook to install and start Nginx in an EC2 machine.

  1. Log in to the AWS account and create an Ubuntu EC2 machine.

2. Connect to the EC2 instance using SSH Client.

3. Now, we need to install Ansible. But first, we need to update the packages for whatever distribution is present for the EC2 machine. For Ubuntu, it is apt. To update packages, we need root user privileges, Hence we use sudo.

ubuntu@ip-172-31-3-97:~$ sudo apt update

4. Install Ansible

ubuntu@ip-172-31-3-97:~$ sudo apt install ansible

You can verify using the below command:

ubuntu@ip-172-31-3-97:~$ ansible --version

5. To start playing with Ansible, you need one more server (a minimum of two servers is required to practically explain that we can configure the second server using the Ansible server). Hence I have created another Ubuntu EC2 machine (eg: Target-Ubuntu)

Second Ubuntu Server

We can create any EC2 machine of your choice as Ansible only requires passwordless authentication so that it can configure anything on that server. (Ansible should be able to communicate with the server without any password)

6. To set up password-less authentication

ubuntu@ip-172-31-3-97:~$ ssh-keygen

For the path of the file and passphrase, just press enter for both.

By doing this, the public key, private key, and known_hosts files are saved in the path — /home/ubuntu/.ssh/

Key Generation

7. Now copy the id_rsa.pub key as this will be helpful for us to communicate with the server.

ubuntu@ip-172-31-3-97:~$ cat /home/ubuntu/.ssh/id_rsa.pub

The above command will provide the public key.

8. Log in to the second server i.e. Target-Ubuntu using SSH Client. After logging in, repeat step-6.

ubuntu@ip-172-31-13-142:~$ ssh-keygen

9. Do the below command to check what files have been created in the ssh folder.

ubuntu@ip-172-31-13-142:~$ ls ~/.ssh/

We can see that the authorized_keys, id_rsa and id_rsa.pub files have been created.

10. Now open the authorized_keys file and paste the public key of the first server (refer to Step 7).

ubuntu@ip-172-31-13-142:~$ vim ~/.ssh/authorized_keys

If you already see a key in the authorized_keys file, remove it and paste the first server key into this file.

11. Let's try to connect to the second server (Target-Ubuntu) from the first server (Ansible-Server)

ubuntu@ip-172-31-3-97:~$ ssh 172.31.13.142

Connection Successful and authenticated without any password!

Ansible Ad-hoc commands:

Adhoc commands are useful for executing Ansible commands. Lets see an example:

Create an inventory file and paste the Target-Ubuntu server's private IPv4 address in it.

ubuntu@ip-172-31-3-97:~$ vim inventory

If you want to create a file in the Target-Ubuntu server, run the below command in Ansible-Server. The IPv4 address will be picked up from the inventory file.

ubuntu@ip-172-31-3-97:~$ ansible -i inventory all -m "shell" -a "touch devopsclass"

-i inventory: it's for telling the location of the inventory file, if it's in default etc/hosts/ path then this line is not needed.
-m: module, here shell commands are supported by ansible, hence we provided -m module and shell to take up shell commands
-a: tells what kind of command to be executed “touch/nproc/df” are shell commands

You can refer to Ansible modules from here.

Create Playbook to install Nginx:

Create an ansible playbook namely first-playbook.yml that has tasks to install and start Nginx.

ubuntu@ip-172-31-3-97:~$ vim first-playbook.yml

In first-playbook.yml, write the tasks to install and start the Nginx.

---
- name: Install and Start Nginx
hosts: all
become: true

tasks:
- name: Install Nginx
apt:
name: nginx
state: present
- name: Start Nginx
service:
name: nginx
state: started

Playbook explanation:

— — —: This indicates that it’s a yaml file.

— name: Indicates the name /description/purpose of the script.

— indicates to start the playbook or there will be a list of playbooks and this is the first playbook.

hosts: Shows here this playbook has to run. if you mention “all”, it will execute in all servers. The hosts will be taken from the inventory file.

become: true— This will execute the playbook as the root user.

tasks: This will show the list of tasks that need to be executed.

In tasks, we provide the name of the task. Later we can provide the installation command in two ways:

shell : apt install nginx

(OR)

apt:
name: nginx
state: present

The second option would be better as it’s best to rely on the “apt” package manager as it’s provided by Ansible. If there are any changes in a shell command, it’s better to rely on an apt package manager.

Service is a module provided by Ansible. There are two ways to start the Nginx server:

shell: systemctl start nginx

(OR)

service:
name: nginx
state: started

To execute the playbook, run the below command:

ubuntu@ip-172-31-3-97:~$ ansible-playbook -i inventory first-playbook.yml

Output:

You can check in the Target-Server if the Nginx is installed or not. To check, run the below command:

ubuntu@ip-172-31-13-142:~$ sudo systemctl status nginx

Output:

To understand the playbook in a more detailed way, Add -v. V indicates verbosity, meaning debugging. More v’s indicate a more in-depth level of debugging.

ansible-playbook -vvv -i inventory first-playbook.yml

Ansible Roles:

It’s an efficient way to write ansible playbooks that will improve the efficiency to improve complex playbooks.

Roles create structured and efficient playbooks.

ubuntu@ip-172-31-3-97:~$ ansible-galaxy role init kubernetes

ansible-galaxy: This command is used to install roles and collections from various resources.

init: It initializes a new role to the directory.

A folder “kubernetes” is created. This folder has many files in it.

ubuntu@ip-172-31-3-97:~$ cd kubernetes
ubuntu@ip-172-31-3-97:~$ ls

README.md: A documentation of the task and its purpose.

defaults: It will store default variable values for your role. They can be overridden by the users of that particular role.

meta: It contains metadata of the role i.e. supported platforms, licensing information, dependencies, and author information.

tests: It contains test-related files such as inventory and test.yml that will be useful for unit testing.

vars: Contains variable definitions for your role.

handlers: It is useful for handling exceptions

tasks: This is where the main tasks for the role will be defined. The main.yml file consists of all the tasks that need to be executed.

Resources Used:

The below videos are provided by Abhishek Veeramalla

Ansible Basics

Ansible Zero to Hero

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

If you find this article helpful, Please do follow and show your support 😄

Thank-you for reading!😄

— Satya Mounika Mushini

--

--

Satya Mounika Mushini

Devops Enthusiast- Beginner level knowledge on AWS | Jenkins | Github | Terraform | Linux 😀