🔹What Is Ansible?
Ansible is an open-source automation tool that simplifies the management and configuration of systems, applications, and infrastructure. It allows you to automate repetitive tasks, deploy applications, and manage configurations efficiently across multiple servers or devices.
Here are some key features and benefits of Ansible:
Agentless: Ansible operates in a agentless manner, meaning it doesn't require any software or agents to be installed on the target systems. It communicates with the systems using SSH or WinRM protocols, making it lightweight and easy to set up.
Simple and Human-readable: Ansible uses a declarative language called YAML (Yet Another Markup Language) for defining tasks, playbooks, and configurations. YAML is easy to understand, write, and maintain, making Ansible playbooks highly readable and accessible to both developers and system administrators.
Idempotent and Safe: Ansible follows an idempotent approach, meaning you can run the same playbook multiple times, and it will ensure that the desired state is achieved on the target systems. It performs checks and takes actions only if necessary, reducing the risk of unintended changes or system disruption.
Infrastructure as Code (IaC): Ansible promotes the concept of Infrastructure as Code, allowing you to define your infrastructure and configurations in a code-like format. This makes it easier to version control, collaborate, and reproduce infrastructure environments consistently.
Extensibility and Integrations: Ansible provides a vast collection of modules that cover a wide range of tasks, from managing system packages and services to configuring cloud resources and network devices. It also integrates well with other tools and systems, enabling you to orchestrate complex workflows and integrate with existing automation processes.
Multi-Platform and Cross-Cloud: Ansible is designed to work across various platforms, including Linux, macOS, and Windows, making it suitable for managing heterogeneous environments. It also supports major cloud providers, enabling you to automate and manage resources in public, private, and hybrid cloud environments.
Scalability and Orchestration: Ansible allows you to scale your automation efforts by using an agentless push-based model. You can manage thousands of systems simultaneously and perform orchestrated tasks across different hosts or groups of hosts, making it suitable for both small-scale and large-scale infrastructures.
Overall, Ansible provides a streamlined and efficient way to automate routine tasks, enforce consistent configurations, and deploy applications, resulting in increased productivity, reduced errors, and improved infrastructure management.
🔹Ansible Setup
Installation of Ansible:
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible
Copy the Private Key to Host Server (Ansible_host) 🔑
Transfer the Private Key:
- From your local machine, copy the key to the host server using SCP.
scp -i /path/to/your-key-pair.pem /path/to/your-key-pair.pem ubuntu@your-ec2-instance-public-dns:/home/ubuntu/.ssh/
Set Permissions on the Key:
- SSH into your host server.
ssh -i /path/to/your-key-pair.pem ubuntu@your-ec2-instance-public-dns
- Change permissions of the private key.
chmod 600 /home/ubuntu/.ssh/ansible_keys.pem
open the file sudo nano /etc/ansible/hosts
[servers]
server1 ansible_host=<Public IP>
server2 ansible_host=<Public IP>
server3 ansible_host=<Public IP>
[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/<file>
🔹Ansible adhoc commands
Ansible ad-hoc commands are one-liner commands used to perform quick tasks on remote servers without writing a full playbook. These commands are useful for simple operations such as checking the status of services, copying files, or managing packages.
Here's a breakdown of the key components of an Ansible ad-hoc command:
ansible
: The command-line tool used to run the ad-hoc command.Host Pattern: Specifies the group of hosts or individual hosts to run the command against. This can be an inventory group name, a hostname, or an IP address.
Module: The Ansible module to run. Modules are the units of work in Ansible, such as
ping
,shell
,copy
,yum
, etc.Arguments: Parameters to pass to the module, which specify the action to take or the configuration to apply.
Examples of Ansible Ad-Hoc Commands:
Ping All Hosts:
ansible all -m ping
This command pings all the hosts in the inventory to check their connectivity.
Check Disk Usage:
ansible all -m shell -a 'df -h'
This command uses the shell
module to execute the df -h
command on all hosts, displaying the disk usage.
Install a Package:
ansible webservers -m apt -a 'name=httpd state=present'
This command installs the httpd
package on all hosts in the webservers
group using the yum
module.
Copy a File:
ansible all -m copy -a 'src=/home/user/file.txt dest=/tmp/file.txt'
This command copies a file from the local system to the remote hosts.
Restart a Service:
ansible database -m service -a 'name=postgresql state=restarted'
This command restarts the postgresql
service on all hosts in the database
group.
Gather Facts:
ansible all -m setup
This command gathers detailed information (facts) about all the hosts.
Explanation of a Command Example:
Let's break down the Install a Package
example:
ansible servers -m apt -a 'name=httpd state=present'
ansible
: Calls the Ansible command-line tool.webservers
: Specifies the group of hosts defined in the inventory to target.-m apt
: Indicates that theapt
module should be used.-a 'name=httpd state=present'
: Provides the arguments to theyum
module, specifying that thehttpd
package should be installed (state=present
).
Ansible ad-hoc commands are powerful for quick, one-off tasks and can save time when you need to perform operations on multiple servers without creating a full playbook.
🔹Creating a playbook
Ansible playbooks are YAML files that define a series of tasks to be executed on a group of hosts. They allow for more complex automation and configuration management than ad-hoc commands. Playbooks are highly readable and reusable, making them ideal for documenting and sharing automation processes.
Structure of an Ansible Playbook:
Playbook: The top-level structure that contains one or more plays.
Play: A set of tasks to be executed on specified hosts. Each play targets a group of hosts and defines a series of tasks to be performed on them.
Task: An action to be executed, such as installing a package, copying a file, or restarting a service.
Module: The specific unit of work to be executed in each task, such as
yum
,copy
, orservice
.
Ansible playbook to install a webserver and deploy a static application on to it using Ansible:
create index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sample Index Page</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
color: #333;
}
header {
background-color: #4CAF50;
color: white;
padding: 1em;
text-align: center;
}
main {
padding: 1em;
}
footer {
background-color: #333;
color: white;
text-align: center;
padding: 1em;
position: fixed;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<header>
<h1>I am learning Ansible with Abhishek Veeramalla</h1>
</header>
<main>
<h2>Ansible Zero to Hero Series</h2>
<p> This is my First Ansible Playbook, Where I learnt how to deploy a static app on ec2 instance using Ansible. </p>
<p>For more information, visit www.youtube.com/abhishekveeramalla</p>
</main>
<footer>
<p>© 2024 Abhishek.Veeramalla</p>
</footer>
</body>
</html>
create first_playbook.yml
---
- hosts: all
become: true
tasks:
- name: Install apache httpd
ansible.builtin.apt:
name: apache2
state: present
update_cache: yes
- name: Copy file with owner and permissions
ansible.builtin.copy:
src: index.html
dest: /var/www/html
owner: root
group: root
mode: '0644'
ansible-playbook first_playbook.yml