91.15 Lab — a local Ansible practice lab
Story (fictional teaching scenario). Rutuja from Satara and Kenji from Kyoto want to practise without touching any real server. They make a safe lab on one computer. The "servers" web1, web2 and db1 are only names in an inventory that all point back to the same machine with a local connection. Everything is written inside one practice folder. This is exactly how most outputs in this chapter were produced.
Before you start
- A Linux or macOS computer (or WSL on Windows) with Python 3.
- No cloud account, no real servers.
- Everything stays inside a folder called
anslab.
Lab steps
- Make a folder and a virtual environment:
mkdir anslab,cd anslab,python3 -m venv .venv,. .venv/bin/activate. - Install Ansible:
pip install ansible-core, thenansible --version. - Create
inventory.iniwith the text in the box below. - Create
ansible.cfgwith two lines:[defaults]andinventory = inventory.ini. - Run
ansible-inventory --graphand check you seeweb,dbandlab. - Run
ansible web -m ansible.builtin.ping. Both web hosts should saypong. - Create
group_vars/web.ymlwithapp_port: 8080andsite_owner: Priya. - Create
templates/app.conf.j2andsite.ymlfrom lesson 7 (with thefiletask from lesson 4 soout/web1exists). - Run
ansible-playbook site.ymltwice. First run:changed=2. Second run:changed=0. - Change
app_portto9090, then runansible-playbook site.yml --check --diff --limit web1. See the one-line diff. - Run it for real, then open
out/web1/app.confand confirmport = 9090. - Try the rolling play from lesson 12 and count how many times "release started" prints.
- When done, deactivate the virtual environment and delete the
anslabfolder.
Practice inventory
[web]
web1 ansible_host=127.0.0.1
web2 ansible_host=127.0.0.1
[db]
db1 ansible_host=127.0.0.1
[lab:children]
web
db
[lab:vars]
ansible_connection=local
Line by line: All three hosts point at 127.0.0.1, your own computer. ansible_connection=local tells Ansible not to use SSH at all. So nothing outside your folder is touched as long as your playbooks only write inside out/.
Next step with real lab VMs (optional)
When your teacher gives you two disposable lab VMs, replace 127.0.0.1 with their addresses, remove ansible_connection=local, add ansible_user= and your key, and run the same playbooks. Never point a practice playbook at a production server.
What good looks like
- Second run shows
changed=0. --check --diffshows only the line you meant to change.- You can explain why "release started" printed twice.
- No real password or key appears in any file.
Do this lab with the local connection first. Only after it feels easy, try two disposable lab VMs. Never practise on a production server.
हा lab आधी local connection ने करा. सोपं वाटायला लागलं की मगच दोन disposable lab VMs वर करा. Production server वर कधीच practice करू नका.
यह lab पहले local connection से करो. आसान लगने लगे तभी दो disposable lab VMs पर करो. Production server पर कभी practice मत करो.
Did you understand? Let us try it once more.