Changing the delimiter for lookup results

Hey everyone,

as I understand, normally a lookup returning a list, will be joined to a string using “,” as a delimiter. Is it possible to change this behavior?

I only found the solution by patching the code. The background of the question is, that I would like to do something like this:

vars:
ssh_keys:

  • key1
  • key2
  • key3
    tasks:
  • name: set authorized keys exclusive
    authorized_key: user=root exclusive=yes key=“{{ lookup(‘items’, ssh_keys, delimiter=‘\n’) }}”

Unfortunately authorized_key using the exclusive flag is not loop aware. I do already have a patch for 1.9 enabling the delimiter keyword, but I wanted to ask whether there is a “native” solution before bothering the developers with a pull request.

Best regards,
Jesse

I would instead of looping, just use the |join filter.

If I understand you correctly this would work like this, yes?

vars:
ssh_keys_string: “{{ lookup(‘items’, ssh_keys, wantlist=True) | join(‘\n’) }}”

I know my example does not make much sense, but I do not actually have the ssh_key variable as a list of all ssh-keys, but create it via lookup. I just used the items lookup to simplify the example.

What I am saying is to literally do:

key=“{{ ssh_keys|join(‘\n’) }}”

There is no need to use any lookup plugin.

Just join the list with a \n