Globbing with Lookup

Hi all
Not sure if this is even the right tile. So I have a set of SSH keys, I want to be used as authorized keys for users. I’ve put all of them in a directory and am trying to do the job but nothing happens. So I did some debugging. This is what I do when not using a variable:

  • name: Some debugging
    debug: msg=“{{ lookup(‘file’, ‘public_ssh_keys/navid.pub’) }}”

And it returns what I expect. Now I try doing a loop:

  • name: Some debugging
    debug: msg=“{{ lookup(‘file’, ‘public_ssh_keys/{{ item }}.pub’) }}”
    with_items:
  • navid

But this time it returns this:

TASK: [base-ami | Some debugging] *********************************************
ok: [default] => (item=navid) => {
“item”: “navid”,
“msg”: “”
}

So how am I supposed to have it return what I expect? I think it’s probably a matter of using the single and double quotes right but how?

Regards

Navid

mustaches don't stack!

you want:
"{{ lookup('file', 'public_ssh_keys/' + item + '.pub') }}"

Stupid me. Thanks, Brian. That worked like a charm.

Hi Navid,

I 'm trying something similar, based on: http://blather.michaelwlucas.com/archives/1819

Do you mind share your approach?

Thanks,
Wawrzek

Here’s how I did it. Created a directory with the public keys in it. For instance:

base-ami/files/public_ssh_keys/jack.pub
base-ami/files/public_ssh_keys/jill.pub

  • name: Create basic users
    user: name={{ item }} shell=/bin/bash groups=grabtaxi
    with_items:
  • jack
  • jill

And then used lookup to populate the keys:

  • name: Use the public keys from the users for the their login
    authorized_key: user={{ item }} key=“{{ lookup(‘file’, ‘public_ssh_keys/’ + item + ‘.pub’) }}”
    with_items:
  • jack
  • jill

You could make it fancier using vars and all but I think you get the gist of it.