Ansible Notes
Ansible Notes
Ansible Notes
ANSIBLE
NOTES
Divya Satpute
Divya Satpute
Table Content
1. What is Ansible ??
2. Configuration Management
3. Push Based vs Pull Based
4. How to install Ansible
5. Host Inventory
6. YAML
7. Playbooks
8. Hands on
9. Conclusion.
Divya Satpute
What is Ansible?
Ansible is one among the DevOps Configuration management tools which is famous for its
simplicity. It is an open-source software developed by Michael DeHaan and its ownership is
on RedHat.
This tool is very simple to use yet powerful enough to automate complex multi-tier IT
application environments.
The main component of ansible are playbooks. configuration management and deployment.
just you want to specify what state you want the system to be in and ansible take care of it.
• Agentless: No need to install agents on managed nodes, reducing complexity and overhead.
• Versatile: Works seamlessly across diverse platforms including Linux, Windows, and cloud
environments.
Configuration Management
Configuration Management: Automate server setups, software installations,
and system configurations.
It is a method through which we automate admin task.
Configuration Management tool turns your code into Infrastructure.
so your code would be testable, repeatable and Versionable.
Infrastructure refers to the composite of -
• software
• Network
• People
• Process
Inventory File
Ansible's inventory hosts file is used to list and group your servers. its default
location is /etc/ansible/hosts
Note: In inventory host file we can mention IP address or Hostname also
Sample of Inventory file
[webservers]
web1.example.com
#web2.example.com
[databases]
192.168.1.20
192.168.1.21
db1.example.com
db2.example.com
Divya Satpute
Ansible setup
create folder
$mkdir keys
Divya Satpute
Now copy the .pem file from your local to remote Machine by using scp
command
$scp -i "divya.pem" divya.pem ubuntu@ec2-13-127-5-106.ap-south-
1.compute.amazonaws.com:/home/ubuntu/keys
verify that your .pem file (key_file) is present on keys folder which we cerate
Now go to your hosts file (Inventory file) and make some configuration like
Host_IP and pass variable (all:vars) where you can specify you key file location
Note: Reverify that you configure all detail correctly
$sudo vim /etc/ansible/hosts
check your connectivity (that your master node Attach to host node
or not)
$ansible all -m ping
Ansible Playbooks
Divya Satpute
---
- name: Setup NGINX Server
hosts: webservers
become: yes
tasks:
- name: Install NGINX
apt:
name: nginx
state: present
update_cache: yes
enabled: yes
src: index.html
dest: /var/www/html/index.html
owner: www-data
group: www-data
mode: '0644'
Divya Satpute
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Visit Learn with Divya</title>
<style>
.button:hover {
background-color: #4682b4;
}
</style>
</head>
<body>
<div>
<h1>Visit Learn with Divya!</h1>
<p>Check out our blog for great content and tutorials.</p>
<a href="https://learnwithdivya.hashnode.dev/" class="button"
target="_blank">Visit Now</a>
</div>
</body>
</html>
Divya Satpute
$ansible-playbook setup-nginx.yml
before you access the application open all traffic rule in your
Inbound rule
Test Results:
Connecting host node success