# Variable to call a handler

**URL:** <https://forum.ansible.com/t/variable-to-call-a-handler/14381>\
**Category:** Ansible Project\
**Created:** [January 20, 2014, 8:33am UTC](https://forum.ansible.com/t/variable-to-call-a-handler/14381 "2014-01-20T08:33:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vlamsdoem](https://avatars.discourse-cdn.com/v4/letter/v/a88e57/32.png) [@vlamsdoem](https://forum.ansible.com/u/vlamsdoem)\
**Post date:** [January 20, 2014, 8:33am UTC](https://forum.ansible.com/t/variable-to-call-a-handler/14381/1 "2014-01-20T08:33:46Z")

</div>

Hello,

I’m trying to use OS based variables for a handler but it doesn’t work.  
Here’s my configuration:

**vars/Debian.yml**

iptables\_service\_name: iptables-persistent  
iptables\_config\_file: /etc/iptables/rules.v4

**roles/common/tasks/main.yml**

- name: test value  
debug: msg=“{{ iptables\_service\_name }} is the name of the service”  
tags:

- test

- name: IPv4 firewall configuration  
template: src=/etc/ansible/roles/common/  
templates/iptables.j2 dest={{ iptables\_config\_file }} owner=root group=root mode=0644  
notify:

- restart {{ iptables\_service\_name }}  
tags:

- test

**roles/common/handlers/main.yml**

- name: restart iptables-persistent  
service: name={{ iptables\_service\_name }} state=restarted

The output when I launch the playbook:

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [172.16.100.211]  
[172.16.100.211](http://172.16.100.211): importing /etc/ansible/vars/Debian.yml

TASK: [common | test value] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [172.16.100.211] =\> {  
“item”: “”,  
“msg”: “iptables-persistent is the name of the service”  
}

TASK: [common | IPv4 firewall configuration] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [172.16.100.211] =\> {“changed”: true, “dest”: “/etc/iptables/rules.v4”, “gid”: 0, “group”: “root”, “item”: “”, “md5sum”: “db47b56591cd1636563d51eeccb4c9e2”, “mode”: “0644”, “owner”: “root”, “size”: 700, “src”: “/root/.ansible/tmp/ansible-1390201288.3-106768121935641/source”, “state”: “file”, “uid”: 0}  
ERROR: change handler (restart {{iptables\_service\_name}}) is not defined

In the task test value, the iptables\_service\_name is OK. But when I try to use it with my handler it doesn’t work.  
Can somebody tell me what I’m doing wrong?

Regards,

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 21, 2014, 12:47am UTC](https://forum.ansible.com/t/variable-to-call-a-handler/14381/2 "2014-01-21T00:47:20Z")

</div>

So is vars/Debian loaded by include\_vars?

If so, that’s the problem, as the name of the facts are a bit more global than something at inventory scope. The same handlers have to be assigned unformally to the task definition, so it can’t notify some on some OS and some others elsewhere.

This can be handled a bit roughly like so:

- module: foo …  
notify:
- debian\_handler
- redhat\_handler

And in the handler

- name: debian\_handler  
service: name=bar state=restarted  
when: ansible\_os\_family == ‘Debian’

etc

A usually better way is just

- name: restart the thing  
service: name={{ service\_name }} state=restarted

And just put the variable in the definition of the task, rather than name.

---

<div class="post-metadata">

**Author:** ![vlamsdoem](https://avatars.discourse-cdn.com/v4/letter/v/a88e57/32.png) [@vlamsdoem](https://forum.ansible.com/u/vlamsdoem)\
**Post date:** [January 21, 2014, 9:05am UTC](https://forum.ansible.com/t/variable-to-call-a-handler/14381/3 "2014-01-21T09:05:34Z")

</div>

> So is vars/Debian loaded by include\_vars?

vars/Debian is included in my playbook:

vars\_files:

- “vars/{{ ansible\_os\_family }}.yml”

> If so, that’s the problem, as the name of the facts are a bit more global than something at inventory scope. The same handlers have to be assigned unformally to the task definition, so it can’t notify some on some OS and some others elsewhere.
> 
> This can be handled a bit roughly like so:
> 
> - module: foo …  
> notify:
> - debian\_handler
> - redhat\_handler
> 
> And in the handler
> 
> - name: debian\_handler  
> service: name=bar state=restarted  
> when: ansible\_os\_family == ‘Debian’
> 
> etc
> 
> A usually better way is just
> 
> - name: restart the thing  
> service: name={{ service\_name }} state=restarted
> 
> And just put the variable in the definition of the task, rather than name.

OK I tried the better way and it’s working now :). I have a common configuration and handler for my 2 OS.

**roles/common/tasks/main.yml:**

- name: IPv4 firewall configuration  
template: src=/etc/ansible/roles/common/templates/iptables.j2 dest={{ iptables\_config\_file }} owner=root group=root mode=0644  
notify:

- restart iptables  
tags:

- test

- name: IPv6 firewall configuration  
template: src=/etc/ansible/roles/common/templates/ip6tables.j2 dest={{ ip6tables\_config\_file }} owner=root group=root mode=0644  
notify:

- restart ip6tables  
tags:

- test

**roles/common/handlers/main.yml**

- name: restart iptables  
service: name={{ iptables\_service\_name }} state=restarted

- name: restart ip6tables  
service: name={{ ip6tables\_service\_name }} state=restarted

Thanks for your help.
