Ansible Automation Ansible Playbooks 3 — Questions and Answers
Question 1: Which Ansible keyword is used to pass variables to an included or imported playbook file?
- extra_vars
- vars_files
- vars (Correct answer)
- pass_vars
Correct answer: vars
The `vars` keyword under `include_playbook` or `import_playbook` passes variables into the included content.
Question 2: What is the key behavioral difference between `import_tasks` and `include_tasks`?
- import_tasks supports loops; include_tasks does not
- include_tasks is processed at playbook parse time; import_tasks at runtime
- import_tasks is static and processed at parse time; include_tasks is dynamic at runtime (Correct answer)
- They are functionally identical
Correct answer: import_tasks is static and processed at parse time; include_tasks is dynamic at runtime
`import_tasks` is statically pre-processed, meaning tags and conditions apply to all imported tasks; `include_tasks` is dynamic and evaluated at runtime.
Question 3: Which loop keyword is preferred in modern Ansible (2.5+) over `with_items`?
- foreach
- loop (Correct answer)
- iterate
- with_list
Correct answer: loop
The `loop` keyword was introduced in Ansible 2.5 as the standard replacement for most `with_*` loop constructs.
Question 4: What does the `loop_control` option `label` do in a playbook task?
- Names the loop variable
- Customizes the output string shown for each loop iteration (Correct answer)
- Sets the number of parallel loop iterations
- Pauses between loop iterations
Correct answer: Customizes the output string shown for each loop iteration
`loop_control.label` replaces the full loop item in task output with a concise custom string, improving readability.
Question 5: How do you access the current loop index (0-based) inside a `loop` block?
- loop.index
- ansible_loop.index0 (Correct answer)
- loop_var.index
- item.index
Correct answer: ansible_loop.index0
`ansible_loop.index0` provides the zero-based index of the current iteration when `loop_control: extended: yes` is set.
Question 6: In a playbook, which construct allows you to retry a task until a condition is met?
- retry_until
- until with retries and delay (Correct answer)
- loop_until
- wait_for
Correct answer: until with retries and delay
The `until`, `retries`, and `delay` keywords together create a retry loop that keeps running a task until its output meets the specified condition.
Question 7: What is the function of the `flush_handlers` meta task in a playbook?
- Clears all defined handlers from memory
- Immediately runs all pending handlers at that point in the play (Correct answer)
- Resets handler notification state
- Exports handler results to a file
Correct answer: Immediately runs all pending handlers at that point in the play
`meta: flush_handlers` triggers all notified handlers immediately instead of waiting until the end of the play.
Which Ansible keyword is used to pass variables to an included or imported playbook file?