Performing Ad-Hoc Commands and Playbooks Using Patterns in Ansible

Table of contents
- Introduction
- Prerequisites
- Step 1: Install Ansible
- Step 2: Create Directory Structure
- Step 3: Copy AWS Key Pair
- Step 4: Set Key Pair Permissions
- Step 5: Create an Inventory File
- Step 6: Test SSH Connection
- Step 7: Run Ansible Ping Command
- Step 8: Troubleshooting SSH and Ansible Issues
- Step 9: Execute Ad-Hoc Commands
- Step 10: Create an Ansible Playbook
- Step 11: Run the Ansible Playbook
- Summary

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:
Ansible installed on your local machine.
An AWS EC2 instance set up with a valid SSH key pair.
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
Update packages:
sudo apt update
Install Apache:
sudo apt install apache2 -y
Start Apache service:
sudo systemctl start apache2
Enable Apache to start on boot:
sudo systemctl enable apache2
Check Apache status:
sudo systemctl status apache2
Step 10: Create an Ansible Playbook
For automation, create a playbook to restart Apache.
Create a YAML file:
nano restart_apache.yml
Add the following content:
- name: Restart Apache on webservers hosts: webservers tasks: - name: Restart Apache service service: name: apache2 state: restarted
Save and exit.
Step 11: Run the Ansible Playbook
Execute the playbook using:
ansible-playbook -i ~/ansible/inventory.ini restart_apache.yml
Summary
Set up Ansible and install required packages.
Define inventory files to specify target hosts.
Verify SSH connectivity before running commands.
Execute ad-hoc commands for quick operations.
Use playbooks for automation and repeatability.
Troubleshoot common issues to ensure smooth execution.
By following this guide, you have successfully automated server management tasks using Ansible! ๐
Subscribe to my newsletter
Read articles from Ayush Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
