# Playbook Failys atc copying ssh keys

**URL:** <https://forum.ansible.com/t/playbook-failys-atc-copying-ssh-keys/24933>\
**Category:** Ansible Project\
**Created:** [July 8, 2017, 6:18pm UTC](https://forum.ansible.com/t/playbook-failys-atc-copying-ssh-keys/24933 "2017-07-08T18:18:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [July 8, 2017, 6:18pm UTC](https://forum.ansible.com/t/playbook-failys-atc-copying-ssh-keys/24933/1 "2017-07-08T18:18:06Z")

</div>

I have the below playbook. Created ssh keys for dave on the localhost (ansible master) and trying to create some users, groups and copy over ssh keys for some users all in the playbook.

All works well until the copying over ssh keys part. Ive confirmed the directory and public key exists for dave on the localhost…and the playbook created /home/dave/.ssh on the remote host

Not sure why this would fail?

Playbook output -

TASK [Copy ths ssh public key into the authorized key dir on the remote host] \*\*\*\*\*\*\*\*  
failed: [10.10.0.5 → localhost] (item=frank) =\> {“failed”: true, “item”: “frank”, “ms g”: “Unable to find ‘/home/frank/.ssh/id\_rsa.pub’ in expected paths.”}  
failed: [10.10.0.5 → localhost] (item=joe) =\> {“failed”: true, “item”: “joe”, “msg”: “Unable to find ‘/home/joe/.ssh/id\_rsa.pub’ in expected paths.”}  
…ignoring  
failed: [10.10.0.5 → localhost] (item=dave) =\> {“failed”: true, “item”: “dave”, “msg” : “Unable to find ‘/home/dave/.ssh/id\_rsa.pub’ in expected paths.”}

Playbook -  
[ansible@localhost playbooks]$ vi userscreate.yml

- hosts: 10.10.0.5  
become: yes  
vars:  
grouplist:
- devops
- dbadbmins
- serveradmins

users:

- frank
- joe
- dave

tasks:

- name: Create groups  
group:  
name: “{{ item }}”  
state: present  
with\_items: “{{grouplist}}”  
ignore\_errors: yes

- name: Create users  
user:  
name: “{{ item }}”  
state: present  
with\_items: “{{users}}”

- name: create the users .ssh directories  
file:  
path: “/home/{{item}}/.ssh”  
state: directory  
owner: “{{item}}”  
group: “{{item}}”  
register: user\_dirs  
with\_items: “{{users}}”

- name: Copy ths ssh public key into the authorized key dir on the remote host  
copy

src: “/home/{{item}}/.ssh/id\_rsa.pub”  
dest: “/home/{{item}}/.ssh/authorized\_keys”  
owner: “{{item}}”  
group: “{{item}}”

with\_items: “{{users}}”  
ignore\_errors: true

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [July 9, 2017, 12:35am UTC](https://forum.ansible.com/t/playbook-failys-atc-copying-ssh-keys/24933/2 "2017-07-09T00:35:55Z")

</div>

It's this part:

&nbsp;&nbsp;&nbsp;&nbsp;- name: Copy ths ssh public key into the authorized key dir on the  
remote host  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy  
src: "/home/{{item}}/.ssh/id\_rsa.pub"

are the public keys at those paths on the Ansible host?

---

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [July 10, 2017, 2:39am UTC](https://forum.ansible.com/t/playbook-failys-atc-copying-ssh-keys/24933/3 "2017-07-10T02:39:03Z")

</div>

Yes. Definitely. I even remove the variables in that part, and just put in /home/dave/.ssh/id\_rsa.pub

Generated the same errors.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 10, 2017, 10:28am UTC](https://forum.ansible.com/t/playbook-failys-atc-copying-ssh-keys/24933/4 "2017-07-10T10:28:54Z")

</div>

Permissions perhaps?  
Keep in mind that you are able to use elevated privileges on the  
target host, so you have no problem writing files to user's home  
directories.  
But it's likely that your own local account (the one you run your  
playbook as) usually does \*not\* have permission to access other local  
accounts (such as /home/dave) for obvious reasons.

Dick

---

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [July 10, 2017, 2:14pm UTC](https://forum.ansible.com/t/playbook-failys-atc-copying-ssh-keys/24933/5 "2017-07-10T14:14:29Z")

</div>

Good point. The fact the playbook has become: yes in it - does that not apply to the commands that run on the localhost and the target? This playbook is just running against the target 10.10.0.5

Maybe I’m misunderstanding the use of the elevated permissions…

---

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [July 12, 2017, 6:11pm UTC](https://forum.ansible.com/t/playbook-failys-atc-copying-ssh-keys/24933/6 "2017-07-12T18:11:20Z")

</div>

How would I get around this issue on the localhost? I tried adding become\_user: root and that didnt work either

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [July 12, 2017, 6:22pm UTC](https://forum.ansible.com/t/playbook-failys-atc-copying-ssh-keys/24933/7 "2017-07-12T18:22:26Z")

</div>

Copy the public keys into the playbook directory.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 12, 2017, 6:26pm UTC](https://forum.ansible.com/t/playbook-failys-atc-copying-ssh-keys/24933/8 "2017-07-12T18:26:07Z")

</div>

It must be something with your setup.

Does this command work for the same user you are running ansible-playbook with?

sudo cat /home/{frank,joe,dave}/.ssh/id\_rsa.pub

---

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [July 12, 2017, 6:53pm UTC](https://forum.ansible.com/t/playbook-failys-atc-copying-ssh-keys/24933/9 "2017-07-12T18:53:24Z")

</div>

Actually no, the playbook is running as the ansible user so cannot read those directories…

Thanks

---

<div class="post-metadata">

**Author:** ![Anfield](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Anfield](https://forum.ansible.com/u/Anfield)\
**Post date:** [July 14, 2017, 3:56pm UTC](https://forum.ansible.com/t/playbook-failys-atc-copying-ssh-keys/24933/10 "2017-07-14T15:56:49Z")

</div>

Thanks. So its clear why this didnt work. Can I ask you how I can tell the playbook to switch to root to execute the commands on the localhost with escalated privileges?

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [July 14, 2017, 4:02pm UTC](https://forum.ansible.com/t/playbook-failys-atc-copying-ssh-keys/24933/11 "2017-07-14T16:02:33Z")

</div>

sudo ansible-playbook .....

will work but then you're running the whole play as local root, which  
feels wrong.

I can't think why you'd want to do that, if you aren't making changes  
to that host.

In this example you're shipping public keys, there's no downside to having them  
locally (or better still version controlled, so you remove a  
particular workstation  
as a single point of failure).

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 14, 2017, 5:25pm UTC](https://forum.ansible.com/t/playbook-failys-atc-copying-ssh-keys/24933/12 "2017-07-14T17:25:35Z")

</div>

What is do is adding all the public keys to the ansible configuration and copies it out from there.

But i guess you could have to plays in one playbook.

One that runs against localhost with become and use slurp module to get the content in memory/variable.

And in play number two copy the content of the variable in the first play out to the authorized\_keys.

Or as Dick say, run ansible-playbook as root.
