Day 55 Task: Understanding Configuration Management with Ansible

Day 55 Task: Understanding Configuration Management with Ansible

Ansible simplifies and streamlines the management of IT infrastructure and applications

ยท

5 min read

๐Ÿ”ถ What's this Ansible?

Ansible is an open-source automation tool or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.

Here are some key features and concepts associated with Ansible:

  1. Agentless: Ansible uses an agentless architecture, meaning it doesn't require any software or agents to be installed on the target systems. Instead, it relies on SSH or other secure communication methods to connect to and manage remote hosts, which makes it easy to get started and maintain.

  2. Playbooks: Ansible automation is defined in Playbooks, which are written in YAML (Yet Another Markup Language). Playbooks describe a set of tasks and configurations to be applied to target hosts. These tasks can include actions like package installation, file copying, service management, and more.

  3. Modules: Ansible uses modules to perform specific tasks on target systems. Modules are included in Playbooks and provide a way to interact with the underlying infrastructure. Ansible comes with a large collection of built-in modules, and you can also create custom modules to suit your needs.

  4. Inventory: The Inventory file is used to define and organize the hosts or groups of hosts that Ansible will manage. It can be a static text file or a dynamic inventory source (e.g., AWS EC2 instances, cloud providers, or other databases).

  5. Idempotent: Ansible enforces the principle of idempotence, which means that running an Ansible playbook multiple times has the same result as running it once. This ensures that the system's configuration remains consistent, and you can safely rerun Ansible Playbooks.

  6. Roles: Roles are a way to organize and package Playbooks, variables, and other Ansible components into reusable and shareable units. Roles help in maintaining a clean and modular codebase.

  7. Ad-hoc Commands: In addition to Playbooks, Ansible allows for ad-hoc commands that can be run from the command line to perform quick, one-off tasks on remote hosts.

  8. Community and Extensibility: Ansible has a large and active community that contributes to the development of Ansible roles, modules, and playbooks. You can easily extend Ansible's functionality with custom scripts and modules.

  9. Integration: Ansible can be integrated with various infrastructure and application components, making it a valuable tool in DevOps and IT automation. It supports integrations with cloud providers, configuration management databases (CMDBs), monitoring tools, and more.

๐Ÿ”ถ Task-01: Installation of Ansible on AWS EC2 (Master Node)

sudo apt-add-repository ppa:ansible/ansible 
sudo apt update -y
sudo apt install ansible -y

To install Ansible on an AWS EC2 instance and set it up as a master node, you can follow these steps:

Step 1: Launch an EC2 Instance

  1. Log in to your AWS Management Console.

  2. Navigate to the EC2 dashboard.

  3. Launch a new EC2 instance.

  4. Configure the instance with appropriate security groups and key pairs. Make sure you have SSH access to the instance.

  5. Launch the instance.

  6. Connect to Your EC2 Instance

Step 2: Add Ansible PPA repository by using the below command

sudo apt-add-repository ppa:ansible/ansible

Step 3: Update the System

Update the package manager and upgrade the system packages to the latest versions:

sudo apt update -y

Step 4: Install Ansible

sudo apt install ansible -y

Step 5: Verify Ansible Installation

You can verify the installation by checking the Ansible version:

ansible --version

This should display the installed Ansible version.


๐Ÿ”ถ Task-02: Read more about the Host file

Ansible Inventory - Ansible automates tasks on managed nodes or โ€œhostsโ€ in your infrastructure, using a list or group of lists known as inventory. You can pass host names at the command line, but most Ansible users create inventory files. Your inventory defines the managed nodes you automate, with groups so you can run automation tasks on multiple hosts at the same time. Once your inventory is defined, you use patterns to select the hosts or groups you want Ansible to run against.

  1. Create an inventory file in the below path and name the file as host.

    sudo vim /etc/ansible/hosts

     sudo vim /etc/ansible/hosts
    
  2. Before that, we need to create two Ansible node servers which will be connected to the Ansible master server.

  3. Assign the values in the file as shown in the below screenshot and save.

     [servers]
     server1 ansible_host=<server1_ip>
     server2 ansible_host=<server2_ip>
    
     [all:vars]
     ansible_user=ubuntu
     ansible_ssh_private_key_file=~/.ssh/id_rsa
     ansible_python_interpreter=/usr/bin/python3
    
  4. Let's verify the inventory that we have created.

    ansible-inventory --list -y

     ansible-inventory --list -y -i /etc/ansible/hosts
    


๐Ÿ”ถ Task-03: Setup 2 more EC2 instances with the same Private keys as the previous instance (Node)

  • Setup 2 more EC2 instances with the same Private keys as the previous instance (Node)

    We have created the two servers and set up the server to master the above task.

  • Copy the private key to the master server where Ansible is set up.

    1. Create a public key on the master server and copy the key using the ssh-keygen command.

       ssh-keygen
      

    2. We can see id_rsa.pub which is the public key of the master server.

    3. Copy the above public key of the master to both the node servers.

  • Try a ping command using Ansible to the Nodes.

    1. Now, use the ping command with the input of inventory file to it so that it will ping both the node servers.

       ansible all -m ping -u ubuntu
      

    2. We can see both pings are successful which indicates servers are in active states.

That's it! We have now set up two additional EC2 instances, copied the private key to the Ansible master, and tested Ansible's connectivity to the nodes.

Happy Learning :)

If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! ๐Ÿš€ #devops #90daysofdevop #AWS

ย