User that runs ansible

1,876 views
Skip to first unread message

Mathias Bogaert

unread,
Jul 2, 2013, 7:53:17 AM7/2/13
to ansible...@googlegroups.com
Hi,

How do I get the local user in an variable? My usercase:

- name: Make sure my authorized key is installed
  authorized_key: user=${USER} key="{{ lookup('file', '/home/' + $USER + '/.ssh/id_rsa.pub') }}"

Thanks,

Mathias

Jan-Piet Mens

unread,
Jul 2, 2013, 8:40:49 AM7/2/13
to ansible...@googlegroups.com
> How do I get the local user in an variable? My usercase:

Look for the ENV lookup: $ENV(USER)

-JP

Mathias Bogaert

unread,
Jul 2, 2013, 9:29:34 AM7/2/13
to ansible...@googlegroups.com
Thanks! But it fails:

msg: Failed to lookup user ${ENV(USER)}: 'getpwnam(): name not found: ${ENV(USER)}'
failed: [hdata01] => {"failed": true, "item": ""}

Any idea?

Mathias Bogaert

unread,
Jul 2, 2013, 9:32:09 AM7/2/13
to ansible...@googlegroups.com
It works like this:

- name: Make sure my authorized key is installed
  authorized_key: user=$ENV(USER) key="{{ lookup('file', '/home/' + $ENV(USER) + '/.ssh/id_rsa.pub') }}"

but the key doesn't evaluate. The lookup doesn't work. My remote file contains 

{{lookup('file', '/home/' + mathias + '/.ssh/id_rsa.pub')}}

?

Jan-Piet Mens

unread,
Jul 2, 2013, 9:32:32 AM7/2/13
to ansible...@googlegroups.com
> msg: Failed to lookup user ${ENV(USER)}: 'getpwnam(): name not found:
> ${ENV(USER)}'
> failed: [hdata01] => {"failed": true, "item": ""}

getpwnam() failed?! Is this a Unix system you're running on? ;-)
What OS/version?

Can you try

- local_action: debug msg="Hello $ENV(HOME)"

and run ansible-playbook -vvv

-JP

Mathias Bogaert

unread,
Jul 2, 2013, 9:38:45 AM7/2/13
to ansible...@googlegroups.com
Yeah, it's an Ubuntu 12.10.

But it works like this:

- name: Make sure my authorized key is installed
  authorized_key: user=$ENV(USER) key="{{ lookup('file', '/home/' + $ENV(USER) + '/.ssh/id_rsa.pub') }}"

but the key doesn't evaluate. The lookup doesn't work. My remote authorized_keys now contains 

{{lookup('file', '/home/' + mathias + '/.ssh/id_rsa.pub')}}

which doesn't work...

Mathias Bogaert

unread,
Jul 2, 2013, 9:40:22 AM7/2/13
to ansible...@googlegroups.com
This works:

- name: Make sure my local RSA SSH key is installed
  authorized_key: user=$ENV(USER) key="{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

Thanks!!

Michael DeHaan

unread,
Jul 2, 2013, 2:26:17 PM7/2/13
to ansible...@googlegroups.com
Friendly reminder:

$ENV(USER) as a mechanism of getting lookup plugins is deprecated

We should be instructing folks do to:

{{ lookup('env', 'USER') }}




--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

patte

unread,
May 14, 2014, 7:28:28 PM5/14/14
to ansible...@googlegroups.com
Hi! For me it doesn't!
Neither this:

  authorized_key: user=$ENV(USER) key="{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
nor this:
  authorized_key: user={{ lookup('env', 'USER') }} key="{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

both fail with:
failed: [dev] => {"failed": true, "item": ""}
msg: Failed to lookup user patte: 'getpwnam(): name not found: patte'

I'm patte on OSX, with ansible 1.5.5 fired by vagrant provision
any idea?

Michael DeHaan

unread,
May 14, 2014, 7:45:18 PM5/14/14
to ansible...@googlegroups.com
This seems unrelated and you're replying to an old thread, but please try this on Ansible 1.6 (so you're using a recently released version) and give us some steps to reproduce this, perhaps.

Thanks!


Reply all
Reply to author
Forward
0 new messages