Ansible Automation Quality Control & Assurance 4 — Questions and Answers
Question 1: Which Ansible strategy minimizes the risk of rolling a bad change across all hosts simultaneously?
- linear
- free
- rolling_update with serial (Correct answer)
- debug
Correct answer: rolling_update with serial
Setting `serial` limits how many hosts are updated in each batch, so failures are detected and stopped before the change propagates to all hosts.
Question 2: What does `any_errors_fatal: true` do in an Ansible play?
- Retries all hosts when any single host fails
- Stops the entire play immediately when any host reports a failure (Correct answer)
- Makes warnings behave like errors
- Converts failed tasks to skipped tasks
Correct answer: Stops the entire play immediately when any host reports a failure
`any_errors_fatal: true` causes Ansible to abort the entire play across all hosts as soon as one host fails, preventing partial deployments.
Question 3: Which Testinfra function would you use in a Molecule verify test to confirm a package is installed?
- host.package('nginx').is_installed (Correct answer)
- host.service('nginx').is_running
- host.file('/etc/nginx').exists
- host.socket('tcp://80').is_listening
Correct answer: host.package('nginx').is_installed
`host.package('nginx').is_installed` returns True if the named package is installed on the system under test.
Question 4: In Ansible, what is `changed_when: false` commonly used for?
- To mark a task as always skipped
- To suppress change reporting for read-only or informational command tasks (Correct answer)
- To force a handler to run even without changes
- To ignore errors on a task
Correct answer: To suppress change reporting for read-only or informational command tasks
`changed_when: false` prevents tasks like information-gathering shell commands from falsely reporting that they changed the system state.
Question 5: Which Ansible module is best suited for verifying that a configuration file contains an expected line after deployment?
- command
- lineinfile with check_mode
- assert with lookup('file') (Correct answer)
- find
Correct answer: assert with lookup('file')
Using `assert` with `lookup('file', ...)` and a string search lets you verify file content declaratively within a quality check task.
Question 6: What is the purpose of the `--diff` flag when running `ansible-playbook`?
- Shows differences between inventory groups
- Displays before-and-after diffs for files and templates that are changed (Correct answer)
- Compares two playbook versions
- Generates a diff of variable precedence
Correct answer: Displays before-and-after diffs for files and templates that are changed
`--diff` outputs a unified diff of file content changes made by modules like `template`, `copy`, and `lineinfile`, helping reviewers understand what changed.
Question 7: Which practice best ensures Ansible roles remain reusable and testable across different environments?
- Hardcoding environment-specific values directly in tasks
- Using defaults/main.yml for all configurable values and overriding per environment (Correct answer)
- Storing all variables in the playbook `vars` block
- Disabling `gather_facts` to speed up execution
Correct answer: Using defaults/main.yml for all configurable values and overriding per environment
Defining defaults in `defaults/main.yml` makes roles self-contained and testable, with environment-specific overrides applied via inventory or extra vars.
Which Ansible strategy minimizes the risk of rolling a bad change across all hosts simultaneously?