Ansible Automation Ansible Playbooks 4 — Questions and Answers
Question 1: Which Jinja2 filter would you use to convert a list into a comma-separated string inside a playbook template?
- join(',') (Correct answer)
- implode(',')
- concat(',')
- merge(',')
Correct answer: join(',')
The `join` filter concatenates list items with a specified delimiter, producing a single string.
Question 2: What does the `default` Jinja2 filter do in an Ansible variable expression like `{{ my_var | default('N/A') }}`?
- Raises an error if my_var is undefined
- Returns 'N/A' if my_var is undefined or empty (Correct answer)
- Converts my_var to a default type
- Sets my_var permanently to 'N/A'
Correct answer: Returns 'N/A' if my_var is undefined or empty
The `default` filter returns the specified fallback value when the variable is undefined, preventing template errors.
Question 3: Which conditional syntax correctly checks if a variable is defined in an Ansible task?
- when: my_var is defined (Correct answer)
- when: defined(my_var)
- when: my_var != undefined
- when: vars['my_var'] exists
Correct answer: when: my_var is defined
The `is defined` Jinja2 test is the correct Ansible idiom for checking variable existence.
Question 4: What is the role of `set_fact` with `cacheable: true` in a playbook?
- Stores the fact in the play's host_vars directory
- Persists the fact to the Ansible fact cache for future plays (Correct answer)
- Makes the fact available to all hosts
- Converts the fact to a global variable
Correct answer: Persists the fact to the Ansible fact cache for future plays
`cacheable: true` causes `set_fact` to write the variable to the configured fact cache backend so it survives across playbook runs.
Question 5: How do you register a task result and use it in a subsequent conditional?
- Use `capture` to store the result and `if capture` to test it
- Use `register` to store the result and `when` to reference the registered variable (Correct answer)
- Use `output` to store the result and `condition` to test it
- Use `save` and `check` directives
Correct answer: Use `register` to store the result and `when` to reference the registered variable
`register` saves a task's return value to a named variable, which can then be tested with `when` in later tasks.
Question 6: Which key in a registered shell task result indicates the return code of the command?
- exit_code
- return_value
- rc (Correct answer)
- status
Correct answer: rc
The `rc` key in the registered result dictionary holds the numeric return code of the executed command.
Question 7: What happens when `ignore_errors: true` is set on a task that fails?
- The host is removed from subsequent tasks
- The play continues and the task is marked as failed but execution proceeds (Correct answer)
- The task is silently skipped with no record
- Ansible automatically retries the task
Correct answer: The play continues and the task is marked as failed but execution proceeds
`ignore_errors: true` allows the play to continue even if the task fails, but the failure is still recorded in the output and stats.
Which Jinja2 filter would you use to convert a list into a comma-separated string inside a playbook template?