# Parameterized handlers

**URL:** <https://forum.ansible.com/t/parameterized-handlers/13382>\
**Category:** Ansible Project\
**Created:** [August 29, 2013, 5:18pm UTC](https://forum.ansible.com/t/parameterized-handlers/13382 "2013-08-29T17:18:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jens\_Hoffrichter](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@Jens\_Hoffrichter](https://forum.ansible.com/u/Jens_Hoffrichter)\
**Post date:** [August 29, 2013, 5:18pm UTC](https://forum.ansible.com/t/parameterized-handlers/13382/1 "2013-08-29T17:18:42Z")

</div>

Hi everyone,

I’m looking into ansible for managing our farm of mail servers, and it looks very, very promising (as having used Puppet for another project, it is great to see the need for node setup greatly reduced 🙂 )

So I’m looking into managing our postfix mailservers with ansible, and we have a lot of postmap type files, which have to be re-hashed on the remote side if a file changes.

I think handlers is the way to go here, but I’m not quite sure how to handle those in a play yet.

As an example, I am creating my relay\_domains file from a template with the following task:

- name: Generate relay\_domains  
template: src=relay\_domains.j2 dest={{ postfix }}/relay\_domains

To get something executed now when the file changes, a notify would have to be used. But I really wouldn’t like adding a static handler for each postmap file I’ll be adding to the config, that is error prone and not really in the spirit of what I try to do here.

Ideally, the task would look something like this:

- name: Generate relay\_domains  
template: src=relay\_domains.j2 dest={{ postfix }}/relay\_domains  
notify:
- postmap {{ postfix }}/relay\_domains

With a handler defined something like this:

- name: postmap $i  
action: postmap hash:$i

Is something like this possible? Or do I need to write a new map module based on the template module? (As most of the postmap files will probably be created from templates)

Or any other solution I could use?

Any help would be greatly appreciated, and thanks in advance!

Best regards,  
Jens

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [August 29, 2013, 6:23pm UTC](https://forum.ansible.com/t/parameterized-handlers/13382/2 "2013-08-29T18:23:38Z")

</div>

I did this by creating a makefile in my postfix configuration (on every  
target) so I have a single notify to postfix-makefile, which then just  
rebuilds for the changed source files my calling postmap or newaliases on  
them.

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [August 29, 2013, 7:23pm UTC](https://forum.ansible.com/t/parameterized-handlers/13382/3 "2013-08-29T19:23:17Z")

</div>

Brian,

you implemented `validate' on template and copy (because of, well, cake 🙂

How about a `compile' or similarly named switch which does  
post-processing when the file has been a) successfully validated and b)  
copied to its destination?

Just a thought.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [August 29, 2013, 7:50pm UTC](https://forum.ansible.com/t/parameterized-handlers/13382/4 "2013-08-29T19:50:15Z")

</div>

It would not be hard, I’ll add it to my list (cookies or cake will accelerate implementation).

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [August 29, 2013, 8:42pm UTC](https://forum.ansible.com/t/parameterized-handlers/13382/5 "2013-08-29T20:42:14Z")

</div>

Actually, I take that back, this will overlap with handlers too much, I am working on a patch to add parameters to handlers when notified which would also solve this issue.

---

<div class="post-metadata">

**Author:** ![Jens\_Hoffrichter](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@Jens\_Hoffrichter](https://forum.ansible.com/u/Jens_Hoffrichter)\
**Post date:** [August 30, 2013, 6:13am UTC](https://forum.ansible.com/t/parameterized-handlers/13382/6 "2013-08-30T06:13:32Z")

</div>

Yes, actually that would be really great, and really what I need.

Do you have a status how far that is along? Can I help? Testing or developing, both would be possible 🙂

Regards,  
Jens

---

<div class="post-metadata">

**Author:** ![Jens\_Hoffrichter](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@Jens\_Hoffrichter](https://forum.ansible.com/u/Jens_Hoffrichter)\
**Post date:** [August 30, 2013, 9:37am UTC](https://forum.ansible.com/t/parameterized-handlers/13382/7 "2013-08-30T09:37:25Z")

</div>

Hi Brian & everyone else,

As I like to continue working on this mailserver playbook, going the Makefile route in the postfix config dir seems to be the way to go (though it is slightly hackish).

As I stated before, I really don’t want to maintain all the possible postmaps in two places (the playbook and a Makefile), so I thought maybe it would be a good idea to render that Makefile from a template as well, showing all the postmaps I have.

So either I need to get a listing (in a template) of all the tasks which define a postmap (possibly with a tag), or I define a structured variable each time I define a postmap, which would also be fine, as then everything is in one place. Then I could iterate over keys/entries in that variable, and generate my Makefile from a template.

Is this possible? I tried various approaches today for this, but I am still struggling to get something working…

Thanks for all the input until now!

Best regards,  
Jens

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [August 30, 2013, 12:40pm UTC](https://forum.ansible.com/t/parameterized-handlers/13382/8 "2013-08-30T12:40:54Z")

</div>

I took a brute force approach on the makefile, it will run postmap on any file in the etc/postfix dir that has changed excluding .conf ones.

Brian Coca
