Create list with template

Bonjour :wave:

I’m little bit lost… :sweat_smile:

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

- 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 }}"
macos_configuration_apps_dock_list:
  - "/System/Applications/Launchpad.app"
  - "/Applications/Firefox.app"
  [...]

But, the result, it emptys my dock :scream:

The secret is, for each icon, I need to have this type of 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>

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

For bad example:

- 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:

<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:

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

- 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! :ring_buoy:

Not sure…have you tried to set state: list ?
https://docs.ansible.com/ansible/latest/collections/community/general/osx_defaults_module.html#parameter-state

Hi @markuman

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

:worried:

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

- 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 option to true (otherwise, I think it will wipe all other app icons from the dock).

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! :sweat_smile:

Thanks again. :partying_face:

1 Like