# Create list with template

**URL:** <https://forum.ansible.com/t/create-list-with-template/10490>\
**Category:** Get Help\
**Tags:** ansible-core\
**Created:** [October 24, 2024, 4:52pm UTC](https://forum.ansible.com/t/create-list-with-template/10490 "2024-10-24T16:52:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fred](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fred/32/5295_2.png) [@Fred](https://forum.ansible.com/u/Fred)\
**Post date:** [October 24, 2024, 4:52pm UTC](https://forum.ansible.com/t/create-list-with-template/10490/1 "2024-10-24T16:52:00Z")

</div>

Bonjour 👋

I’m little bit lost… 😅

I want to use module `community.general.osx_defaults` with list of apps and specific template for each apps. Simple, no?

```yaml
- name: "MacOS | Dock | Add Apps shortcuts"
  community.general.osx_defaults:
    domain: com.apple.dock
    key: persistent-apps
    type: array
    value: "{{ macos_configuration_apps_dock_list }}"

```

```yaml
macos_configuration_apps_dock_list:
  - "/System/Applications/Launchpad.app"
  - "/Applications/Firefox.app"
  [...]

```

But, the result, it emptys my dock 😱

The secret is, for each icon, I need to have this type of value:

```auto
      <dict>
        <key>tile-data</key>
        <dict>
          <key>file-data</key>
          <dict>
            <key>_CFURLString</key>
            <string>/Applications/Firefox.app</string>
            <key>_CFURLStringType</key>
            <integer>0</integer>
          </dict>
        </dict>
      </dict>

```

I would like to do this, but for each item I don’t want to put all template above.

For bad example:

```yaml
- name: "MacOS | Dock | Add Apps shortcuts"
  community.general.osx_defaults:
    domain: com.apple.dock
    key: persistent-apps
    type: array
    value: 
      - <dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Applications/Firefox.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>
      - <dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/System/Applications/Messages.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>
[...]

```

I would like to concatenate the template:

```auto
<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Applications/Firefox.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>

```

And list:

```yaml
macos_configuration_apps_dock_list:
  - "/System/Applications/Launchpad.app"
  - "/Applications/Firefox.app"
  [...]

```

And! I the module `community.general.osx_defaults` declare list as a variable

```auto
- name: "MacOS | Dock | Add Apps shortcuts"
  community.general.osx_defaults:
    domain: com.apple.dock
    key: persistent-apps
    type: array
    value: "{{ macos_configuration_apps_dock_list_template }}"

```

I tried many solutions, nested loops, templating jinja with {% for … %} but I don’t find solution…

If you have an advice. Thanks! 🛟

---

<div class="post-metadata">

**Author:** ![markuman](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@markuman](https://forum.ansible.com/u/markuman)\
**Post date:** [October 25, 2024, 6:43am UTC](https://forum.ansible.com/t/create-list-with-template/10490/2 "2024-10-25T06:43:47Z")

</div>

Not sure…have you tried to set `state: list` ?  
[https://docs.ansible.com/ansible/latest/collections/community/general/osx\_defaults\_module.html#parameter-state](https://docs.ansible.com/ansible/latest/collections/community/general/osx_defaults_module.html#parameter-state)

---

<div class="post-metadata">

**Author:** ![Fred](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fred/32/5295_2.png) [@Fred](https://forum.ansible.com/u/Fred)\
**Post date:** [October 25, 2024, 9:14am UTC](https://forum.ansible.com/t/create-list-with-template/10490/3 "2024-10-25T09:14:33Z")

</div>

Hi @markuman

`state: list` gives me the list of already presents icons.

😟

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [October 28, 2024, 9:20pm UTC](https://forum.ansible.com/t/create-list-with-template/10490/4 "2024-10-28T21:20:02Z")

</div>

If the sample data is representative of all the apps, you could simplify like this (untested):

```yaml
- name: "MacOS | Dock | Add Apps shortcuts"
  community.general.osx_defaults:
    domain: com.apple.dock
    key: persistent-apps
    type: array
    value: "{{ dock_template }}"
  vars:
    macos_dock_apps:
      - "/System/Applications/Launchpad.app"
      - "/Applications/Firefox.app"
    value_start: "<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>"
    value_end: "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"
    dock_template: |
      [{% for app in macos_dock_apps %}
        "{{ value_start }}{{ app }}{{ value_end }}"{{ "," if not loop.last else "" }}
      {% endfor %}]

```

You may also want to set the [array\_add](https://docs.ansible.com/ansible/latest/collections/community/general/osx_defaults_module.html#parameter-array_add) option to true (otherwise, I think it will wipe all other app icons from the dock).

---

<div class="post-metadata">

**Author:** ![Fred](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fred/32/5295_2.png) [@Fred](https://forum.ansible.com/u/Fred)\
**Post date:** [October 29, 2024, 4:10pm UTC](https://forum.ansible.com/t/create-list-with-template/10490/5 "2024-10-29T16:10:54Z")

</div>

Many thanks @shertel that’s it!

I was near of your solution, before you post it.

I just missed the dock\_template variable and you bring me the solution.

For `array-add`, if variable is set to `true`, it will add again and again, I find my dock with dozens of icons! 😅

Thanks again. 🥳

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 28, 2024, 4:11pm UTC](https://forum.ansible.com/t/create-list-with-template/10490/6 "2024-11-28T16:11:19Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
