Ansible and Ansible Playbooks: Simplifying Automation
Table of contents
Ansible is an open-source automation tool that streamlines configuration management, application deployment, task automation, and more. It simplifies the process of managing and orchestrating infrastructure by enabling you to define tasks and configurations in a declarative manner.
In this article, we will explore how to create an Ansible project using Ansible Playbooks. We'll dive into the world of Ansible, focusing on how to structure and organize your automation workflows effectively.
Learn the best practices for creating Ansible projects to simplify configuration management, application deployment, and system orchestration. Leverage the power of Ansible to streamline your infrastructure management and enhance the efficiency of your IT operations.
To get started with Ansible on Ubuntu, follow these steps:
Setting Up Ansible on Ubuntu
Create four instances: Master server (Instance 1), Host 1 (Instance 2), Host 2 (Instance 3), and Host 3 (Instance 4).
Connect the master server with SSH and install Ansible on it using the following command:
sudo apt-add-repository ppa:ansible/ansible sudo apt update sudo apt install ansible
Create a
keypair
, and push your private key to the remote machine using SCP.scp -i "yourkeypair" filename ec2-user@publicIPaddress:/home/ubuntu/keys
Use
vim
to create and edit the Ansible hosts file.
[servers]
server1 ansible_host=18.216.117.221
server2 ansible_host=3.22.225.41
[al2]
server3 ansible_host=3.145.172.247
[al2:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ec2-user
ansible_ssh_private_key_file=/home/ubuntu/ansible-keys/al2-key.pem
[servers:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/ubuntu/ansible-keys/mydevops-key.pem
5 . Verify that the servers are up and running using the following command
ansible -m ping servers
ansible -a "free -h" servers
ansible -a "uptime" servers
Ansible Playbook: Deploying Nginx
An Ansible playbook is a configuration file written in YAML that defines tasks, configurations, and plays to be executed by Ansible. They are the primary building blocks for defining automation workflows.
Let's Create a playbook to install Nginx and add the content given below
sudo vim install_nginx.yml
- name: Install and Start Nginx
hosts: my-servers
become: yes
tasks:
- name: Update apt
apt:
update_cache: yes
- name: Install Nginx
apt:
name: nginx
state: latest
- name: Start Nginx
service:
name: nginx
state: started
enabled: yes
Run the playbook using the ansible-playbook command:
ansible-playbook install_nginx.yml
Check the status of Nginx on all the child servers:
ansible all -a "sudo systemctl status nginx"
For deploying a static website, create a new file, index.html, in the playbook directory, and add some sample content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DevOps is Amazing</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f7f7f7;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #fff;
padding: 40px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
text-align: center;
}
h1 {
color: #333;
font-size: 36px;
margin-bottom: 20px;
}
p {
color: #666;
font-size: 20px;
line-height: 1.6;
}
</style>
</head>
<body>
<div class="container">
<h1 style="font-family: 'Courier New', Courier, monospace;">DevOps is Amazing</h1>
<p style="font-style: italic;">Revolutionizing the way we build, deploy, and manage software.</p>
</div>
</body>
</html>
Update the Ansible playbook file to include the index.html file path and set the destination to the default NGINX web page directory:
- name: Install and Start Nginx
hosts: my-servers
become: yes
tasks:
- name: Update apt
apt:
update_cache: yes
- name: Install Nginx
apt:
name: nginx
state: latest
- name: Start Nginx
service:
name: nginx
state: started
enabled: yes
- name: Deploy Nginx
copy:
src: /home/ubuntu/index.html
dest: /var/www/html/index.html
become: true
become_user: root
become_method: sudo
Run the playbook
ansible-playbook install_nginx.yml
Once the playbook finishes executing, open a web browser and enter the public IP address of one of the EC2 instances running Nginx.
Conclusion This article provides a comprehensive guide on deploying a website using an Ansible Playbook. It covers setting up an Ansible project structure, creating EC2 instances, installing Ansible, configuring secure communication, and deploying Nginx for a static website.