Performing Ad-Hoc Commands and Playbooks Using Patterns in Ansible

Ayush SharmaAyush Sharma
3 min read

Introduction

Ansible is a powerful automation tool that enables infrastructure as code, configuration management, and application deployment with ease. This guide provides a detailed walkthrough for setting up Ansible, configuring inventory files, executing ad-hoc commands, and running playbooks on AWS EC2 instances. By following this guide, you will gain hands-on experience in using patterns effectively in Ansible.

Prerequisites

Before you begin, ensure that you have:

  1. Ansible installed on your local machine.

  2. An AWS EC2 instance set up with a valid SSH key pair.

  3. A user with necessary permissions to execute Ansible commands.

Verify Ansible Installation

To check if Ansible is installed, run:

ansible --version

If Ansible is not installed, follow the installation steps below.

Step 1: Install Ansible

For Ubuntu-based systems, install Ansible using:

sudo apt update
sudo apt install ansible -y

For other systems, refer to the official Ansible documentation.

Step 2: Create Directory Structure

Organize your project by creating a dedicated directory:

mkdir -p ~/ansible/aws_keypairs
cd ~/ansible

Step 3: Copy AWS Key Pair

Ensure your AWS key pair is placed in the correct directory:

cp ~/aws_keypairs/Test.pem ~/ansible/aws_keypairs/

Step 4: Set Key Pair Permissions

Set secure file permissions for your SSH key:

chmod 400 ~/ansible/aws_keypairs/Test.pem

This prevents unauthorized access and ensures SSH connections work correctly.

Step 5: Create an Inventory File

Create an inventory file to define target hosts:

nano ~/ansible/inventory.ini

Add the following content:

[webservers]
ec2-13-127-126-77.ap-south-1.compute.amazonaws.com
ec2-13-201-132-221.ap-south-1.compute.amazonaws.com

[webservers:vars]
ansible_ssh_user=ubuntu
ansible_ssh_private_key_file=~/ansible/aws_keypairs/Test.pem

Save and exit the file.

Step 6: Test SSH Connection

Before using Ansible, manually test SSH connectivity:

ssh -i ~/ansible/aws_keypairs/Test.pem ubuntu@ec2-13-127-126-77.ap-south-1.compute.amazonaws.com

If you face Permission denied (publickey) errors:

  • Ensure the correct user (ubuntu for Ubuntu, ec2-user for Amazon Linux).

  • Verify that the SSH key is correctly associated with the EC2 instance.

  • Check that the security group allows inbound SSH (port 22) connections.

Add Host to Known Hosts

If prompted about authenticity, add the host manually:

ssh-keyscan -H ec2-13-127-126-77.ap-south-1.compute.amazonaws.com >> ~/.ssh/known_hosts

Step 7: Run Ansible Ping Command

Test Ansible connectivity to the EC2 instances:

ansible webservers -i ~/ansible/inventory.ini -m ping

If successful, it will return pong messages from the servers.

Step 8: Troubleshooting SSH and Ansible Issues

  • Check Directory Structure:

      ls -l ~/ansible/aws_keypairs/
    
  • Verify Key Permissions:

      ls -l ~/ansible/aws_keypairs/Test.pem
    
  • Check Network Connectivity:

      ping ec2-13-127-126-77.ap-south-1.compute.amazonaws.com
    

Step 9: Execute Ad-Hoc Commands

Now that connectivity is confirmed, you can execute ad-hoc Ansible commands.

Restart the Apache service on all web servers:

ansible webservers -i ~/ansible/inventory.ini -m service -a "name=apache2 state=restarted"

Install Apache2 on EC2 Instances

  1. Update packages:

     sudo apt update
    
  2. Install Apache:

     sudo apt install apache2 -y
    
  3. Start Apache service:

     sudo systemctl start apache2
    
  4. Enable Apache to start on boot:

     sudo systemctl enable apache2
    
  5. Check Apache status:

     sudo systemctl status apache2
    

Step 10: Create an Ansible Playbook

For automation, create a playbook to restart Apache.

  1. Create a YAML file:

     nano restart_apache.yml
    
  2. Add the following content:

     - name: Restart Apache on webservers
       hosts: webservers
       tasks:
         - name: Restart Apache service
           service:
             name: apache2
             state: restarted
    
  3. Save and exit.

Step 11: Run the Ansible Playbook

Execute the playbook using:

ansible-playbook -i ~/ansible/inventory.ini restart_apache.yml

Summary

  1. Set up Ansible and install required packages.

  2. Define inventory files to specify target hosts.

  3. Verify SSH connectivity before running commands.

  4. Execute ad-hoc commands for quick operations.

  5. Use playbooks for automation and repeatability.

  6. Troubleshoot common issues to ensure smooth execution.

By following this guide, you have successfully automated server management tasks using Ansible! ๐Ÿš€

0
Subscribe to my newsletter

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

Written by

Ayush Sharma
Ayush Sharma