Ravindra BagaleCourses & study guides मराठी

Chapter 91: Ansible Deep Dive

DevOps

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

  1. Make a folder and a virtual environment: mkdir anslab, cd anslab, python3 -m venv .venv, . .venv/bin/activate.
  2. Install Ansible: pip install ansible-core, then ansible --version.
  3. Create inventory.ini with the text in the box below.
  4. Create ansible.cfg with two lines: [defaults] and inventory = inventory.ini.
  5. Run ansible-inventory --graph and check you see web, db and lab.
  6. Run ansible web -m ansible.builtin.ping. Both web hosts should say pong.
  7. Create group_vars/web.yml with app_port: 8080 and site_owner: Priya.
  8. Create templates/app.conf.j2 and site.yml from lesson 7 (with the file task from lesson 4 so out/web1 exists).
  9. Run ansible-playbook site.yml twice. First run: changed=2. Second run: changed=0.
  10. Change app_port to 9090, then run ansible-playbook site.yml --check --diff --limit web1. See the one-line diff.
  11. Run it for real, then open out/web1/app.conf and confirm port = 9090.
  12. Try the rolling play from lesson 12 and count how many times "release started" prints.
  13. When done, deactivate the virtual environment and delete the anslab folder.

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 --diff shows 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.

Did you understand? Let us try it once more.