Ansible Project

What is Ansible?
Ansible is an open-source automation tool used for simplifying and streamlining IT tasks. It doesn't require agents on target systems, uses human-readable playbooks, and is known for its idempotent nature. It's widely used in DevOps and system administration for tasks like configuration management and application deployment. Ansible is extensible, has a strong community, and is valuable for orchestrating complex workflows.
How Ansible Works?
Ansible works by connecting to remote systems and executing tasks defined in playbooks. Here's a simplified overview of how Ansible operates:
Inventory: Ansible starts by reading an inventory file that defines the target systems it will manage. The inventory file can be in various formats, such as INI, YAML, or dynamically generated through scripts.
Playbooks: Automation tasks are defined in Ansible playbooks, which are written in YAML format. Playbooks include a list of tasks, roles, and variables needed to achieve a particular automation goal.
SSH (Unix-like systems) or WinRM (Windows): Ansible uses SSH for Unix-like systems and WinRM for Windows to establish a connection with the target machines. No agent installation is required on the target systems, making Ansible "agentless."
Modules: Ansible executes tasks on the target systems using modules. Modules are small, reusable pieces of code responsible for performing specific actions, such as installing packages, managing users, or copying files. Ansible ships with a wide range of built-in modules.
Task Execution: For each task in the playbook, Ansible sends commands and data to the target systems over the SSH or WinRM connection. It then reports the results back to the Ansible control node.
Idempotence: Ansible is idempotent, meaning it only makes necessary changes to bring the system to the desired state. If a task has already been completed and the system is in the desired state, Ansible won't make unnecessary changes.
Reporting and Output: Ansible provides detailed reporting on the status of each task, allowing you to see what has been changed or if any errors occurred. This helps with troubleshooting and auditing.
Parallel Execution: Ansible can execute tasks in parallel across multiple target systems, making it efficient for managing large infrastructures.
Configuration Management: Ansible can be used for configuration management, ensuring that the systems maintain a desired state over time. It can also be used for continuous integration and continuous deployment (CI/CD) to automate the deployment of applications.
Extensibility: Ansible can be extended through custom modules, plugins, and roles to meet specific automation needs.
Ansible Architecture: Nodes and Modules
Ansible's Architecture is based on the concept of a control node and a managed node. The platform is executed from the control node where a user runs the ansible-playbook command. There must be at least one control node; a backup control node can also exist. The devices being automated and managed by the control node are known as managed nodes.
Ansible automates Linux and Windows by connecting to managed nodes and pushing out small programs called Ansible modules. Ansible executes these modules, which are the resource models of the desired system state, over Secure Socket Shell (SSH) by default and removes them when finished.
Ansible modules are written in Python and can be written in any language. Ansible modules are reusable, standalone scripts that can be used by the Ansible API, Ansible Playbooks, or Ansible Galaxy.
Create 3 Instances on AWS EC2 with the following names:
Ansible_Master_Server
Node_1
Node_2

