# How to create a list of dicts

**URL:** <https://forum.ansible.com/t/how-to-create-a-list-of-dicts/29873>\
**Category:** Ansible Project\
**Created:** [March 13, 2019, 6:55am UTC](https://forum.ansible.com/t/how-to-create-a-list-of-dicts/29873 "2019-03-13T06:55:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashok1](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Ashok1](https://forum.ansible.com/u/Ashok1)\
**Post date:** [March 13, 2019, 6:55am UTC](https://forum.ansible.com/t/how-to-create-a-list-of-dicts/29873/1 "2019-03-13T06:55:25Z")

</div>

Hi All,

I have a list `logf_dest` (of log files):

`"logf_dest": ["/logs/mx/mail.log", "/logs/dyn.log", "/logs/apache-servers/other.log"]`

I need to form a “list of dicts” called `logfile_config`, like the below:

`

- logfile\_name: remote.conf  
logfile\_pattern: /logs/mx/mail.log  
logfile\_owner: root

- logfile\_name: remote.conf  
logfile\_pattern: /logs/dyn.log  
logfile\_owner: root

- logfile\_name: remote.conf  
logfile\_pattern: /logs/apache-servers/other.log  
logfile\_owner: root  
`

I’ve come this far in doing so:

`

- name: Create list of dicts  
set\_fact:  
logfile\_config: |  
{  
{% for k in logf\_dest %}  
logfile\_name: remote.conf  
logfile\_pattern: {{ k }}  
logfile\_owner: root  
{% endfor %}  
}  
`

But, above is obviously not formatted correctly.

I need help on what I am missing here.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [March 13, 2019, 7:20am UTC](https://forum.ansible.com/t/how-to-create-a-list-of-dicts/29873/2 "2019-03-13T07:20:10Z")

</div>

How dynamic is the content of that source list? One simple approach would be:

- logfile\_name: remote.conf  
logfile\_pattern: “{{ logf\_dest[0] }}”  
logfile\_owner: root
- logfile\_name: remote.conf  
logfile\_pattern: “{{ logf\_dest[1] }}”  
logfile\_owner: root
- logfile\_name: remote.conf  
logfile\_pattern: “{{ logf\_dest[2] }}”  
logfile\_owner: root

If the list is not always three items long, you can do it with a loop:

- set\_fact:  
logdict: “{{ loglist |default() +  
[ {  
‘logfile\_name’: ‘remote.conf’,  
‘logfile\_pattern’: item,  
‘logfile\_owner’: ‘root’  
}  
] }}”  
with\_items: “{{ logf\_dest }}”

Anyway, something like that. That’s off the top of my head, so you may need to correct the syntax a bit, but you get the idea.

Regards, k.

---

<div class="post-metadata">

**Author:** ![Ashok1](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Ashok1](https://forum.ansible.com/u/Ashok1)\
**Post date:** [March 13, 2019, 11:08am UTC](https://forum.ansible.com/t/how-to-create-a-list-of-dicts/29873/3 "2019-03-13T11:08:33Z")

</div>

The content of the source is very dynamic on each run, hence, going static isn’t viable.

I tried with your logic, like below:

`

- name: Form list of dicts  
set\_fact:  
logfile\_config: “{{ logf\_dest |default() +  
[ {  
‘logfile\_name’: ‘remote.conf’,  
‘logfile\_pattern’: item,  
‘logfile\_owner’: ‘root’  
}  
] }}”  
with\_items: “{{ logf\_dest }}”

- debug:  
var: logfile\_config  
`

It doesn’t give the desired output. `logfile_config` is:

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/ansible/playbooks/loghost-new.yml:42  
ok: [log-dvl.vpn] =\> {  
“logfile\_config”: [  
“/logs/mx/mail.log”,  
“/logs/dyn.log”,  
“/logs/apache-servers/other.log”,  
{  
“logfile\_name”: “remote.conf”,  
“logfile\_owner”: “root”,  
“logfile\_pattern”: “/logs/apache-servers/other.log”  
}  
]  
}

Anything I missed?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 13, 2019, 12:26pm UTC](https://forum.ansible.com/t/how-to-create-a-list-of-dicts/29873/4 "2019-03-13T12:26:01Z")

</div>

This need to be  
&nbsp;&nbsp;&nbsp;logfile\_config: "{{ logfile\_config |default() +

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [March 13, 2019, 10:15pm UTC](https://forum.ansible.com/t/how-to-create-a-list-of-dicts/29873/5 "2019-03-13T22:15:49Z")

</div>

Sorry about that. I did warn you it was off the top of my head 🙂

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [March 13, 2019, 10:21pm UTC](https://forum.ansible.com/t/how-to-create-a-list-of-dicts/29873/6 "2019-03-13T22:21:48Z")

</div>

As Kai Stian Olstad wrote, I got the target list wrong. The first ocurrence of “loglist” should be “logdict”, OR (even better) do as Kai suggested and substitute “logfile\_config” for loglist and logdict.

Regards, K.

---

<div class="post-metadata">

**Author:** ![Ashok1](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Ashok1](https://forum.ansible.com/u/Ashok1)\
**Post date:** [March 14, 2019, 12:48pm UTC](https://forum.ansible.com/t/how-to-create-a-list-of-dicts/29873/7 "2019-03-14T12:48:24Z")

</div>

Hey, it worked 🙂

Thanks, Kai and Karl.
