Ansible is a powerful open-source automation tool that simplifies IT automation, configuration management, and application deployment. It is agentless, meaning it does not require additional software on managed nodes, making it lightweight and easy to use.
This guide will cover:
Installing and setting up Ansible
Configuring Ansible inventory and playbooks
Automating deployment of applications
Using Ansible with cloud providers (AWS, Azure, etc.)
1. Installing and Configuring Ansible
1.1 Installing Ansible
On Ubuntu/Debian:
sudo apt update
sudo apt install ansible -y
On CentOS/RHEL:
sudo yum install ansible -y
On macOS (via Homebrew):
brew install ansible
To verify the installation, run:
ansible --version
1.2 Setting Up SSH Access to Remote Servers
Ansible connects to remote systems via SSH. Ensure you have passwordless SSH access:
ssh-keygen -t rsa -b 4096
ssh-copy-id user@remote-server
Now, you can test connectivity with:
ansible all -m ping -u user
2. Understanding Ansible Inventory and Playbooks
2.1 Ansible Inventory
The inventory file (/etc/ansible/hosts
or inventory.ini
) defines managed hosts. Example:
[web_servers]
web1 ansible_host=192.168.1.10 ansible_user=ubuntu
web2 ansible_host=192.168.1.11 ansible_user=ubuntu
[database_servers]
db1 ansible_host=192.168.1.20 ansible_user=ubuntu
To check connectivity to all servers:
ansible all -i inventory.ini -m ping
3. Writing Ansible Playbooks
A playbook is a YAML file that defines tasks for automation.
3.1 Example: Deploying an Apache Web Server
Create a playbook (deploy_apache.yml
):
- name: Deploy Apache on Web Servers
hosts: web_servers
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Start and Enable Apache
service:
name: apache2
state: started
enabled: yes
Run the playbook:
ansible-playbook -i inventory.ini deploy_apache.yml
3.2 Example: Deploying a Web Application
To deploy a Django or Flask application, create a playbook (deploy_app.yml
):
- name: Deploy Web Application
hosts: web_servers
become: yes
tasks:
- name: Install dependencies
apt:
name: ['python3', 'python3-pip', 'python3-venv']
state: present
- name: Clone the application repository
git:
repo: "https://github.com/example/myapp.git"
dest: "/var/www/myapp"
- name: Install application dependencies
pip:
requirements: "/var/www/myapp/requirements.txt"
virtualenv: "/var/www/myapp/venv"
- name: Start the application
shell: |
source /var/www/myapp/venv/bin/activate
python /var/www/myapp/app.py
Run the playbook:
ansible-playbook -i inventory.ini deploy_app.yml
4. Using Ansible Roles for Modular Deployment
Roles help organize playbooks into reusable components.
4.1 Creating an Ansible Role
Generate a role structure:
ansible-galaxy init my_webapp
It creates a directory with:
my_webapp/
├── tasks/
│ ├── main.yml
├── handlers/
│ ├── main.yml
├── templates/
├── files/
├── vars/
│ ├── main.yml
├── defaults/
│ ├── main.yml
├── meta/
│ ├── main.yml
Define tasks in tasks/main.yml
:
- name: Install dependencies
apt:
name: ['python3', 'python3-pip']
state: present
- name: Start the application
shell: python3 /var/www/myapp/app.py
Run the role in a playbook:
- name: Deploy Web App
hosts: web_servers
roles:
- my_webapp
Run the playbook:
ansible-playbook -i inventory.ini site.yml
5. Automating Cloud Deployments with Ansible
5.1 AWS EC2 Deployment with Ansible
Install the AWS collection:
ansible-galaxy collection install amazon.aws
Create an EC2 instance:
- name: Launch EC2 Instance
hosts: localhost
tasks:
- name: Launch an instance
amazon.aws.ec2_instance:
name: "MyServer"
key_name: "my-key"
instance_type: "t2.micro"
security_group: "default"
image_id: "ami-12345678"
region: "us-east-1"
wait: yes
Run it:
ansible-playbook launch_ec2.yml