# authorized\_keys module change

**URL:** <https://forum.ansible.com/t/authorized-keys-module-change/12798>\
**Category:** Ansible Project\
**Created:** [May 13, 2013, 11:50am UTC](https://forum.ansible.com/t/authorized-keys-module-change/12798 "2013-05-13T11:50:22Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Maas](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Mark\_Maas](https://forum.ansible.com/u/Mark_Maas)\
**Post date:** [May 13, 2013, 11:50am UTC](https://forum.ansible.com/t/authorized-keys-module-change/12798/1 "2013-05-13T11:50:22Z")

</div>

Hi All,

In my previous playbook, I had this:

> ````auto
> 
> ```
> - name: USERS | Authorized Key toevoegen aan ${item.username}
> action: authorized_key user=${item.username} key="$FILE(/etc/ansible/keys/${item.username}.pub)"
> with_items: $tomadmins
> when_set: $tomadmins
> tags:
> - tomadmins
> ```
> 
> ````

Which worked perfectly.

Now I see this has changed, so I tried this:

> - name: USERS | Authorized Key toevoegen aan ${item.username}
> 
> action: authorized\_key user={{ item.username }} key=“{{ lookup(‘file’, ‘/etc/ansible/keys/{{ item.username }}.pub’) }}”
> 
> with\_items: $tomadmins
> 
> tags:
> 
> - tomadmins

But now it comes with this error:

> TASK: [USERS | Authorized Key toevoegen aan ${item.username}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> fatal: [tom2prap03] =\> /etc/ansible/keys/{{item.username}}.pub does not exist
> 
> FATAL: all hosts have already failed – aborting

Any suggestions much apreciated.

Thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![Lester\_Wade](https://avatars.discourse-cdn.com/v4/letter/l/97f17d/32.png) [@Lester\_Wade](https://forum.ansible.com/u/Lester_Wade)\
**Post date:** [May 13, 2013, 11:54am UTC](https://forum.ansible.com/t/authorized-keys-module-change/12798/2 "2013-05-13T11:54:35Z")

</div>

The new variable stuff referencing of {{ myvar }} only works with 1.2, so make sure you are using the current devel branch. Prior to 1.2 (devel), you need to be using $myvar still.

---

<div class="post-metadata">

**Author:** ![Mark\_Maas](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Mark\_Maas](https://forum.ansible.com/u/Mark_Maas)\
**Post date:** [May 13, 2013, 12:01pm UTC](https://forum.ansible.com/t/authorized-keys-module-change/12798/3 "2013-05-13T12:01:43Z")

</div>

Upgraded this morning 😉

mmaas@pmgtansible:~/playbooks$ ansible --version  
ansible 1.2

So that’s not it 😉

---

<div class="post-metadata">

**Author:** ![Lester\_Wade](https://avatars.discourse-cdn.com/v4/letter/l/97f17d/32.png) [@Lester\_Wade](https://forum.ansible.com/u/Lester_Wade)\
**Post date:** [May 13, 2013, 12:14pm UTC](https://forum.ansible.com/t/authorized-keys-module-change/12798/4 "2013-05-13T12:14:52Z")

</div>

Drat! 😉

Maybe you need some quoting around the variable since that encapsulated by another set of {{ in regards to the lookup.

---

<div class="post-metadata">

**Author:** ![Mark\_Maas](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Mark\_Maas](https://forum.ansible.com/u/Mark_Maas)\
**Post date:** [May 13, 2013, 12:23pm UTC](https://forum.ansible.com/t/authorized-keys-module-change/12798/5 "2013-05-13T12:23:55Z")

</div>

Bettererder:

- name: USERS | Authorized Key toevoegen aan ${item.username}  
action: authorized\_key  
user={{ item.username }}  
key=“{{ lookup(‘file’, ‘/etc/ansible/keys/’{{ item.username }}‘.pub’) }}”  
with\_items: $tomadmins  
tags:
- tomadmins

Gives me this error now:

> TASK: [USERS | Authorized Key toevoegen aan ${item.username}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> failed: [xxxxxxx] =\> (item={‘username’: ‘hmeijer’, ‘group’: ‘hmeijer’, ‘uid’: 510, ‘gecos’: ‘Herman Meijer’, ‘groups’: ‘xxxx’, ‘password’: None}) =\> {“failed”: true, “item”: {“gecos”: “Herman Meijer”, “group”: “hmeijer”, “groups”: “fam”, “password”: null, “uid”: 510, “username”: “hmeijer”}}
> 
> msg: Failed to lookup user {{item.username}}: ‘getpwnam(): name not found: {{item.username}}’
> 
> failed: [xxxxxxx] =\> (item={‘username’: ‘fam’, ‘group’: ‘fam’, ‘uid’: 504, ‘gecos’: ‘Fam User’, ‘groups’: ‘xxxx’, ‘password’: None}) =\> {“failed”: true, “item”: {“gecos”: “Fam User”, “group”: “fam”, “groups”: “users”, “password”: null, “uid”: 504, “username”: “fam”}}
> 
> msg: Failed to lookup user {{item.username}}: ‘getpwnam(): name not found: {{item.username}}’

FATAL: all hosts have already failed – aborting

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 13, 2013, 12:38pm UTC](https://forum.ansible.com/t/authorized-keys-module-change/12798/6 "2013-05-13T12:38:13Z")

</div>

Looks like here’s your problem, you can’t nest template variables (and don’t have to):

If you want to use the new syntax, change:

key=“{{ lookup(‘file’, ‘/etc/ansible/keys/’{{ item.username }}‘.pub’) }}”

To:

key=“{{ lookup(‘file’, ‘/etc/ansible/keys/’ + item[‘username’] + ‘.pub’) }}”

Though our intent is to keep both mechanisms working so nobody has a forced syntax change.  
Very important 🙂

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [May 13, 2013, 12:43pm UTC](https://forum.ansible.com/t/authorized-keys-module-change/12798/7 "2013-05-13T12:43:57Z")

</div>

can you try

with\_items: tomadmins

or

with\_items: ‘{{ tomadmins }}’

also the paste the tomadmins list. and maybe also a debug output -vv

- Benno

---

<div class="post-metadata">

**Author:** ![Mark\_Maas](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Mark\_Maas](https://forum.ansible.com/u/Mark_Maas)\
**Post date:** [May 13, 2013, 12:46pm UTC](https://forum.ansible.com/t/authorized-keys-module-change/12798/8 "2013-05-13T12:46:03Z")

</div>

Closer I guess:

> TASK: [USERS | Authorized Key toevoegen aan ${item.username}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO tom2prap03
> 
> EXEC /bin/sh -c ‘mkdir -p $HOME/.ansible/tmp/ansible-1368449027.57-41569896691629 && echo $HOME/.ansible/tmp/ansible-1368449027.57-41569896691629’
> 
> REMOTE\_MODULE authorized\_key user=hmeijer key=
> 
> PUT /tmp/tmp0bElUj TO /root/.ansible/tmp/ansible-1368449027.57-41569896691629/authorized\_key
> 
> EXEC /bin/sh -c ‘sudo -k && sudo -H -S -p “[sudo via ansible, key=ljwyeixjzmyrddqpiwztitabnvqwtqjo] password: " -u root /bin/sh -c '”’“‘/usr/bin/python /root/.ansible/tmp/ansible-1368449027.57-41569896691629/authorized\_key; rm -rf /root/.ansible/tmp/ansible-1368449027.57-41569896691629/ \>/dev/null 2\>&1’”‘"’’
> 
> fatal: [tom2prap03] =\> failed to parse: Traceback (most recent call last):
> 
> File “/root/.ansible/tmp/ansible-1368449027.57-41569896691629/authorized\_key”, line 1116, in ?
> 
> main()
> 
> File “/root/.ansible/tmp/ansible-1368449027.57-41569896691629/authorized\_key”, line 218, in main
> 
> results = enforce\_state(module, module.params)
> 
> File “/root/.ansible/tmp/ansible-1368449027.57-41569896691629/authorized\_key”, line 193, in enforce\_state
> 
> writekeys(module, keyfile(module, user, write, path, manage\_dir), keys)
> 
> File “/root/.ansible/tmp/ansible-1368449027.57-41569896691629/authorized\_key”, line 164, in writekeys
> 
> module.atomic\_replace(tmp\_path, filename)
> 
> AttributeError: ‘AnsibleModule’ object has no attribute ‘atomic\_replace’
> 
> FATAL: all hosts have already failed – aborting

With this code:

> - name: USERS | Authorized Key toevoegen aan ${item.username}
> 
> action: authorized\_key
> 
> user={{ item.username }}
> 
> key=“{{ lookup(‘file’, ‘/etc/ansible/keys/’ + item[‘username’] + ‘.pub’) }}”
> 
> with\_items: $tomadmins
> 
> tags:
> 
> - tomadmins

But we’re getting closer 😉

---

<div class="post-metadata">

**Author:** ![Mark\_Maas](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Mark\_Maas](https://forum.ansible.com/u/Mark_Maas)\
**Post date:** [May 13, 2013, 12:48pm UTC](https://forum.ansible.com/t/authorized-keys-module-change/12798/9 "2013-05-13T12:48:52Z")

</div>

> can you try
> 
> with\_items: tomadmins
> 
> or
> 
> with\_items: ‘{{ tomadmins }}’

Same error (As in: “AttributeError: ‘AnsibleModule’ object has no attribute ‘atomic\_replace’”)

> also the paste the tomadmins list. and maybe also a debug output -vv

Example bit for the tomadmins variable:  
tomadmins:

- username: hmeijer  
gecos: ‘Herman Meijer’  
group: hmeijer  
groups: fam  
password:  
uid: 501

- username: fam  
gecos: ‘Fam User’  
group: fam  
groups: staff  
password:  
uid: 504

The previous code worked btw, but since I upgraded to 1.2 this morning, it no longer does so.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 13, 2013, 12:50pm UTC](https://forum.ansible.com/t/authorized-keys-module-change/12798/10 "2013-05-13T12:50:49Z")

</div>

Quick note:

with\_items: foo

is all you ever need

with\_items: “{{ foo }}”

is legal, but just unnecessary punctuation 🙂

Ansible will convert one to the other internally, automatically.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 13, 2013, 12:51pm UTC](https://forum.ansible.com/t/authorized-keys-module-change/12798/11 "2013-05-13T12:51:59Z")

</div>

You have an install that is somehow mixed up, probably from doing a “make install” or having cruft in your library directory.

Clean out your library directory or make sure you are running from source using ./hacking/env-setup

We’re obviously still _developing_ 1.2 so the system is not yet coded to prefer modules in subdirectories to ones at top level, thus it is probably finding your 1.1 module you had previously installed.

---

<div class="post-metadata">

**Author:** ![Mark\_Maas](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Mark\_Maas](https://forum.ansible.com/u/Mark_Maas)\
**Post date:** [May 14, 2013, 6:30am UTC](https://forum.ansible.com/t/authorized-keys-module-change/12798/12 "2013-05-14T06:30:27Z")

</div>

Yep that was it!

I moved the old ansible directory completely out of the way, and did the make install again:

mv /usr/share/ansible{,.bak}; sudo make install

Thanks loads!

Mark
