|
| 1 | +# 📜 **Ansible Cheatsheet** |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +## **🔹 Introduction to Ansible** |
| 6 | + |
| 7 | +### ✅ What is Ansible? |
| 8 | + |
| 9 | +Ansible is an **open-source automation tool** used for: |
| 10 | +✅ **Configuration Management** (e.g., installing & managing software on servers) |
| 11 | +✅ **Application Deployment** (e.g., deploying a web app on multiple servers) |
| 12 | +✅ **Orchestration** (e.g., managing multi-tier applications like load balancer + DB) |
| 13 | +✅ **Provisioning** (e.g., setting up cloud infrastructure with AWS, Azure, GCP) |
| 14 | + |
| 15 | +### ✅ Why Use Ansible? |
| 16 | + |
| 17 | +🔹 **Agentless:** No need to install agents on target machines (uses SSH & WinRM) |
| 18 | +🔹 **Idempotent:** Runs multiple times without unwanted changes |
| 19 | +🔹 **Human-Readable:** Uses YAML playbooks |
| 20 | +🔹 **Cross-Platform:** Works on **Linux, Windows, macOS, Cloud Servers** |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## **🛠️ 1. Installing & Setting Up Ansible** |
| 25 | + |
| 26 | +### ✅ Installing Ansible on Linux |
| 27 | + |
| 28 | +```bash |
| 29 | +# Ubuntu/Debian |
| 30 | +sudo apt update |
| 31 | +sudo apt install -y ansible |
| 32 | + |
| 33 | +# CentOS/RHEL |
| 34 | +sudo yum install -y ansible |
| 35 | +``` |
| 36 | + |
| 37 | +### ✅ Checking Installation |
| 38 | + |
| 39 | +```bash |
| 40 | +ansible --version |
| 41 | +``` |
| 42 | + |
| 43 | +### ✅ Setting Up an Inventory File |
| 44 | + |
| 45 | +An **inventory file** (`/etc/ansible/hosts`) tells Ansible where to connect. |
| 46 | +Example: |
| 47 | + |
| 48 | +```ini |
| 49 | +[webservers] |
| 50 | +server1 ansible_host=192.168.1.10 ansible_user=ubuntu |
| 51 | +server2 ansible_host=192.168.1.11 ansible_user=ubuntu |
| 52 | + |
| 53 | +[dbservers] |
| 54 | +db1 ansible_host=192.168.1.20 ansible_user=root |
| 55 | +``` |
| 56 | + |
| 57 | +### ✅ Testing Connectivity with `ping` |
| 58 | + |
| 59 | +```bash |
| 60 | +ansible all -m ping |
| 61 | +``` |
| 62 | + |
| 63 | +📌 If successful, you'll see: |
| 64 | + |
| 65 | +```bash |
| 66 | +server1 | SUCCESS => {"changed": false, "ping": "pong"} |
| 67 | +server2 | SUCCESS => {"changed": false, "ping": "pong"} |
| 68 | +``` |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +## **🚀 2. Running Ad-Hoc Commands (Quick Tasks Without a Playbook)** |
| 73 | + |
| 74 | +✅ **Check disk usage** |
| 75 | + |
| 76 | +```bash |
| 77 | +ansible all -m command -a "df -h" |
| 78 | +``` |
| 79 | + |
| 80 | +✅ **Check system uptime** |
| 81 | + |
| 82 | +```bash |
| 83 | +ansible all -m command -a "uptime" |
| 84 | +``` |
| 85 | + |
| 86 | +✅ **Create a directory on remote hosts** |
| 87 | + |
| 88 | +```bash |
| 89 | +ansible all -m file -a "path=/opt/newdir state=directory" |
| 90 | +``` |
| 91 | + |
| 92 | +✅ **Copy files to remote servers** |
| 93 | + |
| 94 | +```bash |
| 95 | +ansible all -m copy -a "src=/tmp/file.txt dest=/home/ubuntu/file.txt" |
| 96 | +``` |
| 97 | + |
| 98 | +✅ **Install a package (e.g., nginx) on all web servers** |
| 99 | + |
| 100 | +```bash |
| 101 | +ansible webservers -m apt -a "name=nginx state=present" --become |
| 102 | +``` |
| 103 | + |
| 104 | +✅ **Restart a service (e.g., nginx)** |
| 105 | + |
| 106 | +```bash |
| 107 | +ansible webservers -m service -a "name=nginx state=restarted" --become |
| 108 | +``` |
| 109 | + |
| 110 | +--- |
| 111 | + |
| 112 | +## **📜 3. Writing Ansible Playbooks (Automation Scripts)** |
| 113 | + |
| 114 | +✅ **What is a Playbook?** |
| 115 | +A **playbook** is a YAML file that contains tasks to **automate configuration**. |
| 116 | + |
| 117 | +### **🔹 Basic Playbook Example** |
| 118 | + |
| 119 | +```yaml |
| 120 | +- name: Install and Start Nginx |
| 121 | + hosts: webservers |
| 122 | + become: yes # Run as sudo |
| 123 | + tasks: |
| 124 | + - name: Install Nginx |
| 125 | + apt: |
| 126 | + name: nginx |
| 127 | + state: present |
| 128 | + |
| 129 | + - name: Start Nginx |
| 130 | + service: |
| 131 | + name: nginx |
| 132 | + state: started |
| 133 | +``` |
| 134 | +
|
| 135 | +✅ **Run the Playbook** |
| 136 | +
|
| 137 | +```bash |
| 138 | +ansible-playbook playbook.yml |
| 139 | +``` |
| 140 | + |
| 141 | +--- |
| 142 | + |
| 143 | +## **🔹 4. Using Variables in Ansible** |
| 144 | + |
| 145 | +✅ **Define Variables in a Playbook** |
| 146 | + |
| 147 | +```yaml |
| 148 | +- name: Install a Package with a Variable |
| 149 | + hosts: webservers |
| 150 | + vars: |
| 151 | + package_name: nginx |
| 152 | + tasks: |
| 153 | + - name: Install Package |
| 154 | + apt: |
| 155 | + name: "{{ package_name }}" |
| 156 | + state: present |
| 157 | +``` |
| 158 | +
|
| 159 | +✅ **Use Built-in Ansible Facts** |
| 160 | +
|
| 161 | +```bash |
| 162 | +ansible all -m setup |
| 163 | +``` |
| 164 | + |
| 165 | +Example Fact Usage in Playbook: |
| 166 | + |
| 167 | +```yaml |
| 168 | +- name: Display System Information |
| 169 | + hosts: all |
| 170 | + tasks: |
| 171 | + - debug: |
| 172 | + msg: "This server is running {{ ansible_distribution }} {{ ansible_distribution_version }}" |
| 173 | +``` |
| 174 | +
|
| 175 | +--- |
| 176 | +
|
| 177 | +## **🔹 5. Loops & Conditionals** |
| 178 | +
|
| 179 | +✅ **Loop Example (Install Multiple Packages)** |
| 180 | +
|
| 181 | +```yaml |
| 182 | +- name: Install Multiple Packages |
| 183 | + hosts: webservers |
| 184 | + become: yes |
| 185 | + tasks: |
| 186 | + - name: Install Packages |
| 187 | + apt: |
| 188 | + name: "{{ item }}" |
| 189 | + state: present |
| 190 | + loop: |
| 191 | + - nginx |
| 192 | + - curl |
| 193 | + - unzip |
| 194 | +``` |
| 195 | +
|
| 196 | +✅ **Conditional Execution** |
| 197 | +
|
| 198 | +```yaml |
| 199 | +- name: Restart Nginx Only If Needed |
| 200 | + hosts: webservers |
| 201 | + become: yes |
| 202 | + tasks: |
| 203 | + - name: Check if Nginx is Running |
| 204 | + shell: pgrep nginx |
| 205 | + register: nginx_running |
| 206 | + ignore_errors: yes |
| 207 | + |
| 208 | + - name: Restart Nginx |
| 209 | + service: |
| 210 | + name: nginx |
| 211 | + state: restarted |
| 212 | + when: nginx_running.rc == 0 |
| 213 | +``` |
| 214 | +
|
| 215 | +--- |
| 216 | +
|
| 217 | +## **📂 6. Ansible Roles (Best Practices for Large Projects)** |
| 218 | +
|
| 219 | +✅ **Generate an Ansible Role Structure** |
| 220 | +
|
| 221 | +```bash |
| 222 | +ansible-galaxy init my_role |
| 223 | +``` |
| 224 | + |
| 225 | +📌 This creates a structured directory like: |
| 226 | + |
| 227 | +```plaintext |
| 228 | +my_role/ |
| 229 | +├── tasks/ |
| 230 | +│ └── main.yml |
| 231 | +├── handlers/ |
| 232 | +│ └── main.yml |
| 233 | +├── templates/ |
| 234 | +├── files/ |
| 235 | +├── vars/ |
| 236 | +│ └── main.yml |
| 237 | +├── defaults/ |
| 238 | +│ └── main.yml |
| 239 | +├── meta/ |
| 240 | +│ └── main.yml |
| 241 | +├── README.md |
| 242 | +``` |
| 243 | + |
| 244 | +✅ **Use Roles in a Playbook** |
| 245 | + |
| 246 | +```yaml |
| 247 | +- name: Deploy Web Server |
| 248 | + hosts: webservers |
| 249 | + roles: |
| 250 | + - nginx_role |
| 251 | +``` |
| 252 | +
|
| 253 | +--- |
| 254 | +
|
| 255 | +## **🔐 7. Ansible Vault (Encrypting Secrets)** |
| 256 | +
|
| 257 | +✅ **Create an Encrypted File** |
| 258 | +
|
| 259 | +```bash |
| 260 | +ansible-vault create secrets.yml |
| 261 | +``` |
| 262 | + |
| 263 | +✅ **Edit an Encrypted File** |
| 264 | + |
| 265 | +```bash |
| 266 | +ansible-vault edit secrets.yml |
| 267 | +``` |
| 268 | + |
| 269 | +✅ **Use Vault in Playbooks** |
| 270 | + |
| 271 | +```yaml |
| 272 | +- name: Deploy with Encrypted Secrets |
| 273 | + hosts: webservers |
| 274 | + vars_files: |
| 275 | + - secrets.yml |
| 276 | + tasks: |
| 277 | + - debug: |
| 278 | + msg: "The secret password is {{ secret_password }}" |
| 279 | +``` |
| 280 | +
|
| 281 | +✅ **Run Playbook with Vault Password Prompt** |
| 282 | +
|
| 283 | +```bash |
| 284 | +ansible-playbook playbook.yml --ask-vault-pass |
| 285 | +``` |
| 286 | + |
| 287 | +--- |
| 288 | + |
| 289 | +## **🎯 8. Useful Ansible Commands** |
| 290 | + |
| 291 | +✅ **Check Playbook Syntax** |
| 292 | + |
| 293 | +```bash |
| 294 | +ansible-playbook playbook.yml --syntax-check |
| 295 | +``` |
| 296 | + |
| 297 | +✅ **Dry Run (Test Without Executing Changes)** |
| 298 | + |
| 299 | +```bash |
| 300 | +ansible-playbook playbook.yml --check |
| 301 | +``` |
| 302 | + |
| 303 | +✅ **List All Available Modules** |
| 304 | + |
| 305 | +```bash |
| 306 | +ansible-doc -l |
| 307 | +``` |
| 308 | + |
| 309 | +✅ **Get Help for a Specific Module** |
| 310 | + |
| 311 | +```bash |
| 312 | +ansible-doc apt |
| 313 | +``` |
| 314 | + |
| 315 | +--- |
| 316 | + |
| 317 | +## 🎯 **Conclusion** |
| 318 | + |
| 319 | +This **Ansible Cheatsheet** provides a **step-by-step guide** from **beginner to advanced**. |
| 320 | + |
| 321 | +🚀 **Next Steps:** |
| 322 | +✅ **Practice with real-world playbooks** |
| 323 | +✅ **Use roles for better structuring** |
| 324 | +✅ **Secure credentials with Ansible Vault** |
| 325 | +✅ **Automate cloud infrastructure with Terraform + Ansible** |
| 326 | + |
| 327 | +🔗 **Contribute to the Cheatsheet Collection:** [GitHub Repo](https://github.com/NotHarshhaa/devops-cheatsheet) |
0 commit comments