Ansible Automation Ansible Playbooks 2 — Questions and Answers
Question 1: Which directive in a play limits execution to a subset of hosts defined in the inventory?
- hosts
- limit (Correct answer)
- delegate_to
- run_once
Correct answer: limit
The `limit` directive (or `--limit` CLI flag) restricts playbook execution to a subset of the matched hosts.
Question 2: What is the purpose of the `any_errors_fatal: true` play-level option?
- Ignore all task errors
- Abort the entire play if any host fails a task (Correct answer)
- Retry failed tasks automatically
- Skip handlers when errors occur
Correct answer: Abort the entire play if any host fails a task
`any_errors_fatal: true` causes Ansible to immediately stop the play for all hosts if any single host fails a task.
Question 3: Which keyword forces a task to run only once regardless of how many hosts are targeted?
- single_host
- run_once (Correct answer)
- serial: 1
- delegate_once
Correct answer: run_once
`run_once: true` on a task causes it to execute on only one host in the play, typically the first host.
Question 4: In a playbook, what does `gather_subset: ['network']` control?
- Filters which hosts gather facts
- Limits fact gathering to network-related facts only (Correct answer)
- Disables gathering facts entirely
- Specifies the network interface to scan
Correct answer: Limits fact gathering to network-related facts only
`gather_subset` restricts `setup` module output to specific fact categories, reducing overhead.
Question 5: Which statement about `pre_tasks` in a playbook is correct?
- pre_tasks run after roles
- pre_tasks run before roles and tasks sections (Correct answer)
- pre_tasks cannot notify handlers
- pre_tasks are skipped when tags are applied
Correct answer: pre_tasks run before roles and tasks sections
`pre_tasks` execute before the `roles` and `tasks` blocks, allowing setup steps to complete first.
Question 6: What is the effect of setting `strategy: free` in a play?
- Hosts wait for every other host before moving to the next task
- Each host executes tasks as fast as it can without waiting for others (Correct answer)
- Tasks are run in a random order
- Only one host runs at a time
Correct answer: Each host executes tasks as fast as it can without waiting for others
The `free` strategy allows each host to proceed through tasks independently at its own pace.
Question 7: How does `max_fail_percentage: 25` affect a play running against 8 hosts?
- The play fails after 1 host fails
- The play continues until more than 2 hosts fail (Correct answer)
- The play continues until more than 25 hosts fail
- The play ignores all failures up to 25 tasks
Correct answer: The play continues until more than 2 hosts fail
25% of 8 hosts is 2; the play aborts when failures exceed 2 hosts (i.e., 3 or more fail).
Which directive in a play limits execution to a subset of hosts defined in the inventory?