Ansible Automation Ansible Playbooks 5 — Questions and Answers
Question 1: Which playbook feature allows you to apply a single task to a different host than the one being iterated over?
- run_once
- delegate_to (Correct answer)
- hosts_redirect
- target_host
Correct answer: delegate_to
`delegate_to` runs a task on a specified host while retaining the inventory variables of the current host.
Question 2: When using `delegate_to` with `delegate_facts: true`, what is the result?
- Facts are gathered on the delegated host and stored under the delegated host's name (Correct answer)
- Facts are gathered on the current host using the delegated host's credentials
- Facts are suppressed on both hosts
- Facts are merged between the current and delegated hosts
Correct answer: Facts are gathered on the delegated host and stored under the delegated host's name
`delegate_facts: true` assigns gathered facts to the delegated host's namespace rather than the current host's.
Question 3: What does the `serial` keyword control in an Ansible play?
- The order in which tasks are executed
- How many hosts are processed in each batch during a rolling update (Correct answer)
- The number of forks used by the playbook
- The timeout for each task in seconds
Correct answer: How many hosts are processed in each batch during a rolling update
`serial` defines the batch size for rolling updates, letting you update a percentage or fixed number of hosts at a time.
Question 4: Which playbook directive prevents SSH host key checking for all tasks in a play?
- host_key_checking: false in the play
- Setting ANSIBLE_HOST_KEY_CHECKING=False as an environment variable or in ansible.cfg (Correct answer)
- ssh_strict: false
- skip_ssh_check: true
Correct answer: Setting ANSIBLE_HOST_KEY_CHECKING=False as an environment variable or in ansible.cfg
Host key checking is controlled via the `host_key_checking` setting in ansible.cfg or the `ANSIBLE_HOST_KEY_CHECKING` environment variable, not a play-level directive.
Question 5: In a playbook, what is the purpose of `become_method: sudo`?
- Specifies the user to become
- Specifies the privilege escalation tool to use (Correct answer)
- Specifies the password for escalation
- Enables privilege escalation globally
Correct answer: Specifies the privilege escalation tool to use
`become_method` selects the privilege escalation mechanism; valid options include `sudo`, `su`, `pbrun`, `pfexec`, and others.
Question 6: What does `check_mode: yes` (or `--check` flag) do when running a playbook?
- Validates playbook syntax only
- Runs the playbook in dry-run mode, reporting changes without applying them (Correct answer)
- Checks connectivity to all target hosts
- Verifies that all required variables are set
Correct answer: Runs the playbook in dry-run mode, reporting changes without applying them
Check mode performs a dry run where Ansible reports what would change without making any actual modifications to the target systems.
Question 7: Which directive makes Ansible display task output even for tasks that succeed without changes?
- verbose: true
- no_log: false
- verbosity: 0
- The `-v` flag passed to ansible-playbook (Correct answer)
Correct answer: The `-v` flag passed to ansible-playbook
Passing `-v` (or `-vvv` for more detail) to `ansible-playbook` increases verbosity and shows additional task output including stdout from successful tasks.
Which playbook feature allows you to apply a single task to a different host than the one being iterated over?