Ansible in Action: Configuring and Managing EC2 Instances on AWS

MOHAMMAD TAHAMOHAMMAD TAHA
4 min read

Introduction

This week, I took my Ansible skills to the next level by setting up and configuring a simple yet powerful environment on AWS. I deployed three Amazon Linux 2 instances, designated one as the Ansible server, and the other two as nodes. In this blog, I’ll walk you through the process of setting up Ansible, adding hosts, configuring users, and managing nodes—all while sharing the challenges I faced and how I overcame them.


What I Learned This Week

  1. Installing Essential Packages:

    • I learned how to download and install the EPEL (Extra Packages for Enterprise Linux) package, which is necessary for accessing additional software packages not included in the default repositories.

    • I also installed Python, python-pip, python-level, Git, and Ansible, ensuring my environment was ready for automation tasks.

  2. Configuring Ansible Hosts:

    • Adding hosts (nodes) in the /etc/ansible/hosts file allowed me to define which machines Ansible will manage. This was a critical step in setting up my environment.
  3. Customizing Ansible Configuration:

    • I explored the ansible.cfg file, where I uncommented the inventory and sudo-user settings to ensure seamless communication between the Ansible server and the nodes.
  4. User Management and Privileges:

    • Creating an ansible user across all three instances and granting it full privileges via the visudo file was crucial for running Ansible tasks with the necessary permissions.
  5. Establishing SSH Connections:

    • I connected the nodes using SSH from the Ansible server, allowing me to securely manage and configure the nodes through Ansible.

Steps I Took

  1. Downloaded and Installed EPEL Package Latest 8:

    • I started by enabling the EPEL repository, which provides additional packages for Enterprise Linux distributions.

        amazon-linux-extras install epel -y
      
  2. Installed Python, python-pip, python-level, Git, and Ansible:

    • Next, I installed the necessary software to run Ansible and manage my nodes.

        yum install python3 python3-pip python3-devel git ansible -y
      
  3. Added Hosts in /etc/ansible/hosts:

    • I edited the Ansible hosts file to include the private IP addresses of my two nodes, which allowed the Ansible server to communicate with them.

        vi /etc/ansible/hosts
      
    • Here’s a sample of what the file might look like:

        node1 ansible_host=<private-ip-node1>
        node2 ansible_host=<private-ip-node2>
      
  4. Uncommented Inventory and Sudo-User in /etc/ansible/ansible.cfg:

    • I configured the Ansible settings to match my environment by editing the ansible.cfg file.

        vi /etc/ansible/ansible.cfg
      
    • I uncommented the following lines:

        codeinventory = /etc/ansible/hosts
        sudo-user = ansible
      
  5. Created a User Named "ansible" on All Three Instances:

    • I created an ansible user on each instance and set up passwordless SSH access.

        adduser ansible
        sudo passwd ansible
      
  6. Granted All Privileges to the ansible User via visudo:

    • I edited the sudoers file to grant full sudo privileges to the ansible user.

        visudo
      
    • Added the following line:

        ansible ALL=(ALL) NOPASSWD: ALL
      
  7. Connected Nodes as ansible User via SSH:

    • I connected to each node from the Ansible server using the private IP addresses:

        ssh ansible@<private-ip-node1>
        ssh ansible@<private-ip-node2>
      
  8. Configured and Managed Nodes from the Ansible Server:

    • Finally, I used Ansible to configure and manage node1 and node2 from the Ansible server. I executed a simple ping command to ensure everything was set up correctly:

        ansible all -m ping
      

Challenges and How I Solved Them

  1. Issue: SSH connection errors when trying to connect from the Ansible server to the nodes.

    • Solution: I ensured that the security groups for all instances allowed inbound SSH traffic on port 22. I also double-checked that the public SSH keys were correctly added to the authorized_keys file for the ansible user on each node.
  2. Issue: Configuration settings in ansible.cfg not taking effect.

    • Solution: I verified that the ansible.cfg file was located in the correct directory (/etc/ansible/). I also ensured that the appropriate lines were uncommented and correctly configured.

Resources I Used

  • Official Ansible Documentation: Helped me understand the core concepts and specific configurations needed.

  • AWS Documentation: Guided on setting up and managing EC2 instances.

  • GitHub Repository: I’ve documented all the code and steps involved in this setup. You can check it out here.


Conclusion

Setting up Ansible on AWS EC2 instances was both challenging and rewarding. The process deepened my understanding of infrastructure automation and taught me valuable lessons in managing remote servers. Whether you’re just starting with Ansible or looking to refine your skills, I hope this guide helps you navigate the process smoothly.

Feel free to explore the code and configurations in my GitHub repository and reach out if you have any questions or need further assistance.


This structured approach shares my experience and provides practical guidance for others looking to set up Ansible on AWS.

0
Subscribe to my newsletter

Read articles from MOHAMMAD TAHA directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

MOHAMMAD TAHA
MOHAMMAD TAHA