# Intended behavior of variable expansion in role handlers?

**URL:** <https://forum.ansible.com/t/intended-behavior-of-variable-expansion-in-role-handlers/27546>\
**Category:** Ansible Project\
**Created:** [May 16, 2018, 5:28pm UTC](https://forum.ansible.com/t/intended-behavior-of-variable-expansion-in-role-handlers/27546 "2018-05-16T17:28:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luciano.cavalheiro](https://avatars.discourse-cdn.com/v4/letter/l/58f4c7/32.png) [@luciano.cavalheiro](https://forum.ansible.com/u/luciano.cavalheiro)\
**Post date:** [May 16, 2018, 5:28pm UTC](https://forum.ansible.com/t/intended-behavior-of-variable-expansion-in-role-handlers/27546/1 "2018-05-16T17:28:54Z")

</div>

Greetings,

I found an unexpected behavior when role variables, with default values defined, are overwritten by role parameters in the playbook. I would like to confirm whether its a bug or working as designed before opening an issue on github.

Here is the test case. I have defined a role to configure multiple instances of a given service. Whenever the instance configuration changes, I need to reload that specific instance informing the path of its configuration file.

roles/some\_role/defaults.yml:  
instance\_name: “default”  
instance\_conf\_file: “/etc/servicename-{{instance\_name}}.conf”  
instance\_param1: “some value”  
instance\_param2: “some other value”

roles/some\_role/tasks.yml:

- template:  
dest: {{instance\_conf\_file}}  
src: some\_role\_conf.j2  
notify:
- reload conf

roles/some\_role/handers.yml:

- name: reload conf  
command: some command to reload {{instance\_name}} {{instance\_conf\_file}}

In the main playbook, I call the role multiple times with different values for instance parameters, like this:

- hosts: all  
roles:

# first instance “default”

- role: some\_role

# second instance

- role: some\_role  
instance\_name: “i2”  
instance\_param1: “abc”

# third instance

- role: some\_role  
instance\_name: “i3”  
instance\_param1: “xyz”

When configuration of either “i2” or 'i3" is modified, notification to “reload conf” actually goes to handler of first instance (variables in “reload conf” are filled with default values).

I would expect that, like for tasks, variables in role/handlers.yml were evaluated considering the current role environment.

Should I open a bug or this is the intended behavior? If this is the intended behavior, are there any other means to pass instance parameters to handlers?

Any help is appreciated.

Best regards,

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 20, 2018, 5:17pm UTC](https://forum.ansible.com/t/intended-behavior-of-variable-expansion-in-role-handlers/27546/2 "2018-05-20T17:17:25Z")

</div>

> roles/some\_role/defaults.yml:  
> instance\_name: "default"  
> instance\_conf\_file: "/etc/servicename-{{instance\_name}}.conf"  
> instance\_param1: "some value"  
> instance\_param2: "some other value"
> 
> roles/some\_role/tasks.yml:  
> - template:  
> &nbsp;&nbsp;&nbsp;&nbsp;dest: {{instance\_conf\_file}}  
> &nbsp;&nbsp;&nbsp;&nbsp;src: some\_role\_conf.j2  
> &nbsp;&nbsp;notify:  
> &nbsp;&nbsp;&nbsp;&nbsp;- reload conf
> 
> roles/some\_role/handers.yml:  
> - name: reload conf  
> &nbsp;&nbsp;command: some command to reload {{instance\_name}} {{instance\_conf\_file}}
> 
> In the main playbook, I call the role multiple times with different values  
> for instance parameters, like this:
> 
> - hosts: all  
> &nbsp;&nbsp;roles:  
> &nbsp;&nbsp;&nbsp;&nbsp;# first instance "default"  
> &nbsp;&nbsp;&nbsp;&nbsp;- role: some\_role
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# second instance  
> &nbsp;&nbsp;&nbsp;&nbsp;- role: some\_role  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_name: "i2"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_param1: "abc"
> 
> &nbsp;&nbsp;&nbsp;# third instance  
> &nbsp;&nbsp;&nbsp;&nbsp;- role: some\_role  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_name: "i3"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_param1: "xyz"
> 
> When configuration of either "i2" or 'i3" is modified, notification to  
> "reload conf" actually goes to handler of first instance (variables in  
> "reload conf" are filled with default values).
> 
> I would expect that, like for tasks, variables in role/handlers.yml were  
> evaluated considering the current role environment.

Handlers only run once no mater how many times they are notified and handlers run is triggered after all the roles is finished.

> Should I open a bug or this is the intended behavior? If this is the  
> intended behavior, are there any other means to pass instance parameters to  
> handlers?

Since handlers is special passing parameters to it is not trivial.

You could instead have "register: result" on the template instead of notify and just have command as a regular task with this added "when: result is changed"
