Ansible is an open-source automation tool used for:
✅ Configuration Management (e.g., installing & managing software on servers)
✅ Application Deployment (e.g., deploying a web app on multiple servers)
✅ Orchestration (e.g., managing multi-tier applications like load balancer + DB)
✅ Provisioning (e.g., setting up cloud infrastructure with AWS, Azure, GCP)
🔹 Agentless: No need to install agents on target machines (uses SSH & WinRM)
🔹 Idempotent: Runs multiple times without unwanted changes
🔹 Human-Readable: Uses YAML playbooks
🔹 Cross-Platform: Works on Linux, Windows, macOS, Cloud Servers
# Ubuntu/Debian
sudo apt update
sudo apt install -y ansible
# CentOS/RHEL
sudo yum install -y ansibleansible --versionAn inventory file (/etc/ansible/hosts) tells Ansible where to connect.
Example:
[webservers]
server1 ansible_host=192.168.1.10 ansible_user=ubuntu
server2 ansible_host=192.168.1.11 ansible_user=ubuntu
[dbservers]
db1 ansible_host=192.168.1.20 ansible_user=rootansible all -m ping📌 If successful, you'll see:
server1 | SUCCESS => {"changed": false, "ping": "pong"}
server2 | SUCCESS => {"changed": false, "ping": "pong"}✅ Check disk usage
ansible all -m command -a "df -h"✅ Check system uptime
ansible all -m command -a "uptime"✅ Create a directory on remote hosts
ansible all -m file -a "path=/opt/newdir state=directory"✅ Copy files to remote servers
ansible all -m copy -a "src=/tmp/file.txt dest=/home/ubuntu/file.txt"✅ Install a package (e.g., nginx) on all web servers
ansible webservers -m apt -a "name=nginx state=present" --become✅ Restart a service (e.g., nginx)
ansible webservers -m service -a "name=nginx state=restarted" --become✅ What is a Playbook?
A playbook is a YAML file that contains tasks to automate configuration.
- name: Install and Start Nginx
hosts: webservers
become: yes # Run as sudo
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
- name: Start Nginx
service:
name: nginx
state: started✅ Run the Playbook
ansible-playbook playbook.yml✅ Define Variables in a Playbook
- name: Install a Package with a Variable
hosts: webservers
vars:
package_name: nginx
tasks:
- name: Install Package
apt:
name: "{{ package_name }}"
state: present✅ Use Built-in Ansible Facts
ansible all -m setupExample Fact Usage in Playbook:
- name: Display System Information
hosts: all
tasks:
- debug:
msg: "This server is running {{ ansible_distribution }} {{ ansible_distribution_version }}"✅ Loop Example (Install Multiple Packages)
- name: Install Multiple Packages
hosts: webservers
become: yes
tasks:
- name: Install Packages
apt:
name: "{{ item }}"
state: present
loop:
- nginx
- curl
- unzip✅ Conditional Execution
- name: Restart Nginx Only If Needed
hosts: webservers
become: yes
tasks:
- name: Check if Nginx is Running
shell: pgrep nginx
register: nginx_running
ignore_errors: yes
- name: Restart Nginx
service:
name: nginx
state: restarted
when: nginx_running.rc == 0✅ Generate an Ansible Role Structure
ansible-galaxy init my_role📌 This creates a structured directory like:
my_role/
├── tasks/
│ └── main.yml
├── handlers/
│ └── main.yml
├── templates/
├── files/
├── vars/
│ └── main.yml
├── defaults/
│ └── main.yml
├── meta/
│ └── main.yml
├── README.md
✅ Use Roles in a Playbook
- name: Deploy Web Server
hosts: webservers
roles:
- nginx_role✅ Create an Encrypted File
ansible-vault create secrets.yml✅ Edit an Encrypted File
ansible-vault edit secrets.yml✅ Use Vault in Playbooks
- name: Deploy with Encrypted Secrets
hosts: webservers
vars_files:
- secrets.yml
tasks:
- debug:
msg: "The secret password is {{ secret_password }}"✅ Run Playbook with Vault Password Prompt
ansible-playbook playbook.yml --ask-vault-pass✅ Check Playbook Syntax
ansible-playbook playbook.yml --syntax-check✅ Dry Run (Test Without Executing Changes)
ansible-playbook playbook.yml --check✅ List All Available Modules
ansible-doc -l✅ Get Help for a Specific Module
ansible-doc aptThis Ansible Cheatsheet provides a step-by-step guide from beginner to advanced.
🚀 Next Steps:
✅ Practice with real-world playbooks
✅ Use roles for better structuring
✅ Secure credentials with Ansible Vault
✅ Automate cloud infrastructure with Terraform + Ansible
🔗 Contribute to the Cheatsheet Collection: GitHub Repo
