Ansible Automation Case Studies & Practical Application 2 — Questions and Answers
Question 1: A company needs to deploy a multi-tier web application across 50 servers where web servers should be configured before app servers. Which Ansible feature best enforces this ordering?
- Using alphabetical inventory group names
- Defining serial: 1 in the playbook
- Using multiple plays in a single playbook with separate host groups (Correct answer)
- Setting gather_facts: false on all plays
Correct answer: Using multiple plays in a single playbook with separate host groups
Multiple plays in one playbook execute sequentially, so placing the web server play before the app server play guarantees ordering.
Question 2: During a rolling update, one server fails its health check after the new app version is deployed. Which Ansible mechanism automatically stops the rolling update and prevents further hosts from being updated?
- The ignore_errors directive
- The max_fail_percentage option combined with serial (Correct answer)
- Setting force_handlers: true
- Using the rescue block in tasks
Correct answer: The max_fail_percentage option combined with serial
max_fail_percentage halts the play when the percentage of failed hosts exceeds the defined threshold, preventing the bad release from spreading.
Question 3: A security team requires that database passwords used in playbooks are never stored in plain text in version control. What is the recommended Ansible solution?
- Store passwords in environment variables only
- Use Ansible Vault to encrypt sensitive variables (Correct answer)
- Base64-encode all passwords in group_vars
- Use a separate untracked vars file with no_log: true
Correct answer: Use Ansible Vault to encrypt sensitive variables
Ansible Vault encrypts sensitive data at rest so encrypted files can safely be committed to version control.
Question 4: An ops engineer wants to test a new playbook against production inventory without making any actual changes. Which command-line flag achieves this?
- --syntax-check
- --list-tasks
- --check (Correct answer)
- --diff
Correct answer: --check
The --check flag runs Ansible in dry-run mode, simulating changes without applying them to target hosts.
Question 5: A team manages 200 servers split across prod and staging environments in different data centers. They need per-environment variable overrides. What is the best practice for organizing this?
- Create separate playbooks for each environment
- Use group_vars directories named after each inventory group (Correct answer)
- Pass all variables via -e on the command line
- Hardcode environment values inside each role's defaults/main.yml
Correct answer: Use group_vars directories named after each inventory group
group_vars directories allow environment-specific variables to be automatically loaded based on which inventory group a host belongs to.
Question 6: A playbook task must only run when a specific file exists on the remote host. Which approach correctly implements this conditional?
- Using when: ansible_facts['file'] is defined
- Using stat module to check the file, then when: stat_result.stat.exists (Correct answer)
- Using the file module with state: exists
- Setting creates: /path/to/file in the task args
Correct answer: Using stat module to check the file, then when: stat_result.stat.exists
The stat module registers file metadata, and the .stat.exists attribute is then evaluated in a when condition.
Question 7: A SysAdmin needs to update the same nginx.conf template on 300 servers but only restart nginx if the config actually changed. Which Ansible feature handles this automatically?
- Setting changed_when: false on the template task
- Using a handler that is notified by the template task (Correct answer)
- Adding a debug task after the template task
- Using register to capture output and then shell: systemctl restart nginx
Correct answer: Using a handler that is notified by the template task
Handlers are triggered only when a task reports a change, making them ideal for service restarts after config updates.
A company needs to deploy a multi-tier web application across 50 servers where web servers should be configured before app servers.
Which Ansible feature best enforces this ordering?