Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!
We spend hours on Instagram and YouTube and waste money on coffee and fast food, but wonβt spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!
Learn from Guru Rajesh Kumar and double your salary in just one year.
Notes
What is Ansible?
====================
Server(S) Config mgt tool
From Radhat
Written in py
Release
ansible - cmd
ansible awx - gui - free
ansible tower - gui - paid
What config
Server(S) Config
What in Server?
Virtual - HARDWARE
- file
- directory
- services
- yum - apt
- bathc - bash
- users
- group
Others --> puppet - chef - salt - cfegine
Why Ansible
manage config of 100s server
IDEMPOTENCY
DESIRE ACTUAL
APACHE YES - no need to run the script
CODE YES - no cha
NO - make change
DESIRE aCTUAL
apache 80 apache 80
code
1st --- 10 Steps ----------- 10 STEPS - 10 mins
2nd 1 steps 1 step - 1 mins
==============================================================
How it works aka Ansible Architecure
HUMAN -> ACS ---> ARS
ACS - Ansible control Server
ARS - Ansible Remote Server
ACS ARS
----------------------------------------
Linux ANY
64 bit
Ansible NA
Linux --SSH-- LINUX
Linux --Winrm-- Windows
Linux --SSH-- LINUX
py py
Linux --Winrm-- Windows
py dotnet + ps 3.0
Ansible
Executable(S)
Modules - PY CODE -- RUN IN ARS - based on param
Plugins - PY CODE -- RUN IN ACS
Configfile /etc/ansible/config
cp file dest
===============================================================================
1 Install Ansible - DONE
2 Ansible ADHOC Commands DONE -
3 Ansible Inventory DONE
4. Ansible Playbook
5. Ansible variables - cond
6. Ansible roles
project -- Setup a webserver
----------------------------------
Step 1 - Install Apache2 in ubuntu apt name=apache2 state=present
Step 2 - copy package file copy src=index.html dest=/var/www/html/
Step 3 - Restart a services service name=apache2 state=started
ansible localhost -m apt -a"name=apache2 state=present"
ansible localhost -m copy -a"src=index.html dest=/var/www/html/"
ansible localhost -m service -a"name=apache2 state=started"
What is inventory
-------------------------
LIST of IPS ADD (ARS)
----
CMD
FILE
SCRIPT
ansible all -i "52.66.80.208,52.66.80.209" -m apt -a"name=apache2 state=present" -u ubuntu -k -b
ansible all -i "52.66.80.208,52.66.80.209" -m copy -a"src=index.html dest=/var/www/html/" -u ubuntu -k -b
ansible all -i "52.66.80.208,52.66.80.209" -m service -a"name=apache2 state=started" -u ubuntu -k -b
52.66.80.208
52.66.80.209
52.66.80.210
52.66.80.211
ansible all -i inventory -m apt -a"name=apache2 state=present" -u ubuntu -k -b
ansible all -i inventory -m copy -a"src=index.html dest=/var/www/html/" -u ubuntu -k -b
ansible all -i inventory -m service -a"name=apache2 state=started" -u ubuntu -k -b
52.66.80.208 ansible_user=rajesh ansible_ssh_pass=rajesh12345
52.66.80.209 ansible_user=rajesh ansible_ssh_private_key_file=rajesh.pem
52.66.80.210 ansible_user=rajesh ansible_ssh_pass=rajesh12345
52.66.80.211 ansible_user=rajesh ansible_ssh_pass=rajesh12345
[web]
52.66.80.208 ansible_user=rajesh ansible_ssh_pass=rajesh12345
[app]
52.66.80.209 ansible_user=rajesh ansible_ssh_private_key_file=rajesh.pem
52.66.80.210 ansible_user=rajesh ansible_ssh_pass=rajesh12345
52.66.80.211 ansible_user=rajesh ansible_ssh_pass=rajesh12345
[db]
52.66.80.209 ansible_user=rajesh ansible_ssh_private_key_file=rajesh.pem
52.66.80.210 ansible_user=rajesh ansible_ssh_pass=rajesh12345
52.66.80.211 ansible_user=rajesh ansible_ssh_pass=rajesh12345
ansible web -i inventory -m apt -a"name=apache2 state=present" -u ubuntu -k -b
ansible web -i inventory -m copy -a"src=index.html dest=/var/www/html/" -u ubuntu -k -b
ansible all -i inventory -m service -a"name=apache2 state=started" -u ubuntu -k -b
Hereβs a structured and refined version of your Ansible guide with better readability, explanations, and formatting.
What is Ansible?
Ansible is a server configuration management tool developed by Red Hat. It is written in Python and is widely used for automating IT infrastructure.
Ansible Versions
- Ansible CLI β Command-line tool for executing automation tasks.
- Ansible AWX β Open-source GUI for Ansible automation (Free).
- Ansible Tower β Enterprise GUI version of Ansible (Paid).
What Can Ansible Configure?
Ansible can manage and automate various aspects of a server, including:
- Files & Directories β Managing configuration files, system files, and folders.
- Services β Starting, stopping, and configuring services.
- Package Management β Installing/updating packages via
yum
(Red Hat) orapt
(Debian). - Users & Groups β Managing user accounts and permissions.
- Bash Scripts β Automating batch jobs and shell scripts.
- Infrastructure Components β Virtual machines, networking, cloud infrastructure, etc.
Alternatives to Ansible
Other popular configuration management tools:
- Puppet
- Chef
- SaltStack
- CFEngine
Why Use Ansible?
1. Manage Configurations Across Hundreds of Servers
- Ansible helps maintain configuration consistency across multiple servers.
2. Ensures Idempotency
- Idempotency means Ansible will only make changes when necessary.
- Example:
- If Apache is already installed, Ansible wonβt install it again.
- If code is missing, Ansible will deploy it.
3. Efficient Execution
- The first execution may involve multiple steps, taking time.
- The second execution only runs required changes, saving time.
Example:
Run | Steps | Time Taken |
---|---|---|
First Execution | 10 steps | 10 minutes |
Second Execution | 1 step | 1 minute |
How Ansible Works (Architecture)
Ansible operates using a Control Server (ACS) and multiple Remote Servers (ARS).
Components
Component | Description |
---|---|
ACS (Ansible Control Server) | The system where Ansible is installed and executed. |
ARS (Ansible Remote Server) | The target machines that Ansible configures and manages. |
Communication Protocols
ACS (Control Server) | ARS (Managed Nodes) | Protocol |
---|---|---|
Linux | Linux | SSH |
Linux | Windows | WinRM |
Ansible Components
Component | Function |
---|---|
Executables | Main Ansible CLI tools |
Modules | Python scripts that execute commands on remote servers |
Plugins | Python scripts that enhance Ansible functionality (run on ACS) |
Configuration File | Located at /etc/ansible/ansible.cfg |
Installing Ansible
sudo apt update && sudo apt install -y ansible # For Ubuntu/Debian
sudo yum install -y ansible # For CentOS/RHEL
Running Ansible Ad-Hoc Commands
Ad-hoc commands are used to perform quick tasks without writing playbooks.
Example Commands
# Install Apache on localhost
ansible localhost -m apt -a "name=apache2 state=present"
# Copy an index.html file
ansible localhost -m copy -a "src=index.html dest=/var/www/html/"
# Start Apache service
ansible localhost -m service -a "name=apache2 state=started"
Ansible Inventory
The inventory is a list of target hosts (remote servers) Ansible manages.
Inventory File Examples
Inline Inventory (Command-line)
ansible all -i "52.66.80.208,52.66.80.209" -m apt -a "name=apache2 state=present" -u ubuntu -k -b
File-based Inventory (inventory
file)
[web]
52.66.80.208 ansible_user=rajesh ansible_ssh_pass=rajesh12345
[app]
52.66.80.209 ansible_user=rajesh ansible_ssh_private_key_file=rajesh.pem 52.66.80.210 ansible_user=rajesh ansible_ssh_pass=rajesh12345
[db]
52.66.80.211 ansible_user=rajesh ansible_ssh_pass=rajesh12345
Using Inventory File
# Install Apache on all web servers
ansible web -i inventory -m apt -a "name=apache2 state=present" -u ubuntu -k -b
# Copy index.html to all web servers
ansible web -i inventory -m copy -a "src=index.html dest=/var/www/html/" -u ubuntu -k -b
# Start Apache service on all servers
ansible all -i inventory -m service -a "name=apache2 state=started" -u ubuntu -k -b
Ansible Playbook
A playbook is a YAML file that automates multiple tasks.
Example: Web Server Setup
- name: Install Apache Web Server
hosts: web
become: yes
tasks:
- name: Install Apache2
apt:
name: apache2
state: present
- name: Copy index.html
copy:
src: index.html
dest: /var/www/html/
- name: Restart Apache service
service:
name: apache2
state: restarted
Running the Playbook
ansible-playbook -i inventory webserver.yml
Conclusion
Ansible is a powerful automation tool that simplifies server configuration, application deployment, and IT orchestration. Its agentless architecture, idempotency, and ease of use make it one of the top choices for IT automation.
π‘ Next Steps:
β
Learn about Ansible Variables, Conditionals, and Loops
β
Explore Ansible Roles for reusable automation
β
Deploy real-world Ansible projects
This refined version improves readability, structure, and clarity while preserving all key information. π Let me know if you need further modifications!