Changing the delimiter for lookup results

63 views
Skip to first unread message

Jesse Schlüter

unread,
Jan 8, 2016, 5:37:50 PM1/8/16
to Ansible Project
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

Matt Martz

unread,
Jan 8, 2016, 6:07:21 PM1/8/16
to ansible...@googlegroups.com
I would instead of looping, just use the |join filter.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/bc5227af-2a8d-4532-a352-880dbe27d588%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Jesse Schlüter

unread,
Jan 9, 2016, 2:39:14 AM1/9/16
to Ansible Project
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.


Am Samstag, 9. Januar 2016 00:07:21 UTC+1 schrieb Matt Martz:
I would instead of looping, just use the |join filter.

On Friday, January 8, 2016, Jesse Schlüter <jesse.s...@helix360.de> wrote:
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Matt Martz

unread,
Jan 9, 2016, 9:18:51 AM1/9/16
to ansible...@googlegroups.com
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


On Saturday, January 9, 2016, Jesse Schlüter <jesse.s...@helix360.de> wrote:
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.

Am Samstag, 9. Januar 2016 00:07:21 UTC+1 schrieb Matt Martz:
I would instead of looping, just use the |join filter.

On Friday, January 8, 2016, Jesse Schlüter <jesse.s...@helix360.de> wrote:
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.


--
Matt Martz
@sivel
sivel.net

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/069b1d16-6bfc-4cf7-8efa-1228be56674c%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages