Ansible: Zero to Hero

CloudDeepDive
6 min readApr 1, 2023
Ansible

Introduction:

Ansible is an open-source IT automation tool that can help you automate repetitive tasks and simplify complex configurations. With Ansible, you can automate everything from application deployment to network configuration to container orchestration. It is agentless, meaning you don’t need to install any software on the hosts you want to manage, which makes it easy to use and maintain. Ansible uses a simple language called YAML for defining tasks, making it easy to learn and use.

In this blog, we will explore Ansible and its core concepts, including architecture, modules, playbooks, and inventories. We will also look at some examples to demonstrate how to use Ansible to automate common IT tasks.

  • Ansible Architecture:

Ansible follows a client-server architecture where the server (also called the control machine) manages the clients (also called nodes) using SSH or WinRM (Windows Remote Management) protocols. The server contains a set of modules that define the tasks to be performed on the nodes. When the server receives a command, it compiles the module and sends it to the nodes. The nodes then execute the module and report back to the server with the results.

Ansible: Architecture and components
  • Ansible Modules:

Modules are the building blocks of Ansible, and they are used to execute tasks on remote systems. Ansible provides a wide range of modules that can be used to perform various tasks, such as managing files, installing packages, and configuring services.

Here's an example of how to use the file module to create a directory on a remote host:

Example:

- name: Create a directory
hosts: webserver
tasks:
- name: Create a directory
file:
path: /var/www/myapp
state: directory

In this example, we have a playbook with a single task that uses the file module to create a directory on a remote host. The path parameter specifies the path of the directory we want to create, and the state parameter tells Ansible to create a directory if it doesn’t exist.

  • Ansible Playbooks:

Playbooks are YAML files that describe the tasks that Ansible should perform. Playbooks consist of one or more plays, and each play consists of one or more tasks. Playbooks are used to automate complex tasks such as application deployment, infrastructure management, and configuration management.

Here's an example of a playbook that installs Apache on a remote host:

Example:

- name: Install Apache
hosts: webserver
become: true
tasks:
- name: Install Apache
apt:
name: apache2
state: present

In this example, we have a playbook with a single play that installs Apache on a remote host. The become parameter tells Ansible to become root before executing the tasks, which is required to install packages on most systems. The apt module is used to install the apache2 package, and the state parameter tells Ansible to install the package if it is not already installed.

  • Ansible Inventories:

Inventories are lists of hosts that Ansible should manage. Inventories can be static, where the list of hosts is hardcoded in a file, or dynamic, where Ansible discovers the hosts to manage dynamically.

Here's an example of a static inventory file:

[webservers]
webserver1 ansible_host=192.168.1.10
webserver2 ansible_host=192.168.1.11

[databases]
dbserver1 ansible_host=192.168.1.20
dbserver2 ansible_host=192.168.1.21

In this example, we have defined two groups of hosts, webservers and databases, with four hosts in total. Each host is identified by a hostname and an IP address.

  • Ansible Commands:

Ansible provides a set of CLI commands that can be used to manage the system. Here are some commonly used commands:

  • ansible: Executes commands on the target hosts.
  • ansible-playbook: Executes Playbooks on the target hosts.
  • ansible-galaxy: Manages Ansible roles from the Galaxy repository.

Here is a step-by-step guide on how to install and set up Ansible on an EC2 instance :

Prerequisites:
1) An EC2 instance running Amazon Linux or a compatible operating system.
2) A user with sudo privileges.

Step 1: Connect to the EC2 instance
Connect to the EC2 instance using an SSH client such as PuTTY or the built-in SSH client on Linux/Mac. Make sure you are logged in as a user with sudo privileges.

Step 2: Update the operating system
Run the following command to update the operating system:

sudo yum update -y

Step 3: Install Ansible
Note: If you are trying for Amazon linux2 then you need to install the EPEL repository by running the following command:

sudo amazon-linux-extras install epel -y

Then run the following command to install Ansible:

sudo yum install ansible -y

Step 4: Verify the installation
Run the following command to verify that Ansible is installed:

ansible --version

The output should look like this:

Step 5: Configure Ansible
By default, Ansible looks for configuration files in the /etc/ansible directory. You can modify the configuration files to suit your needs. For example, you can specify the IP address or hostname of the target servers, the SSH user, and the SSH key.

sudo vi /etc/ansible/hosts

Add the IP addresses or hostnames of the target servers to the hosts file. For example:

[webservers]
10.0.0.1
10.0.0.2

[database]
10.0.0.3

Save the file and exit.

Step 6: Test Ansible
Run the following command to test Ansible:

ansible all -m ping

The output should look like this:

10.0.0.1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.0.0.2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.0.0.3 | SUCCESS => {
"changed": false,
"ping": "pong"
}

This means that Ansible can connect to the target servers and execute commands.

Step 7: Create a playbook file
Create a new file called playbook.yml using the following command:

sudo vi /home/ec2-user/playbook.yml

Step 8: Add tasks to the playbook
In this example, we’ll create a user account, install a package, and add a line to a file.

---
- name: Configure server
hosts: webservers
become: true
tasks:
- name: Create user account
user:
name: john
password: "{{ 'secretpassword' | password_hash('sha512') }}"
comment: John Doe
shell: /bin/bash
- name: Install package
yum:
name: httpd
state: present
- name: Add line to file
lineinfile:
path: /etc/httpd/conf/httpd.conf
line: Listen 8080

Save the file and exit.

Step 9: Run the playbook
Run the following command to execute the playbook:

ansible-playbook /home/ec2-user/playbook.yml

Ansible will connect to the target servers and execute the tasks defined in the playbook.

Hooray

That’s it! You have successfully created and executed a simple playbook using Ansible. You can now use Ansible to automate more complex tasks on your servers.

Summary:

In conclusion, Ansible is a powerful and flexible automation tool that allows IT teams to automate the management of their infrastructure. With Ansible, you can easily provision new servers, deploy applications, manage users and groups, and automate almost any IT task.

Ansible’s simple syntax and vast library of modules make it easy to get started, while its scalability and advanced features allow it to meet the needs of even the largest and most complex environments. Whether you’re managing a small set of servers or a massive cloud infrastructure, Ansible can help you automate your IT workflows, increase efficiency, and reduce errors.

With its ease of use and comprehensive documentation, Ansible is an excellent choice for any IT team looking to automate their operations.

Hope! you have found this blog informative & useful and if so please 👏, share and also subscribe to our “CloudDeepDive” space for more wonderful content.

Next Blog: Stay tuned

--

--

CloudDeepDive

Hey Folks! Let's Deep Dive the clouds 🌨️ with me. In this journey we will discover hidden treasures of cloud together. #Cloud #DevOps #CloudComputing