# Help with the "authorized\_key" Modul

**URL:** <https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619>\
**Category:** Ansible Project\
**Created:** [February 21, 2014, 12:45pm UTC](https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619 "2014-02-21T12:45:05Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yaniv\_Ferszt](https://avatars.discourse-cdn.com/v4/letter/y/bc8723/32.png) [@Yaniv\_Ferszt](https://forum.ansible.com/u/Yaniv_Ferszt)\
**Post date:** [February 21, 2014, 12:45pm UTC](https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619/1 "2014-02-21T12:45:05Z")

</div>

Hi,

I am just starting to learn ansible and have a question regarding the “authorized\_key” Modul.

what i am trying to accomplish is add multiple ssh public keys to a remote systems root user.

in the documentation [http://docs.ansible.com/authorized\_key\_module.html](http://docs.ansible.com/authorized_key_module.html)

i see that i need something like this

```auto
<i># Using with_file
- name: Set up authorized_keys for the deploy user
  authorized_key: user=deploy
                  key="{{ item }}"
  with_file:
    - public_keys/doe-jane
    - public_keys/doe-john</i>

so i created a playbook "ssh-keys.yml" with this contend
*---*

*- name: "Set up authorized_keys for the root user"*
 *hosts: testvms*
 *user: root*

 *tasks:*
*- name: Set up authorized_keys for the root user*
 *authorized_key: user=root*
 *key="{{ item }}"*
 *with_file:*
 *- /path/to/pub-key/user-a*
 *- /path/to/pub-key/user-b*

```

i receive the following error running this playbook  
_# ansible-playbook -v ssh-keys.yml_  
_ERROR: with\_file is not a legal parameter in an Ansible Playbook_

OS Information  
_ansible server_  
_# cat /etc/redhat-release_  
_Red Hat Enterprise Linux Server release 6.4 (Santiago)_

_# ansible-playbook --version_  
_ansible-playbook 1.5_

any idea what i did wrong?

---

<div class="post-metadata">

**Author:** ![Marc\_Patermann](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Marc\_Patermann](https://forum.ansible.com/u/Marc_Patermann)\
**Post date:** [February 21, 2014, 2:53pm UTC](https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619/2 "2014-02-21T14:53:44Z")

</div>

Yaniv,

Yaniv Ferszt schrieb (21.02.2014 13:45 Uhr):

> I am just starting to learn ansible and have a question regarding the "authorized\_key" Modul.
> 
> what i am trying to accomplish is add multiple ssh public keys to a remote systems root user.

I used copy and assemble to achieve this.  
First I copy a directory "keys" containing the individual keys to ~/.ssh/.  
Second I assemble all the files in this directory in the authozied\_keys file.

Marc

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [February 21, 2014, 11:25pm UTC](https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619/3 "2014-02-21T23:25:52Z")

</div>

I think that your only problem is indentation, you have to be careful with it. I used your code and added indentation:

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [February 22, 2014, 3:25am UTC](https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619/4 "2014-02-22T03:25:44Z")

</div>

user: root, sets the user you login as remotely to root. user has been deprecated in favor of the more explicit remote\_user: but they are the same (consider it an alias), it can also be set by the inventory variable ansible\_ssh\_user.

---

<div class="post-metadata">

**Author:** ![Yaniv\_Ferszt](https://avatars.discourse-cdn.com/v4/letter/y/bc8723/32.png) [@Yaniv\_Ferszt](https://forum.ansible.com/u/Yaniv_Ferszt)\
**Post date:** [February 22, 2014, 1:28pm UTC](https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619/5 "2014-02-22T13:28:24Z")

</div>

> I think that your only problem is indentation, you have to be careful with it. I used your code and added indentation:

thanks. i used your corrected code and i did not receive the error again.

but now something else is happening. if i run this code only the second ssh public key stays in the authorized\_keys file.  
its placing user-a public key and then replacing it with the public key from user-b.

---

<div class="post-metadata">

**Author:** ![Yaniv\_Ferszt](https://avatars.discourse-cdn.com/v4/letter/y/bc8723/32.png) [@Yaniv\_Ferszt](https://forum.ansible.com/u/Yaniv_Ferszt)\
**Post date:** [February 22, 2014, 1:39pm UTC](https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619/6 "2014-02-22T13:39:57Z")

</div>

even if i try it with this code

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [February 22, 2014, 1:48pm UTC](https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619/7 "2014-02-22T13:48:19Z")

</div>

What version of Ansible are you using? For me authorized\_keys module only adds new keys, it doesn’t replace them, so that shouldn’t happen, even though I would like to have an option to remove all previouse keys before adding new ones.

---

<div class="post-metadata">

**Author:** ![Yaniv\_Ferszt](https://avatars.discourse-cdn.com/v4/letter/y/bc8723/32.png) [@Yaniv\_Ferszt](https://forum.ansible.com/u/Yaniv_Ferszt)\
**Post date:** [February 22, 2014, 1:53pm UTC](https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619/8 "2014-02-22T13:53:37Z")

</div>

the latest from git (today)  
[yaniv@yfhv1 playbooks]$ ansible --version  
ansible 1.5

---

<div class="post-metadata">

**Author:** ![Yaniv\_Ferszt](https://avatars.discourse-cdn.com/v4/letter/y/bc8723/32.png) [@Yaniv\_Ferszt](https://forum.ansible.com/u/Yaniv_Ferszt)\
**Post date:** [February 22, 2014, 1:56pm UTC](https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619/9 "2014-02-22T13:56:29Z")

</div>

actually what is happening it reminds me of a puppet feature where you can manage the content of a file.  
but i did not expect this from the ansible “authorized\_keys” Module.

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [February 22, 2014, 2:42pm UTC](https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619/10 "2014-02-22T14:42:03Z")

</div>

I’m using 1.4.4 and it is not doing that, it is only appending new keys to the current ones. Maybe something was changed in 1.5. Could you try 1.4.5?

---

<div class="post-metadata">

**Author:** ![Yaniv\_Ferszt](https://avatars.discourse-cdn.com/v4/letter/y/bc8723/32.png) [@Yaniv\_Ferszt](https://forum.ansible.com/u/Yaniv_Ferszt)\
**Post date:** [February 22, 2014, 4:34pm UTC](https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619/11 "2014-02-22T16:34:43Z")

</div>

> I’m using 1.4.4 and it is not doing that, it is only appending new keys to the current ones. Maybe something was changed in 1.5. Could you try 1.4.5?

same behaviour with version 1.4.5

[yaniv@yfhv1 playbooks]$ ansible-playbook --version  
ansible-playbook 1.4.5

here are the playbooks i have tested

1. 

_[yaniv@yfhv1 playbooks]$ cat ssh-keys.yml_

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [February 22, 2014, 4:48pm UTC](https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619/12 "2014-02-22T16:48:27Z")

</div>

I just tried your first playbook on 1.4.4 and it added both keys to the authorized\_keys file. Are you sure that those keys are different files? Maybe you made a mistake an copied one key to another?

---

<div class="post-metadata">

**Author:** ![Yaniv\_Ferszt](https://avatars.discourse-cdn.com/v4/letter/y/bc8723/32.png) [@Yaniv\_Ferszt](https://forum.ansible.com/u/Yaniv_Ferszt)\
**Post date:** [February 22, 2014, 5:03pm UTC](https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619/13 "2014-02-22T17:03:08Z")

</div>

i just confirmed that i made a mistake and tested it with every version since 1.4.4 and it is working.

what i did was taking 1 ssh public key made a copy and changed inside the name at the end (user-a → user-b). was thinking it would handle it as a different key.

**not working example**  
user-a public key

cat usera.pub  
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAybrGnaPXZJ9LeTVO664PsrybLm5HPuwjOwhz+7+CQbKTqqk9OxL83gQDToCm55XT+6FUcNC1Yxs73Wymt5AetTAGBHKBpXknn1kMe/TFxb0rcF+W1e/LKciO0vjMAi+IyLFMwRiKE7IGUAR0P3eZxrz3TIhrZc+DzQupbCWdAVTAtneHfi5VAh3wg2CCx35SaLpNYZCbaYVTTvj7YjL7R3fkP2zEPTeJxoy7L+NOzSPShwGtMsFqxJTN6up4Y5sVfAAGqNQtGEDDfS0dd0sueg2OwY9KUk6iqE9GE2wPWbYHtNn+bfupAQvaOK1nSh6r2wgiZyd5x5qYaGPzabe8oQ== **user-a**

user-b public key  
cat userb.pub  
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAybrGnaPXZJ9LeTVO664PsrybLm5HPuwjOwhz+7+CQbKTqqk9OxL83gQDToCm55XT+6FUcNC1Yxs73Wymt5AetTAGBHKBpXknn1kMe/TFxb0rcF+W1e/LKciO0vjMAi+IyLFMwRiKE7IGUAR0P3eZxrz3TIhrZc+DzQupbCWdAVTAtneHfi5VAh3wg2CCx35SaLpNYZCbaYVTTvj7YjL7R3fkP2zEPTeJxoy7L+NOzSPShwGtMsFqxJTN6up4Y5sVfAAGqNQtGEDDfS0dd0sueg2OwY9KUk6iqE9GE2wPWbYHtNn+bfupAQvaOK1nSh6r2wgiZyd5x5qYaGPzabe8oQ== **user-b**

so i generated a new key pair and gave user-b a new public key and it works.  
sorry for all that trouble and thanks for the help.

---

<div class="post-metadata">

**Author:** ![Strahinja\_Kustudic2](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Strahinja\_Kustudic2](https://forum.ansible.com/u/Strahinja_Kustudic2)\
**Post date:** [February 22, 2014, 6:21pm UTC](https://forum.ansible.com/t/help-with-the-authorized-key-modul/14619/14 "2014-02-22T18:21:28Z")

</div>

Glad you found the problem 🙂
