The Complete Visual Guide: Ansible Setup on AWS EC2 with Ad-hoc Commands

The Complete Visual Guide: Ansible Setup on AWS EC2 with Ad-hoc Commands

ยท

3 min read

Initial Setup in AWS Console

Step 1: Create EC2 Instances

  • Launch one EC2 instance as Ansible Server

  • Scale it to launch 4 instances of t2.micro

  • Rename 3 instances to Worker 1, Worker 2, and Worker 3

  • Ensure all instances are in the same security group with appropriate SSH access

Step 2: Instance Initialization

  • Wait for all instances to show "Running" status

  • Verify that all status checks have passed

Step 3: Connect to Ansible Server

  • Connect to the Ansible server instance using SSH

  • Verify connection is successful

Ansible Installation and Configuration

Step 4: Install Ansible

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

Step 5: Verify Ansible Installation

ansible --version

You should see output showing the Ansible version and configuration paths.

Step 6: Configure Hosts File

Edit the hosts file:

sudo vim /etc/ansible/hosts

Add the following configuration:

[webservers]
server1 ansible_host=<Worker1-Private-IP>
server2 ansible_host=<Worker2-Private-IP>
server3 ansible_host=<Worker3-Private-IP>

[all:vars]
ansible_ssh_private_key_file=/home/ubuntu/.ssh/key.pem
ansible_user=ubuntu

Step 7: SSH Key Setup

  1. Copy existing key from local machine:
scp -i your-key.pem your-key.pem ubuntu@ansible-server:/home/ubuntu/.ssh/key.pem
  1. or direct copy the key form the local Machine

Set proper permissions:

chmod 400 /home/ubuntu/.ssh/key.pem

Testing and Verification

Step 8: Test Connection

Test connection to all workers:

ansible all -m ping

Step 9: Python Interpreter Warning

You'll notice a warning about Python interpreter in the output.

Step 10: Update Hosts File

Update the [all:vars] section in your hosts file:

[all:vars]
ansible_ssh_private_key_file=/home/ubuntu/.ssh/key.pem
ansible_user=ubuntu
ansible_python_interpreter=/usr/bin/python3

Step 11: Verify Updated Configuration

Run ping test again:

ansible all -m ping

The output should now be clean without warnings.

Using Ad-hoc Commands

Step 12: Check System Uptime

ansible all -m command -a "uptime"

This will show how long each server has been running.

Step 13: Install Nginx

ansible all -m apt -a "name=nginx state=present" --become

This command installs Nginx on all worker nodes.

Step 14: Verify Nginx Installation

Check Nginx status:

ansible all -m service -a "name=nginx state=started" --become

Verify Nginx is running:

ansible all -m command -a "systemctl status nginx" --become

Troubleshooting Tips

  1. SSH Connection Issues:

    • Verify security group settings

    • Check key permissions (should be 400)

    • Ensure private IPs are correct

  2. Python Interpreter Issues:

    • Verify Python is installed on all workers

    • Check the Python path is correct

  3. Permission Issues:

    • Ensure the ubuntu user has sudo privileges

    • Verify key permissions are set correctly

    • Use --become flag when required

  4. Nginx Installation Issues:

    • Check network connectivity

    • Verify apt cache is updated

    • Check available disk space

ย