A complete, hands-on Ansible lab guide for DevOps students. Learn infrastructure automation from zero to production-ready โ with real exercises, code examples, and best practices.
A structured learning path from Ansible fundamentals to production-grade infrastructure automation.
Work through these lab exercises in GitHub Codespaces or any Linux environment. Each lab builds on the last.
---
- name: Install and start Nginx
hosts: localhost
become: yes
tasks:
- name: Install nginx
apt:
name: nginx
state: present
- name: Start nginx service
service:
name: nginx
state: started
enabled: yeswebserver.yml and paste the playbook aboveansible-playbook webserver.yml โ note changed=1# Create the role structure
mkdir -p roles/{webserver,mysql,app}/tasks
mkdir -p roles/mysql/vars
# Create the main playbook
touch playbook.yml inventory.ini
# Verify structure
tree roles/main.ymlplaybook.ymlinclude_vars.# Create secrets file cat > roles/mysql/vars/secrets.yml << EOF db_name: appdb db_user: appuser db_password: SuperSecret123! EOF # Encrypt it ansible-vault encrypt roles/mysql/vars/secrets.yml # Run playbook with vault ansible-playbook -i inventory.ini playbook.yml \ --ask-vault-pass
secrets.yml with plain text credentialsansible-vault encrypt on the fileinclude_vars: vars/secrets.yml to your MySQL tasks--ask-vault-pass and confirm it worksrequirements.yml file, install a Galaxy role from Jeff Geerling, and use it in your playbook instead of your custom Nginx role.# requirements.yml roles: - name: geerlingguy.nginx - name: geerlingguy.mysql
# Install all roles at once ansible-galaxy install -r requirements.yml
requirements.yml with the roles aboveansible-galaxy install -r requirements.ymlThe most important Ansible commands you'll use every day.