Table of contents
What's this Ansible?
Ansible is an open-source automation tool used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning. It simplifies these tasks by allowing users to define their infrastructure as code. Here are some key points about Ansible:
Agentless Architecture: Unlike many other automation tools, Ansible does not require any agents or special software to be installed on the nodes it manages. It uses SSH (Secure Shell) for Unix-based systems and WinRM (Windows Remote Management) for Windows systems.
Playbooks and Roles: Ansible configurations are defined in YAML files called playbooks. Playbooks describe the desired state of the system, and Ansible takes care of making the system match that state. Roles allow for organizing playbooks and tasks in a modular way, promoting reuse and maintainability.
Idempotency: Ansible ensures that the results of operations are the same whether they are run once or multiple times. This is crucial for maintaining consistent environments.
Modules: Ansible uses modules to perform its tasks. These modules can manage system resources, cloud resources, networks, and more. Users can also create custom modules if needed.
Inventory: Ansible maintains an inventory of the systems it manages. This inventory can be specified in a static file or dynamically fetched from various sources.
Extensibility: Ansible is highly extensible with a wide range of built-in modules and plugins. The Ansible Galaxy community platform provides additional roles and playbooks shared by the community.
Ansible Tower: For enterprise users, Ansible Tower provides a web-based interface, REST API, and other features such as role-based access control, job scheduling, and graphical inventory management.
Task 01
Installation of Ansible on AWS EC2 (Master Node)
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
To install Ansible on an AWS EC2 instance (Master Node) running Ubuntu, you can follow the steps you provided. Here is a detailed guide to help you through the process:
Launch an EC2 Instance:
Go to the AWS Management Console.
Navigate to the EC2 Dashboard and click "Launch Instance".
Select an Ubuntu AMI (Amazon Machine Image), such as "Ubuntu Server 20.04 LTS".
Choose an instance type (e.g., t2.micro for free tier eligibility).
Configure instance details, add storage, and add tags as needed.
Configure the security group to allow SSH (port 22) access.
Review and launch the instance. Make sure to download the key pair (or use an existing one).
Connect to the EC2 Instance:
Open a terminal on your local machine.
Connect to your EC2 instance using SSH:
shCopy codessh -i /path/to/your-key.pem ubuntu@your-ec2-public-dns
Replace
/path/to/your-key.pem
with the path to your downloaded key pair, andyour-ec2-public-dns
with the public DNS name of your EC2 instance.
Install Ansible:
Once connected to your EC2 instance, run the following commands:
shCopy codesudo apt-add-repository ppa:ansible/ansible sudo apt update sudo apt install ansible -y
Here's a step-by-step breakdown of what these commands do:
sudo apt-add-repository ppa:ansible/ansible
: Adds the Ansible PPA (Personal Package Archive) to your list of repositories, allowing you to install the latest version of Ansible.sudo apt update
: Updates the package list to include the latest packages from the newly added Ansible PPA.sudo apt install ansible -y
: Installs Ansible and automatically confirms the installation.
Verify Ansible Installation:
Check that Ansible is installed correctly by running:
shCopy codeansible --version
This command should display the installed version of Ansible along with some configuration details.
Task 02
read more about Hosts file
sudo nano /etc/ansible/hosts ansible-inventory --list -y
he /etc/ansible/hosts
file, often referred to as the Ansible inventory file, is where you define the hosts and groups of hosts that Ansible will manage. This file can be customized to suit the needs of your infrastructure, allowing you to specify different groups, variables, and host details.
Editing the Hosts File
To edit the hosts file, you can use a text editor like nano
:
shCopy codesudo nano /etc/ansible/hosts
Here’s a basic example of what the hosts file might look like:
iniCopy code[webservers]
web1.example.com
web2.example.com
[dbservers]
db1.example.com
db2.example.com
[all:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=/path/to/your-key.pem
Groups: Hosts can be grouped under a name, such as
[webservers]
and[dbservers]
.Host entries: Under each group, you list the hostnames or IP addresses of the servers.
Group variables: The
[all:vars]
section defines variables that apply to all hosts.
Using ansible-inventory
The ansible-inventory
command helps you view and manage your inventory in a structured format. The --list
option displays the inventory as JSON, and the -y
option formats the output in YAML.
shCopy codeansible-inventory --list -y
This command reads the inventory file and outputs a YAML representation of the inventory. Here’s an example of what the output might look like:
yamlCopy codeall:
children:
dbservers:
hosts:
db1.example.com:
db2.example.com:
webservers:
hosts:
web1.example.com:
web2.example.com:
vars:
ansible_ssh_private_key_file: /path/to/your-key.pem
ansible_user: ubuntu
Detailed Example
Let’s consider a more detailed example. Suppose you have an infrastructure with web servers and database servers, each with specific variables:
Edit the hosts file:
shCopy codesudo nano /etc/ansible/hosts
Add the following content:
iniCopy code[webservers] web1.example.com ansible_host=192.168.1.101 ansible_user=ubuntu web2.example.com ansible_host=192.168.1.102 ansible_user=ubuntu [dbservers] db1.example.com ansible_host=192.168.1.201 ansible_user=ubuntu db2.example.com ansible_host=192.168.1.202 ansible_user=ubuntu [all:vars] ansible_ssh_private_key_file=/path/to/your-key.pem
View the inventory in YAML format:
shCopy codeansible-inventory --list -y
The output might look like this:
yamlCopy codeall: children: dbservers: hosts: db1.example.com: ansible_host: 192.168.1.201 ansible_user: ubuntu db2.example.com: ansible_host: 192.168.1.202 ansible_user: ubuntu webservers: hosts: web1.example.com: ansible_host: 192.168.1.101 ansible_user: ubuntu web2.example.com: ansible_host: 192.168.1.102 ansible_user: ubuntu vars: ansible_ssh_private_key_file: /path/to/your-key.pem
Tips for Managing the Hosts File
Dynamic Inventory: For dynamic and large-scale environments, you can use dynamic inventory scripts or plugins that integrate with cloud providers (like AWS, Azure, GCP) to automatically generate inventory.
Host Variables: You can define specific variables for each host directly in the inventory file.
Group Variables: You can define variables that apply to all hosts in a group or across multiple groups.
Host Patterns: Ansible supports various patterns to select hosts, making it flexible to run tasks on specific groups or subsets of hosts.