Day 55 & 56 Configuration Management with Ansible

Day 55 & 56 Configuration Management with Ansible

Day 55 & 56 of #90daysofdevops

Hey Techies! Welcome to this blog

In this blog, we are going to start with Understanding Configuration Management with Ansible.

Introduction

Ansible is a radically simple IT automation system. It handles configuration management, application deployment, cloud provisioning, ad-hoc task execution, network automation, and multi-node orchestration. Ansible makes complex changes like zero-downtime rolling updates with load balancers easy. More information on the Ansible website.

Prerequisites

  1. EC2 Instances:

    • Create three EC2 instances on Amazon AWS: one as the Ansible master and two as hosts.

  1. SSH to Master Instance:

    • Connect to the Ansible master instance using SSH.
ssh -i "your-key.pem" ubuntu@your-master-ip

Installing Ansible

  1. Install Ansible:

    • Add the Ansible repository and install Ansible.
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
  1. Verify Installation:

    • Confirm that Ansible is installed successfully.
ansible --version

Setting Up Ansible Configuration

  1. Transfer PEM File:

    • Copy the PEM file from your local machine to the master instance.
scp -i "your-key.pem" your-key.pem ubuntu@your-master-ip:/home/ubuntu/keys
  1. Edit Ansible Hosts File:

    • Open the Ansible hosts file for editing.
sudo vim /etc/ansible/hosts

  1. Configure Hosts:

    • Add the following lines to the hosts file to define the hosts in a group named 'servers.'
[servers]
host_1 ansible_host=your-host-1-ip
host_2 ansible_host=your-host-2-ip

[all:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/ubuntu/keys/your-key.pem
ansible_python_interpreter=/usr/bin/python3
  1. Change PEM File Permissions:

    • Restrict permissions on the PEM file.
chmod 400 your-key.pem
  1. Ping Hosts:

    • Verify connectivity by pinging the hosts in the 'servers' group.
ansible -m ping servers
  • The ping should be successful.

To check host or inventory list use this command

ansible-inventory --list

Ansible Ad-hoc Commands

Ad-hoc commands are one of the simplest ways of using Ansible. These are used when you want to issue some commands on a server or bunch of servers. The ad-hoc commands are not stored for future use, but it represents a fast way to interact with the desired servers.

The Ansible ad-hoc command uses the /usr/bin/ansible command-line tool to automate a single task on one or more managed nodes. The Ad-hoc commands are quick and easy, but they are not re-usable. The Ad-hoc commands demonstrate the simplicity and power of Ansible.

Syntax

ansible <hosts> [-m <module_name>] -a <"arguments"> -u <username> [--become]

Ping Multiple Servers

  1. Ad-hoc Ping Command:

    • Use the following Ansible ad-hoc command to ping three servers from the inventory file.
ansible -m ping -i /etc/ansible/hosts host_1 host_2 another_host

Check Uptime

  1. Ad-hoc Uptime Command:

    • Check the uptime of the servers using the ad-hoc command.
ansible -m command -a "uptime" -i /etc/ansible/hosts servers

To check space in the servers we use

sudo ansible -a "free -h" servers

Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Happy Learning!

~kritika :)

Connect with me: LinkedIn