Installation of Ansible on AWS EC2 (Master Node)
#!/bin/bash
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
sudo chmod 777 install.sh
sudo ./install.sh
ansible --version
Create SSH key on Master Node
cd ~/.ssh
ssh-keygen
What is a Hosts file?
A "hosts file" is a simple text file that maps hostnames to IP addresses on a computer. It's used for local DNS resolution overrides and is often used for testing and development purposes. You can find it on your computer at a specific location, and it takes precedence over DNS resolutions for specified hostnames.
Where is the Hosts file located?
Ansible uses this file to map target hosts to managed nodes. The host file is usually located in
/etc/ansible/hosts.So, open the host file and add the IP addresses of the Nodes:
sudo vim /etc/ansible/hosts
[servers]
Node_1 ansible_host= <Public IP-Adddress of Node-1>
Node_2 ansible_host= <Public IP-Adddress of Node-2>
[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=/home/ubuntu/.ssh/id_rsa
Now we will ping the Node server using Master Node by pasting the public SSH key of Master Node to the Node server.
# Open Node_1
cd /home/ubuntu/.ssh
vim authorized_keys
# Open Node_2
cd /home/ubuntu/.ssh
vim authorized_keys
What is the Ansible Playbook?
An Ansible playbook is a human-readable, YAML-formatted file that defines a set of automation tasks and configurations to be applied to one or more target systems. Playbooks are a fundamental concept in Ansible and serve as the basis for automating various IT tasks. Here's what you need to know about Ansible playbooks:
Automation Script: Ansible playbooks are essentially automation scripts written in YAML. YAML is easy to read and write, making playbooks approachable even for those without extensive programming experience.
Task-Based: Playbooks are organized into tasks, each of which represents a single unit of work. Tasks can include actions like installing software, configuring services, copying files, or making system changes.
Declarative: Playbooks are declarative, meaning they describe the desired state of the target systems. You specify what you want to achieve, and Ansible takes care of making the necessary changes to reach that state. This makes playbooks idempotent, as Ansible only modifies what's required to achieve the desired state.
Hosts and Groups: Playbooks can target one or more hosts or groups of hosts defined in your Ansible inventory. This allows you to apply the same set of tasks to different systems or groups of systems.
Roles: Playbooks can include roles, which are reusable collections of tasks, templates, and variables. Roles help you organize your automation code and encourage modular, maintainable playbooks.
Variables: You can use variables in playbooks to make them more dynamic. Variables allow you to customize tasks and configurations based on different hosts, environments, or user inputs.
Handlers: Handlers are special tasks in playbooks that are triggered only when notified. They are often used for actions that need to be executed in response to changes made by other tasks.
Conditionals: Playbooks support conditional statements, allowing you to execute tasks only if specific conditions are met. This adds flexibility to your automation.
Loops: Playbooks can include loops to repeat tasks with different parameters. This is useful for tasks that need to be performed multiple times with variations.
Tags: You can assign tags to tasks in playbooks, which makes it possible to selectively run specific tasks, providing greater control over playbook execution.
Error Handling: Playbooks can define what to do when errors occur during task execution, including options for ignoring, failing, or handling errors gracefully.
Extensibility: Ansible playbooks can be extended with custom modules, roles, and plugins to meet specific automation requirements.
How to create an Ansible Playbook?
An Ansible playbook is a structured and human-readable document written in YAML (YAML Ain't Markup Language) that defines a set of tasks and configurations to be performed on remote servers. Playbooks are the heart of Ansible automation and allow you to define the desired state of your systems and applications.
YAML Syntax: Playbooks are written in YAML format, which is a human-readable markup language. YAML uses indentation and colons to structure data hierarchically. Indentation is crucial in YAML to define the nesting levels of data.
Play: A playbook starts with a list of plays. Each play targets a specific group of hosts and defines a set of tasks to be executed on those hosts.
name: A user-friendly name for the play.
hosts: The group(s) of hosts this play will target. Host groups are defined in the inventory file.
become: Indicates whether privilege escalation is required to perform tasks (using sudo).
tasks: List of tasks to be executed in this play.
---
- name: My First Playbook
hosts: web_servers
become: yes
tasks:
# Tasks will be defined here
- name: Ensure Nginx is installed
apt:
name: nginx
state: present
name: A description of the task.
module: The Ansible module to use for the task (e.g., apt, yum, file, copy, etc.).
module_arguments: Arguments specific to the module being used.
Create a playbook to install Nginx
- Installing Nginx using Playbook:
-
name: Installing Nginx using Playbook
hosts: all
become: yes
tasks:
- name: Install Nginx
apt:
command: nginx -y
- name: Start and enable Nginx
service:
name: nginx
state: started
enabled: yes
- Checking the status of Nginx on one of the Node_server
service nginx status
nginx -v
Deploy a sample webpage using the Ansible playbook.
cd playbook
# Put any index.html file in the playbook folder
sudo vim index.html
- Modify the playbook as follows:
-
name: Installing Nginx using Playbook
hosts: all
become: yes
tasks:
- name: Install Nginx
apt:
command: nginx -y
- name: Start and enable Nginx
service:
name: nginx
state: started
enabled: yes
- name: Creating Webpage
copy:
src: index.html
dest: /var/www/html/index.html
- Output of the above playbook:





