Skip to content

Instantly share code, notes, and snippets.

@pkunc
Last active September 14, 2024 21:31
Show Gist options
  • Save pkunc/860ab552a4e0630c4e5d6f063c4a3fc0 to your computer and use it in GitHub Desktop.
Save pkunc/860ab552a4e0630c4e5d6f063c4a3fc0 to your computer and use it in GitHub Desktop.
Ansible: Add a prefix or suffix to all items of a list

Ansible: Add a prefix or suffix to all items of a list

Add prefix to a list items

- debug:
    var: result
  vars:
    prefix: foo
    a_list: [ "bar", "bat", "baz" ]
    result: "{{ [prefix] | product(a_list) | map('join') | list }}"

Add suffix to a list items

- debug:
    var: result
  vars:
    suffix: foo
    a_list: [ "bar", "bat", "baz" ]
    result: "{{ a_list | product([suffix]) | map('join') | list }}"

Add prefix and suffix to a list items

- debug:
    var: result
  vars:
    prefix: foo1
    suffix: foo2
    a_list: [ "bar", "bat", "baz" ]
    result: "{{ [prefix] | product(a_list) | map('join') | list | product([suffix]) | map('join') | list }}"

Credit: Nicolas Massé

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment