Chapter 11: Ansible Introduction
Friends, today we learn Ansible — config management. Raise 20 identical lab servers with playbooks. Inventory, modules, playbooks, idempotence, ad-hoc ping / package / copy / service, a simple role layout, and Bash vs Ansible. Deep Nginx hosting lives in the AWS course — here it is only an example target. Do not fear: one playbook for Nginx + a badge API index.
मित्रांनो, आज आपण Ansible — config management शिकूया. Playbooks ने २० identical lab servers उभारूया. Inventory, modules, playbooks, idempotence, ad-hoc ping / package / copy / service, simple role layout, आणि Bash vs Ansible. Nginx deep hosting AWS course मध्ये आहे — इथे फक्त example target. घाबरू नका: एक playbook ने Nginx + badge API index.
दोस्तों, आज हम Ansible — config management सीखेंगे. Playbooks से २० identical lab servers खड़े करेंगे. Inventory, modules, playbooks, idempotence, ad-hoc ping / package / copy / service, simple role layout, और Bash vs Ansible. Nginx deep hosting AWS course में है — यहाँ सिर्फ example target. घबराओ मत: एक playbook से Nginx + badge API index.
What you will learn in this chapter
- Inventory, modules, playbooks, idempotence
- Ad-hoc ping, package, copy, service
- Simple role folder layout
- Configure a web welcome page without rewriting AWS chapters
- When Ansible beats raw shell scripts
Concepts in this chapter
- 11.1Why Ansible — one playbook for many servers
- 11.2Inventory — who are we configuring?
- 11.3Modules and ad-hoc commands
- 11.4Idempotence — run twice, safe result
- 11.5Playbook — Nginx welcome for badge API
- 11.6Handlers — restart only when needed
- 11.7Simple role folder layout
- 11.8Variables, vault awareness and secrets
- 11.9Ansible vs shell scripts — choose deliberately
- 11.10Troubleshooting table
- 11.11Git layout for Ansible labs
- 11.12Lab — playbook installs Nginx welcome
- 11.13What good looks like after this chapter
Review and practice
- ★11.5b ansible.cfg for friendlier labs
- ★11.9b Check mode and diff
- ★11.9c Tags — run part of a playbook
- ★11.9d Dynamic inventory (awareness)
- ★11.10b Limit and serial (awareness)
The chapter recap is at the end of the last concept page.
Practice Tasks
Suppose we are on the DevOps team at Tata Consultancy Services, the IT consulting company, working from Mumbai. Priya Iyer leads the team, and we look after the HR leave portal. Costs are counted in INR. In the tasks below we do real DevOps jobs on this project using what this lesson teaches. Tata Consultancy Services is a real company, but this project, the people, servers, IP addresses and numbers are made up for practice.
0 / 12
Easy Playbook: install and start nginx
Why: A playbook makes every web server set up the same way.
What to do: Write a playbook (a list with one play) for hosts web with become: true. Tasks: apt with name nginx and state present; service with name nginx, state started, enabled true.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Use "- " for list items and fill each value from the task.
- Check the colons and spacing, then click Check answer.
Easy Run a playbook
Why: Playbooks are the repeatable form of your server setup.
What to do: Run site.yml against inventory.ini.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Easy Ping all hosts
Why: The ping module proves SSH and Python work on every host.
What to do: Using inventory inventory.ini, run the ping module on all hosts.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium YAML inventory
Why: A clear inventory is the map of every server Ansible manages.
What to do: Write a YAML inventory: all.children.web.hosts has web1 and web2; all.children.db.hosts has db1.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Use "- " for list items and fill each value from the task.
- Check the colons and spacing, then click Check answer.
Medium Playbook: create a deploy user
Why: A dedicated user for deploys keeps root logins off.
What to do: Playbook for hosts all with become true. One task user: name priya, groups docker, append true, shell /bin/bash.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Use "- " for list items and fill each value from the task.
- Check the colons and spacing, then click Check answer.
Medium Dry run with diff
Why: Check mode shows changes before they touch production.
What to do: Run site.yml on inventory.ini in check mode and show the diff.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Limit to one group and tag
Why: Limiting the blast radius keeps a change small and safe.
What to do: Run site.yml on inventory.ini only for the group db and only tasks tagged backup.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Install several packages with a loop
Why: A loop keeps one task for many items.
What to do: Playbook for hosts all, become true. One apt task with name "{{ item }}" and state present, with loop over git, curl and htop.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Use "- " for list items and fill each value from the task.
- Check the colons and spacing, then click Check answer.
Medium Ad-hoc command with sudo
Why: Ad-hoc commands fix one thing across many servers fast.
What to do: On the group web from inventory.ini, restart the service nginx using the service module with become.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Give a folder to the web server user
Why: If nginx does not own the upload folder, uploads fail with "permission denied".
What to do: Make user nginx and group nginx the owners of /var/www/tcs/uploads and everything inside.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Click Check answer.
Hard Template with a handler
Why: Handlers restart a service only when its config really changed.
What to do: Playbook for hosts web, become true. Task template: src nginx.conf.j2, dest /etc/nginx/nginx.conf, notify Restart nginx. Handler named Restart nginx using service name nginx state restarted.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Use "- " for list items and fill each value from the task.
- Check the colons and spacing, then click Check answer.
Hard Encrypt a secrets file
Why: Vault lets you commit secrets safely, encrypted.
What to do: Encrypt the file group_vars/all/secrets.yml with Ansible Vault.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Ravi Sir's Cards: 39 revision, interview and comparison cards for this chapter · मराठी cards