MOTOSHARE 🚗🏍️
Turning Idle Vehicles into Shared Rides & Earnings
From Idle to Income. From Parked to Purpose.
Earn by Sharing, Ride by Renting.
Where Owners Earn, Riders Move.
Owners Earn. Riders Move. Motoshare Connects.
With Motoshare, every parked vehicle finds a purpose.
Owners earn. Renters ride.
🚀 Everyone wins.
Ansible: A Comprehensive Tutorial
Introduction to Ansible
Ansible is an open-source automation tool used for configuration management, server management, and application deployment. Developed by Red Hat, it is written in Python and is known for its simplicity, agentless architecture, and powerful automation capabilities.
Key Terminology
- Configuration Management Tool: Software that automates the setup, configuration, and maintenance of systems.
- Server Management Tool: A tool that helps administrators manage servers, including installing software, updating configurations, and monitoring services.
- Deployment Tool: Automates the process of delivering applications or updates to multiple servers.
Ansible Editions
| Edition | Interface | Licensing | Description |
|---|---|---|---|
| Ansible | CLI | Free | Command-line interface for automation tasks. |
| Tower | UI | Paid | Enterprise-grade web UI for Ansible. |
| AWX | UI | Free | Open-source version of Ansible Tower. |
Why Use Ansible?
Organizations use Ansible to automate repetitive tasks, manage large numbers of servers, and ensure consistency across environments.
Benefits
- Cost Savings: Automate changes across hundreds of servers, reducing manual effort.
- Time Savings: Execute tasks simultaneously on multiple systems.
- Quality Improvement: Ensure consistent changes and reduce human error.
Use Cases
- IT Operations: Deploy system software, antivirus, policies, and more.
- Development: Deploy applications and manage development environments.
- Resource Management: Automate changes to files, directories, packages, services, users, and groups.
Ansible vs. Other Tools
Ansible is often compared to similar tools:
| Tool | Language | Agentless | Notable Features |
|---|---|---|---|
| Ansible | Python | Yes | Simple YAML syntax, SSH-based |
| Chef | Ruby | No | Chef server/agent model |
| Puppet | Ruby | No | Declarative language, agent-based |
| CFEngine | C | No | Lightweight, policy-based |
| SaltStack | Python | Yes | Event-driven automation |
How Ansible Works
Architecture
- ACS (Ansible Control Server): The machine where Ansible is installed and from which commands are issued.
- ARS (Ansible Remote Server): Target machines managed by Ansible.
Workflow:
textHUMAN → ACS (Control Server) → ARS (Remote Servers)
Platform Support
- Control Server: Typically Linux (64-bit), requires Python.
- Remote Server:
- Linux: Requires Python, communicates via SSH.
- Windows: Requires PowerShell 3.0+ (.NET 4.5+), communicates via WinRM.
Agentless: No need to install any agent on remote servers.
Core Components of Ansible
1. Ansible Executables
/usr/bin/ansible: For ad-hoc commands./usr/bin/ansible-playbook: For running playbooks.
2. Modules
Modules are reusable scripts (mostly in Python) that perform specific tasks on remote servers. Examples:
copy: Copy files.apt: Manage packages on Debian/Ubuntu.yum: Manage packages on Red Hat/CentOS.service: Manage services.
Modules reside on the Control Server but execute on Remote Servers.
3. Plugins
Plugins are Python scripts that extend Ansible’s core functionality and run on the Control Server. Examples include connection plugins, callback plugins, and lookup plugins.
4. Configuration File
- Default:
/etc/ansible/ansible.cfg - Manages global settings for Ansible’s behavior.
5. Inventory File (Host File)
Defines the list of remote servers (ARS) to manage. Can be specified as:
- A simple text file with hostnames/IPs.
- Inline via command line.
- Generated dynamically via scripts.
Example:
text[web]
13.127.150.68
3.110.173.45
[db]
1.2.43.5
1.2.43.6
1.2.43.8
1.2.43.9
6. Playbooks
Playbooks are YAML files that define a series of tasks to be executed on remote servers. They use modules to perform actions.
Playbook Structure
- Play: A mapping of hosts and tasks.
- Tasks: List of actions, each using a module and parameters.
Example:
text---
- name: Update web servers
hosts: web
tasks:
- name: Install Apache in ubuntu
ansible.builtin.apt:
name: "apache2"
state: latest
- name: Copy index.html
ansible.builtin.copy:
src: index.html
dest: /var/www/html/index.html
- name: Starting a Apache Server
ansible.builtin.service:
name: "apache2"
state: started
Writing a Playbook: Step-by-Step
- Define the Project: e.g., Set up a web server.
- List Steps:
- Install Apache2.
- Copy application files.
- Start Apache2 service.
- Choose Modules and Parameters for each step.
Ansible Ad-Hoc Commands
Ad-hoc commands are one-liners used for quick tasks without writing a playbook.
Syntax
textansible <host-pattern> -m <module> -a "<arguments>"
Examples
- Install Apache2 locally: text
ansible localhost -m apt -a "name=apache2 state=latest" - Stop Apache2 service: text
ansible localhost -m service -a "name=apache2 state=stopped" - Start Apache2 service: text
ansible localhost -m service -a "name=apache2 state=started" - Install Apache2 on multiple servers: text
ansible all -i 13.127.150.68,3.110.173.45, -m apt -a "name=apache2 state=latest" - Start Apache2 on remote servers using SSH key: text
ansible all -i 13.127.150.68,3.110.173.45, -m service -a "name=apache2 state=started" -u ubuntu -b --key-file=node.pem - Run with inventory file: text
ansible all -i inventory -m apt -a "name=apache2 state=latest"
Additional Ad-Hoc Command Examples
- Check disk usage: text
ansible all -m shell -a "df -h" - Check uptime: text
ansible all -m command -a "uptime" - Add a user: text
ansible all -m user -a "name=newuser state=present" - Remove a user: text
ansible all -m user -a "name=olduser state=absent" - Create a directory: text
ansible all -m file -a "path=/tmp/testdir state=directory" - Change file permissions: text
ansible all -m file -a "path=/tmp/testdir mode=0755" - Install multiple packages: text
ansible all -m apt -a "name=git,curl,wget state=latest" - Reboot servers: text
ansible all -m reboot - Gather system facts: text
ansible all -m setup - Ping all servers: text
ansible all -m ping
Ansible Playbook Execution
Use the ansible-playbook command to run playbooks:
textansible-playbook -i inventory web.yaml -u ubuntu -b --key-file=node.pem
ansible-playbook -i inventory db.yaml -u ubuntu -b --key-file=node.pem
ansible-playbook -i inventory master.yaml -u ubuntu -b --key-file=node.pem
Including and Reusing Tasks
You can include task files within playbooks for modularity:
text- hosts: web
tasks:
- debug:
msg: task1
- name: Include task list in play
include: web.yaml
- name: Include task list in play
include: db.yaml
Ansible Variables
Variables make playbooks dynamic and reusable.
Types of Variables
- vars: Defined within the playbook.
- vars_files: External YAML files containing variables.
- vars_prompt: Prompt user for input during execution.
Example:
text---
- name: Update web servers
hosts: web
vars:
myname: "Rajeshkumar"
age: "18"
packagename: "apache2"
servicename: "apache2"
vars_files:
- "vars.yaml"
vars_prompt:
- name: "version"
prompt: "Which version Do you want to install?"
private: no
tasks:
- name: Install Apache in ubuntu
ansible.builtin.apt:
name: "{{ packagename }}"
state: latest
- name: Copy index.html
ansible.builtin.copy:
src: index.html
dest: /var/www/html/index.html
- name: Starting a Apache Server
ansible.builtin.service:
name: "{{ servicename }}"
state: started
- name: Print variable
ansible.builtin.debug:
var: myname
- name: Print message
ansible.builtin.debug:
msg: "My Name is {{ myname }} and My age is {{ age }}"
- name: Print version
ansible.builtin.debug:
var: version
- name: Register variable
shell: "find *.txt"
args:
chdir: "/root/ansible"
register: find_output
- debug:
var: find_output
- debug:
var: find_output.stdout_lines
- debug:
var: find_output.stdout_lines[0]
Ansible Roles
Roles are a standardized way to organize playbooks and related files for reuse and sharing.
Role Structure
- tasks/: Main list of tasks.
- vars/: Variables used by the role.
- templates/: Jinja2 templates.
- handlers/: Handlers for service notifications.
- files/: Static files to be used.
Example:
text---
- name: Update web servers
hosts: web
roles:
- web
Run with:
textansible-playbook -i inventory site.yaml -u ubuntu -b --key-file=node.pem
Conclusion
Ansible is a powerful tool for automating IT tasks, managing servers, and deploying applications at scale. Its agentless architecture, simple YAML syntax, and vast ecosystem of modules and plugins make it a top choice for DevOps professionals and system administrators. With the foundational knowledge and examples provided above, you can begin automating your infrastructure efficiently and reliably.