# Does current Ansible support templates macro?

**URL:** <https://forum.ansible.com/t/does-current-ansible-support-templates-macro/31972>\
**Category:** Ansible Project\
**Created:** [January 14, 2020, 9:27pm UTC](https://forum.ansible.com/t/does-current-ansible-support-templates-macro/31972 "2020-01-14T21:27:45Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [January 14, 2020, 9:27pm UTC](https://forum.ansible.com/t/does-current-ansible-support-templates-macro/31972/1 "2020-01-14T21:27:45Z")

</div>

Hello,

I’m working on a role for system SSSD daemon. I found this piece of code online:  
[https://github.com/picotrading/ansible-sssd/blob/master/templates/sssd.conf.j2](https://github.com/picotrading/ansible-sssd/blob/master/templates/sssd.conf.j2)

I have defined my own sssd\_config variable in my role’s defaults directory, so I’d like to use that piece of code. That code is neat. However, I don’t understand what it is doing in line:  
{% from “templates/encoder/macros/ini\_encode\_macro.j2” import ini\_encode with context -%}

Also does current Ansible support templates macro like above?

If it doesn’t, then sssd\_config variable is a large dictionary map, with INI-style different sections. What really needs to happen is based on that variable, change it to use = symbol as delimiter for each INI section. For example:

sssd\_config:  
sssd:  
debug\_level: 1

config\_file\_version: 2  
…

Then the produced sssd.conf file would look like below:

[sssd]  
debug\_level=1  
config\_file\_version=2

Thank you,

Xinhuan Zheng

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 14, 2020, 10:42pm UTC](https://forum.ansible.com/t/does-current-ansible-support-templates-macro/31972/2 "2020-01-14T22:42:10Z")

</div>

This link to the Jinja doc will help you  
[https://jinja.palletsprojects.com/en/2.10.x/templates/#import](https://jinja.palletsprojects.com/en/2.10.x/templates/#import)

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [January 15, 2020, 5:45pm UTC](https://forum.ansible.com/t/does-current-ansible-support-templates-macro/31972/3 "2020-01-15T17:45:41Z")

</div>

**I still cannot figure out how to loop through my variable:**

**sssd\_config:**  
**sssd:**  
**debug\_level: 1**  
**nss:**  
**`reconnection_retries: 3`**  
**`pam:`**  
` **debug_level: 5** `

`Here is my template code:`

`{% for item in sssd_config %}`  
`[{{ item }}]`  
`{% set list = sssd_config[item] %}`  
`{% for i in list %}`  
`{{ i }} =`  
`{% endfor %}`  
`{% endfor %}`

`I cannot figure out what to put after {{ i }}. Please HELP!`

`Thank you,`

`- Xinhuan Zheng`

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 15, 2020, 6:35pm UTC](https://forum.ansible.com/t/does-current-ansible-support-templates-macro/31972/4 "2020-01-15T18:35:44Z")

</div>

Variable 'sssd\_config' is dictionary. It's not possible to iterate  
dictionary. Try for example

&nbsp;&nbsp;{% for item in sssd\_config.items() %}  
&nbsp;&nbsp;{{ item }}  
&nbsp;&nbsp;{% endfor %}

see the 'items' in the created file and fit the template to your needs.

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [January 15, 2020, 6:40pm UTC](https://forum.ansible.com/t/does-current-ansible-support-templates-macro/31972/5 "2020-01-15T18:40:53Z")

</div>

Tried what you said. Here is what {{ item }} look like:

[(u’sssd’, {u’debug\_level’: 5, u’reconnection\_retries’: 3, u’config\_file\_version’: 2, u’sbus\_timeout’: 30})]

[(u’services’, [u’nss’, u’pam’, u’ssh’])]

…

What should I do next?

Thanks again,

- Xinhuan

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 15, 2020, 6:47pm UTC](https://forum.ansible.com/t/does-current-ansible-support-templates-macro/31972/6 "2020-01-15T18:47:23Z")

</div>

Fit the template to your needs. For example the template

&nbsp;&nbsp;&nbsp;&nbsp;{% for item in sssd\_config.items() %}  
&nbsp;&nbsp;&nbsp;&nbsp;[{{ item.0 }}]  
&nbsp;&nbsp;&nbsp;&nbsp;{{ item.1.keys().0 }}={{ item.1.values().0 }}  
&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}

gives

&nbsp;&nbsp;&nbsp;&nbsp;[nss]  
&nbsp;&nbsp;&nbsp;&nbsp;reconnection\_retries=3  
&nbsp;&nbsp;&nbsp;&nbsp;[pam]  
&nbsp;&nbsp;&nbsp;&nbsp;debug\_level=5  
&nbsp;&nbsp;&nbsp;&nbsp;[sssd]  
&nbsp;&nbsp;&nbsp;&nbsp;debug\_level=1

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 15, 2020, 6:56pm UTC](https://forum.ansible.com/t/does-current-ansible-support-templates-macro/31972/7 "2020-01-15T18:56:42Z")

</div>

There might be more items in the configuration sections. The template  
below gives the same result and would include other parameters if present

&nbsp;&nbsp;&nbsp;&nbsp;{% for item in sssd\_config.items() %}  
&nbsp;&nbsp;&nbsp;&nbsp;[{{ item.0 }}]  
&nbsp;&nbsp;&nbsp;&nbsp;{% for iitem in item.1.items() %}  
&nbsp;&nbsp;&nbsp;&nbsp;{{ iitem.0 }}={{ iitem.1 }}  
&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}

---

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [January 15, 2020, 7:57pm UTC](https://forum.ansible.com/t/does-current-ansible-support-templates-macro/31972/8 "2020-01-15T19:57:49Z")

</div>

I tested the solution, it doesn’t work. item.1 becomes:  
{u’id\_provider’: u’local’, u’auth\_provider’: u’local’, u’enumerate’: True}

So I get error there is no keys on {{ item.1.keys().0 }}

- Xinhuan

---

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [January 15, 2020, 8:00pm UTC](https://forum.ansible.com/t/does-current-ansible-support-templates-macro/31972/9 "2020-01-15T20:00:41Z")

</div>

Got this error:

"AnsibleUndefinedVariable: ‘list object’ has no attribute ‘items’ for item.1.items()

- Xinhuan

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 15, 2020, 8:45pm UTC](https://forum.ansible.com/t/does-current-ansible-support-templates-macro/31972/10 "2020-01-15T20:45:30Z")

</div>

Both versions works for me. Double-check the code. The playbook

- hosts: localhost  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;sssd\_config:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sssd:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug\_level: 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nss:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reconnection\_retries: 3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pam:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug\_level: 5  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: template.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: config.ini

with the template

% for item in sssd\_config.items() %}  
[{{ item.0 }}]  
{{ item.1.keys().0 }}={{ item.1.values().0 }}  
{% endfor %}  
# ------------------------------------------  
{% for item in sssd\_config.items() %}  
[{{ item.0 }}]  
{% for iitem in item.1.items() %}  
{{ iitem.0 }}={{ iitem.1 }}  
{% endfor %}  
{% endfor %}

gives

[nss]  
reconnection\_retries=3  
[pam]  
debug\_level=5  
[sssd]  
debug\_level=1  
# ------------------------------------------  
[nss]  
reconnection\_retries=3  
[pam]  
debug\_level=5  
[sssd]  
debug\_level=1

---

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [January 16, 2020, 1:49pm UTC](https://forum.ansible.com/t/does-current-ansible-support-templates-macro/31972/11 "2020-01-16T13:49:07Z")

</div>

Hello,

Your testing looks fine in the test data model. However, the sssd\_config real data model is like below:

sssd\_config:  
sssd:  
debug\_level: 1

additional\_key: additional\_value  
another\_addtional\_key: another\_additional\_value

nss:  
reconnection\_retries: 3

additional\_key: additional\_value  
another\_addtional\_key: another\_additional\_value  
pam:  
debug\_level: 5

additional\_key: additional\_value  
another\_addtional\_key: another\_additional\_value

The addtional\_key and another\_additional\_key isn’t same per section (pam,nss,sssd), and number of additional\_key per section isn’t identical either. So nss section may have 5 key/value pairs, pam may have 6 key/value pairs, and sssd may have only 3 key/value pairs. Each additional\_key is pretty much unique to that section.

Because this is so difficult to manipulate in template (I spend most yesterday to figure it out), I think it is probably better just put INI-style content into template file, then fill in the values from variables that varies for that key/value pair. It makes the work more simpler.

Thank you for providing the test case. I’ll remember this lesson.

- Xinhuan

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 16, 2020, 3:49pm UTC](https://forum.ansible.com/t/does-current-ansible-support-templates-macro/31972/12 "2020-01-16T15:49:20Z")

</div>

It also possible to use module 'ini\_file'  
[https://docs.ansible.com/ansible/latest/modules/ini\_file\_module.html](https://docs.ansible.com/ansible/latest/modules/ini_file_module.html)

With the configuration data transformed to this list

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sssd\_config:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- params:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- additional\_key: additional\_value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- reconnection\_retries: 3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- another\_addtional\_key: another\_additional\_value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;section: nss  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- params:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug\_level: 5  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- another\_addtional\_key: another\_additional\_value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- additional\_key: additional\_value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;section: pam  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- params:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug\_level: 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- another\_addtional\_key: another\_additional\_value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- additional\_key: additional\_value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;section: sssd

the task below

&nbsp;&nbsp;&nbsp;&nbsp;- ini\_file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /scratch/tmp/config.ini  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;section: "{{ item.0.section }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;option: "{{ item.1.keys()|list|first }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value: "{{ item.1.values()|list|first }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_subelements:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ sssd\_config }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- params

gives

$ cat /scratch/tmp/config.ini  
[nss]  
additional\_key = additional\_value  
reconnection\_retries = 3  
another\_addtional\_key = another\_additional\_value  
[pam]  
debug\_level = 5  
another\_addtional\_key = another\_additional\_value  
additional\_key = additional\_value  
[sssd]  
debug\_level = 1  
another\_addtional\_key = another\_additional\_value  
additional\_key = additional\_value

---

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [January 16, 2020, 5:08pm UTC](https://forum.ansible.com/t/does-current-ansible-support-templates-macro/31972/13 "2020-01-16T17:08:28Z")

</div>

Hello Mr. Botka,

This is exactly what I am looking for. It looks so neat with ini\_file module instead of template. I’ll play with it in my tasks.

Thank you very much! 🙂

- Xinhuan
