Ansible Automation Ansible Handlers and Notifications 2 — Questions and Answers
Question 1: A handler is defined with the name 'Restart Nginx'. Which task directive triggers it?
- notify: Restart Nginx (Correct answer)
- trigger: Restart Nginx
- run_handler: Restart Nginx
- call: Restart Nginx
Correct answer: notify: Restart Nginx
The 'notify' directive with the exact handler name queues that handler for execution at the end of the play if the task changed.
To trigger a handler, use 'notify' in a task with the exact name of the handler (case-sensitive string match): - name: Deploy nginx.conf template: src: nginx.conf.j2 dest: /etc/nginx/nginx.conf notify: Restart Nginx The handler named 'Restart Nginx' will be queued if and only if the template task reports 'changed'. The matching is by name string, so spelling and case must be exact. Using 'listen' topics provides a looser coupling alternative.
Question 2: In Ansible roles, where should handler files be placed?
- In the 'handlers/' directory of the role, typically in a file named main.yml (Correct answer)
- In the 'tasks/' directory of the role alongside task files
- In the 'vars/' directory of the role
- In the 'defaults/' directory of the role
Correct answer: In the 'handlers/' directory of the role, typically in a file named main.yml
Role handlers are placed in the 'handlers/' directory, with Ansible automatically loading 'handlers/main.yml'.
Ansible roles follow a strict directory convention. Handlers belong in the 'handlers/' directory, and Ansible automatically loads 'handlers/main.yml'. The role structure looks like: myrole/ tasks/main.yml handlers/main.yml templates/ files/ vars/main.yml defaults/main.yml Handlers defined in a role are scoped to that role by default but can be made globally available. When tasks in one role notify a handler, the handler must be in the same role's handlers directory unless 'dependencies' are configured.
Question 3: What happens if a handler is notified but the task that notified it is skipped (not run)?
- The handler is not triggered because a skipped task is not considered changed (Correct answer)
- The handler still triggers since it was previously defined
- The handler triggers but only if 'force_handlers' is set
- The handler triggers the next time the playbook runs
Correct answer: The handler is not triggered because a skipped task is not considered changed
Handlers are only triggered when the notifying task actually runs and reports 'changed'. A skipped task never changes, so the handler is not notified.
In Ansible, a task can be 'skipped' (not executed, typically due to a 'when' condition being false), 'ok' (ran but no change made), or 'changed' (ran and modified something). Handlers are only queued when a task status is 'changed'. A skipped task does not trigger any notify directives. This is by design—if a configuration file was not deployed (task skipped), there's no reason to restart the associated service.
Question 4: Which Ansible command-line option allows you to run only the handlers of a playbook without running the tasks?
- There is no such option—handlers can only be triggered by task notifications, not run independently (Correct answer)
- --handlers-only
- --skip-tasks
- --run-handlers
Correct answer: There is no such option—handlers can only be triggered by task notifications, not run independently
Ansible does not have a built-in option to run handlers independently. Handlers are always triggered by task notifications.
Ansible handlers cannot be executed directly from the command line without their triggering tasks. They are designed as reactive components that only run in response to changes. To test a handler independently, you can convert it to a regular task temporarily, or write a separate playbook that explicitly calls the desired action. The '--tags' option can be used with task tags to limit which tasks run, which indirectly controls which handlers are notified.
Question 5: How can you notify multiple handlers from a single task in Ansible?
- Use 'notify' as a list with multiple handler names (Correct answer)
- Use multiple 'notify' directives in the same task
- Use 'notify_all' with a comma-separated string
- Handlers cannot be chained from a single task
Correct answer: Use 'notify' as a list with multiple handler names
The 'notify' directive accepts a list of handler names, allowing a single task to queue multiple handlers.
A single task can notify multiple handlers by providing a YAML list: - name: Deploy application config template: src: app.conf.j2 dest: /etc/app/app.conf notify: - Restart Application - Reload Nginx - Clear Application Cache All listed handlers will be queued if the task reports a change. Each handler still deduplicates independently—if another task also notifies 'Restart Application', it still runs only once. This pattern is useful when a single change has multiple downstream effects.
Question 6: What is the scope of handler visibility when working with Ansible roles?
- Handlers defined in a role are globally available to all roles in the play, which can cause naming conflicts (Correct answer)
- Handlers defined in a role are strictly private and cannot be called from outside that role
- Handlers defined in a role are only available within that specific role's tasks
- Handler scope depends on the Ansible version being used
Correct answer: Handlers defined in a role are globally available to all roles in the play, which can cause naming conflicts
Role handlers are globally scoped within a play, meaning any task in any role can notify them, but this also risks naming conflicts between roles.
Ansible role handlers have global scope within a play. Once a role is included, its handlers become available to all tasks in the play, including tasks from other roles. This means: 1. Tasks in role A can notify handlers defined in role B (if role B is also included) 2. Handler names must be unique across all roles in a play to avoid conflicts 3. Using prefixes like 'myrole : restart service' helps prevent naming collisions The '{{role_name}} : handler_name' convention is recommended for namespacing role handlers.
A handler is defined with the name 'Restart Nginx'.
Which task directive triggers it?