# Flatten a dict-list in a template

**URL:** <https://forum.ansible.com/t/flatten-a-dict-list-in-a-template/15288>\
**Category:** Ansible Project\
**Created:** [May 21, 2014, 9:59am UTC](https://forum.ansible.com/t/flatten-a-dict-list-in-a-template/15288 "2014-05-21T09:59:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joachim\_Friberg](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@Joachim\_Friberg](https://forum.ansible.com/u/Joachim_Friberg)\
**Post date:** [May 21, 2014, 9:59am UTC](https://forum.ansible.com/t/flatten-a-dict-list-in-a-template/15288/1 "2014-05-21T09:59:14Z")

</div>

So I’m trying to setup a shinken-enviroment and it has a similar syntax to Nagios.  
In the contactgroups.cfg I need to put all my admins on one line.  
Is there anyway to take entries from a list of dicts and flatten them to one line?  
Below is my test-example

`

---

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [May 21, 2014, 11:30am UTC](https://forum.ansible.com/t/flatten-a-dict-list-in-a-template/15288/2 "2014-05-21T11:30:18Z")

</div>

> So I'm trying to setup a shinken-enviroment and it has a similar  
> syntax to Nagios.  
> In the contactgroups.cfg I need to put all my admins on one line.  
> Is there anyway to take entries from a list of dicts and flatten them  
> to one line?  
> Below is my test-example  
> \>  
> ---  
> &nbsp;&nbsp;- hosts: localhost  
> &nbsp;&nbsp;&nbsp;&nbsp;remote\_user: root  
> &nbsp;&nbsp;&nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;users:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Andrew  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Doug  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Paul  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: ipphone  
> &nbsp;&nbsp;&nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=users  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: test  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;template: src=../templates/test.j2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest=/home/root/test.cfg
> 
> \>  
> The template:  
> \>  
> define contactgroup{  
> &nbsp;&nbsp;&nbsp;&nbsp;contactgroup\_name Admins  
> &nbsp;&nbsp;&nbsp;&nbsp;alias Seamless\_internal\_admins  
> &nbsp;&nbsp;&nbsp;&nbsp;members {{ users.name }}  
> }  
> \>
> 
> result:  
> \>  
> define contactgroup{  
> &nbsp;&nbsp;&nbsp;&nbsp;contactgroup\_name JAPS  
> &nbsp;&nbsp;&nbsp;&nbsp;alias Seamless\_internal\_admins  
> &nbsp;&nbsp;&nbsp;&nbsp;members Doug  
> }  
> \>
> 
> Desired result:  
> \>  
> define contactgroup{  
> &nbsp;&nbsp;&nbsp;&nbsp;contactgroup\_name JAPS  
> &nbsp;&nbsp;&nbsp;&nbsp;alias Seamless\_internal\_admins  
> &nbsp;&nbsp;&nbsp;&nbsp;members Andrew,Doug,Paul,ipphone  
> }  
> \>
> 
> --  
> You received this message because you are subscribed to the Google  
> Groups "Ansible Project" group.  
> To unsubscribe from this group and stop receiving emails from it, send  
> an email to ansible-project+unsubscribe@googlegroups.com  
> \<mailto:ansible-project+unsubscribe@googlegroups.com\>.  
> To post to this group, send email to ansible-project@googlegroups.com  
> \<mailto:ansible-project@googlegroups.com\>.  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/ansible-project/2ce4fa5b-80e9-4e33-915b-7f1361027eb2%40googlegroups.com](https://groups.google.com/d/msgid/ansible-project/2ce4fa5b-80e9-4e33-915b-7f1361027eb2%40googlegroups.com)  
> \<[https://groups.google.com/d/msgid/ansible-project/2ce4fa5b-80e9-4e33-915b-7f1361027eb2%40googlegroups.com?utm\_medium=email&utm\_source=footer&gt;\](https://groups.google.com/d/msgid/ansible-project/2ce4fa5b-80e9-4e33-915b-7f1361027eb2%40googlegroups.com?utm_medium=email&utm_source=footer&gt;%5C).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

Modify your template like this:

define contactgroup{  
&nbsp;&nbsp;&nbsp;&nbsp;contactgroup\_name Admins  
&nbsp;&nbsp;&nbsp;&nbsp;alias Seamless\_internal\_admins  
&nbsp;&nbsp;&nbsp;&nbsp;members {{ users[0].name }}{% for user in users[1:] %},{{ user.name  
}}{% endfor %}  
}

---

<div class="post-metadata">

**Author:** ![Joachim\_Friberg](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@Joachim\_Friberg](https://forum.ansible.com/u/Joachim_Friberg)\
**Post date:** [May 21, 2014, 11:34am UTC](https://forum.ansible.com/t/flatten-a-dict-list-in-a-template/15288/3 "2014-05-21T11:34:19Z")

</div>

How do you know all of this! You are amazing! 🙂

It worked
