# How pass an array of dictionaries to my custom module elegantly?

**URL:** <https://forum.ansible.com/t/how-pass-an-array-of-dictionaries-to-my-custom-module-elegantly/25906>\
**Category:** Ansible Project\
**Created:** [November 2, 2017, 6:55pm UTC](https://forum.ansible.com/t/how-pass-an-array-of-dictionaries-to-my-custom-module-elegantly/25906 "2017-11-02T18:55:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [November 2, 2017, 6:55pm UTC](https://forum.ansible.com/t/how-pass-an-array-of-dictionaries-to-my-custom-module-elegantly/25906/1 "2017-11-02T18:55:13Z")

</div>

I’m not trying to re-invent the “replace” module that Ansible has.

I’m trying to write a (my FIRST) custom module to encapsulate the repetitive Linux source build process, namely,

- run ./configure $CONF\_OPTS
- run make
- run make $TARGETS

However, i have an outlying case where I have to modify Makefile (ugh) that ./configure produces (not my idea), so my steps are now

- run ./configure $CONF\_OPTS
- modify Makefile
- run make
- run make $TARGETS

If I was writing above steps using generic Ansible modules, I’d of course use the replace module, like so, correct? This will change all the instances of “/usr/local” into “/usr/my/local” and “/usr/perl” into “/usr/my/perl” in my Makefile

`

- name: Update Makefile (ugh)  
replace:  
dest: “/path\_to/Makefile”  
regexp: “{{item.old\_str}}”  
replace: “{{item.new\_str}}”  
with\_items:
- {old\_str: “/usr/local”, new\_str: “/usr/my/local”}
- {old\_str: “/local/perl”, new\_str: “/usr/my/perl”}

`

I want my module call to look like this. Or is there a better way to represent the “modify\_regex” parameters?

`

- name: Run configure, make, and make targets  
config\_make\_targets:  
path: /path\_to\_source  
modify\_file: “Makefile”  
modify\_regex: [  
{old\_str: “/usr/local”, new\_str: “/usr/my/local”},  
{old\_str: “/local/perl”, new\_str: “/usr/my/perl”}  
]

`

Thanks!

---

<div class="post-metadata">

**Author:** ![tkuratom](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@tkuratom](https://forum.ansible.com/u/tkuratom)\
**Post date:** [November 3, 2017, 1:24pm UTC](https://forum.ansible.com/t/how-pass-an-array-of-dictionaries-to-my-custom-module-elegantly/25906/2 "2017-11-03T13:24:02Z")

</div>

You could probably do:

{“/usr/local”: “/usr/my/local”,  
“/local/perl”: “/usr/my/perl”}

That will work as long as order does not matter (if the replacements do not cause there to be more or less of the replacer substrings). If you can’t guarantee that, then the list is better, although you can do single element dicts or two element lists inside:

[{“/usr/local”: “/usr/my/local”},  
{“/local/perl”: “/usr/my/perl”}]

[[“/usr/local”, “/usr/my/local”],  
[“/local/perl”, “/usr/my/perl”]]
