🏆 Team Leaderboard

Live Ansible progress across all teammates

↻ Loading…

🧠 Knowledge Quiz

Test your Ansible knowledge

☁ Firebase Cross-Device Sync

Sync Ansible lab progress across devices.

Quick setup:
1. console.firebase.google.com → Create project
2. Realtime Database → Create (test mode)
3. Project Settings → Web app → copy config

📄 Ansible Cheatsheet

Essential commands, inventory syntax, playbook patterns & Vault — quick reference

⚡ Ad-Hoc Commands

ansible all -m pingTest connectivity to all hosts
ansible web -m shell -a "uptime"Run shell command on group
ansible all -m setupGather all facts from hosts
ansible all -m copy -a "src=f dest=/tmp/f"Copy file to hosts
ansible all -m yum -a "name=nginx state=present"Install package (RHEL)
ansible all -m apt -a "name=nginx state=present"Install package (Debian)
ansible all -m service -a "name=nginx state=started"Manage service state
ansible web -m command -a "whoami" -bRun as root (-b = become)

📋 Playbook Commands

ansible-playbook site.ymlRun playbook
ansible-playbook site.yml --checkDry-run (no changes made)
ansible-playbook site.yml --diffShow file diffs
ansible-playbook site.yml --tags deployRun only tagged tasks
ansible-playbook site.yml --limit web01Target specific host/group
ansible-playbook site.yml -e "env=prod"Pass extra variable
ansible-playbook site.yml --list-tasksList all tasks without running
ansible-playbook site.yml -vVerbose (-vvv for more detail)

📁 Inventory Formats

# INI inventory
[web]
web01 ansible_host=10.0.0.1
web02 ansible_host=10.0.0.2

[db]
db01 ansible_host=10.0.0.10

[web:vars]
ansible_user=ec2-user
ansible_ssh_private_key_file=~/.ssh/id_rsa

# YAML inventory
all:
  children:
    web:
      hosts:
        web01:
          ansible_host: 10.0.0.1
    db:
      hosts:
        db01:
          ansible_host: 10.0.0.10

📝 Playbook Structure

---
- name: Deploy nginx
  hosts: web
  become: true
  vars:
    nginx_port: 80

  handlers:
    - name: restart nginx
      service: name=nginx state=restarted

  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present
      notify: restart nginx

    - name: Deploy config
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: restart nginx

    - name: Debug var
      debug:
        msg: "Port is {{ nginx_port }}"

🔒 Vault Commands

ansible-vault create secrets.ymlCreate new encrypted file
ansible-vault edit secrets.ymlEdit encrypted file in-place
ansible-vault view secrets.ymlView decrypted content
ansible-vault encrypt vars.ymlEncrypt existing file
ansible-vault decrypt vars.ymlDecrypt file permanently
ansible-vault encrypt_string 'pass' --name 'db_pass'Encrypt inline string
ansible-playbook site.yml --ask-vault-passPrompt for vault password
ansible-playbook site.yml --vault-password-file .vpassRead password from file

🧰 Roles & Galaxy

ansible-galaxy init myroleScaffold role directory structure
ansible-galaxy install geerlingguy.nginxInstall role from Galaxy
ansible-galaxy install -r requirements.ymlInstall from requirements file
ansible-galaxy collection install community.generalInstall collection
ansible-galaxy listList installed roles
molecule init scenarioInit Molecule test scenario
molecule testFull test cycle (create→verify→destroy)

📊 Variable Precedence (low → high)

role defaults/Lowest priority — easy to override
inventory varsHost/group vars from inventory
group_vars/allVars for all groups
group_vars/<group>Group-specific vars
host_vars/<host>Host-specific vars
play vars:Vars section in playbook
register / set_factTask-registered vars
-e "key=val"Extra vars — highest priority

🦰 Jinja2 & Useful Patterns

# Conditionals
when: ansible_os_family == "RedHat"
when: item in groups['web']

# Loops
loop: {{ packages }}
loop: {{ range(1, 4) | list }}

# Register & use result
- command: cat /etc/os-release
  register: os_info
- debug: msg="{{ os_info.stdout }}"

# Error handling
block:
  - name: risky task
    command: /might/fail
rescue:
  - debug: msg="Task failed, recovering"
always:
  - debug: msg="Always runs"

# delegate_to
- name: run on localhost
  command: echo done
  delegate_to: localhost
🏆

Ansible Expert!

You've completed all Ansible labs!

0
Total Labs
0
Completed
0/0
Phase 1-3 Done
0/0
Phase 4-6 Done
Overall Ansible Progress0%