# authorized\_key and multiple keys for one user

**URL:** <https://forum.ansible.com/t/authorized-key-and-multiple-keys-for-one-user/32151>\
**Category:** Ansible Project\
**Created:** [February 5, 2020, 9:54pm UTC](https://forum.ansible.com/t/authorized-key-and-multiple-keys-for-one-user/32151 "2020-02-05T21:54:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Nanboe](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@Daniel\_Nanboe](https://forum.ansible.com/u/Daniel_Nanboe)\
**Post date:** [February 5, 2020, 9:54pm UTC](https://forum.ansible.com/t/authorized-key-and-multiple-keys-for-one-user/32151/1 "2020-02-05T21:54:19Z")

</div>

I have an inventory file where for one host I have defined the users and keys that I want to use in a playbook:

`  
sftp\_users:

- remote\_user: prod  
state: present  
long\_name: prod user  
ssh\_key:
- prodkey\_1
- prodkey\_2
- remote\_user: test  
state: present  
long\_name: test user  
ssh\_key:
- testkey  
`

I want then to add to each user one or multiple ssh keys that I have located in the repository from where I run the script.

I know that authorized\_key on the key: need to have joined the both keys from an user.

This is what I have no but it takes only the last key and not both.

`

- name: Create sftp user authorized\_key entries.  
authorized\_key:  
comment: “{{ item.1 | default(‘’) }}”  
exclusive: false  
key: “{{ lookup(‘file’, GIT\_HOME + ‘/config/clients/client/ssh\_keys/{{ item.1 }}.pub’) }}”  
manage\_dir: true  
user: “{{ item.0.remote\_user }}”  
become: true  
when: (item.0.state != “absent”)  
with\_subelements:
- “{{ sftp\_users }}”
- ssh\_key  
`

Any ideas on how I can concatenate the 2 or more keys for one client?

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 6, 2020, 8:25am UTC](https://forum.ansible.com/t/authorized-key-and-multiple-keys-for-one-user/32151/2 "2020-02-06T08:25:08Z")

</div>

> I have an inventory file where for one host I have defined the users and keys that I want to use in a playbook:
> 
> \>  
> sftp\_users:  
> -remote\_user:prod  
> state:present  
> long\_name:prod user  
> ssh\_key:  
> -prodkey\_1  
> -prodkey\_2  
> -remote\_user:test  
> state:present  
> long\_name:test user  
> ssh\_key:  
> -testkey  
> \>  
> \>  
> \>
> 
> I want then to add to each user one or multiple ssh keys that I have located in the repository from where I run the script.
> 
> I know that authorized\_key on the key: need to have joined the both keys from an user.
> 
> This is what I have no but it takes only the last key and not both.
> 
> \>  
> -name:Createsftp user authorized\_key entries.  
> authorized\_key:  
> comment:"{{ item.1 | default('') }}"  
> exclusive:false  
> key:"{{ lookup('file', GIT\_HOME + '/config/clients/client/ssh\_keys/{{ item.1 }}.pub') }}"  
> manage\_dir:true  
> user:"{{ item.0.remote\_user }}"  
> become:true  
> when:(item.0.state!="absent")  
> with\_subelements:  
> -"{{ sftp\_users }}"  
> -ssh\_key

The authorized\_key module should be able to handle multiple keys for a remote user, there is no need to "join" them.

I'm using the following task successfully:

- name: Add SSH keys  
&nbsp;&nbsp;authorized\_key:  
&nbsp;&nbsp;&nbsp;&nbsp;user: "{{ item.0.username }}"  
&nbsp;&nbsp;&nbsp;&nbsp;key: "{{ lookup('file', users\_inventory\_dir + '/files/ssh-keys/' + item.1 + '-ssh.pub') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;state: present  
&nbsp;&nbsp;loop: "{{ users | subelements('ssh\_keys', {'skip\_missing': True})}}"  
&nbsp;&nbsp;tags:  
&nbsp;&nbsp;&nbsp;&nbsp;- users

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
