# Trying to copy script from local to remote and run

**URL:** <https://forum.ansible.com/t/trying-to-copy-script-from-local-to-remote-and-run/28769>\
**Category:** Ansible Project\
**Created:** [October 11, 2018, 8:30pm UTC](https://forum.ansible.com/t/trying-to-copy-script-from-local-to-remote-and-run/28769 "2018-10-11T20:30:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Bidwell](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@Chris\_Bidwell](https://forum.ansible.com/u/Chris_Bidwell)\
**Post date:** [October 11, 2018, 8:30pm UTC](https://forum.ansible.com/t/trying-to-copy-script-from-local-to-remote-and-run/28769/1 "2018-10-11T20:30:13Z")

</div>

Hey all, me again! So I’ve got this playbook that _should_ run fine but it dies at trying to copy. Now I don’t know if it’s because it can’t find it locally having the problem copying it remotely. Here it is:

cat check\_for\_reboot.yml

---

<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:** [October 15, 2018, 4:45pm UTC](https://forum.ansible.com/t/trying-to-copy-script-from-local-to-remote-and-run/28769/2 "2018-10-15T16:45:33Z")

</div>

> cat check\_for\_reboot.yml  
> ---  
> - name: Check for reboot  
> &nbsp;&nbsp;hosts: testserver  
> &nbsp;&nbsp;become: yes
> 
> &nbsp;&nbsp;vars\_files:  
> &nbsp;&nbsp;&nbsp;&nbsp;- passwd.yml  
> &nbsp;&nbsp;&nbsp;&nbsp;- vars.yml
> 
> &nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;script\_dir: "/home/tsg/scripts"
> 
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Does script directory exist?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stat:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ script\_dir }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: dir\_exists

You don't need to do this, the file module will check if the directory exist and create if it doesn't exist.

> &nbsp;&nbsp;&nbsp;&nbsp;- name: Does script exist?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stat:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ script\_dir }}/needs-restarting.py"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: fic

You don't need to do this, the copy module will check if the file exist and create/copy if it doesn't exist.

> &nbsp;&nbsp;&nbsp;&nbsp;- name: Create scripts dir  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: directory  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ script\_dir }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: tsg  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: tsg  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: 0755  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: dir\_exists.stat.exists == false
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Copy script  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ script\_dir }}/needs-restarting.py"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ script\_dir }}/needs-restarting.py"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: tsg  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: tsg  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: 0755  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: fic.stat.exists == false
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Check for reboot  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: "{{ script\_dir }}/needs-restarting.py -r"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: reboot\_reqd  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;changed\_when: false
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: "Rebooting {{ ansible\_hostname }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: sleep 1 && reboot  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;async: 30  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;poll: 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: reboot\_reqd.rc == 1

If you are using Ansible 2.7 I recommend using the reboot module.  
[https://docs.ansible.com/ansible/2.7/modules/reboot\_module.html#reboot-module](https://docs.ansible.com/ansible/2.7/modules/reboot_module.html#reboot-module)

If not you should change it to this to avoid errors.

&nbsp;&nbsp;- name: "Rebooting {{ ansible\_hostname }}"  
&nbsp;&nbsp;&nbsp;&nbsp;shell: sleep 2 && reboot  
&nbsp;&nbsp;&nbsp;&nbsp;async: 1  
&nbsp;&nbsp;&nbsp;&nbsp;poll: 0  
&nbsp;&nbsp;&nbsp;&nbsp;when: reboot\_reqd.rc == 1

> &nbsp;&nbsp;&nbsp;&nbsp;- name: Wait for ssh to come back available  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wait\_for:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host: "{{  
> (ansible\_ssh\_host|default(ansible\_host))|default(inventory\_hostname) }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port: 22  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search\_regex: OpenSSH  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delay: 10  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timeout: 240  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_connection: local  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: reboot\_reqd.rc == 1

Instead of wait\_for I highly recommend using wait\_for\_connection instead because that module will check the remote host is ready to execute Ansible modules before it continues.  
(With the reboot module this is not needed)

> Output:  
> TASK [Create scripts dir]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> changed: [testserver]
> 
> TASK [Copy script]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> An exception occurred during task execution. To see the full traceback, use  
> -vvv. The error was: If you are using a module and expect the file to exist  
> on the remote, see the remote\_src option  
> fatal: [testserver]: FAILED! =\> {"changed": false, "msg": "Could not find  
> or access '/home/tsg/scripts/needs-restarting.py' on the Ansible  
> Controller.\nIf you are using a module and expect the file to exist on the  
> remote, see the remote\_src option"}
> 
> Okay, so I know that file is on the src server (ansible server).
> 
> [tsg@server]$ pwd  
> /home/tsg/scripts  
> [tsg@server scripts]$ll  
> -rwxr-x---. 1 root tsg 8432 Aug 23 17:57 needs-restarting.py

The dot after the permission indicate that your file has a SELinux ACL, so you should check your SELinux log to see if SELinux is blocking Ansible to access the file.

---

<div class="post-metadata">

**Author:** ![Chris\_Bidwell](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@Chris\_Bidwell](https://forum.ansible.com/u/Chris_Bidwell)\
**Post date:** [October 16, 2018, 11:09pm UTC](https://forum.ansible.com/t/trying-to-copy-script-from-local-to-remote-and-run/28769/3 "2018-10-16T23:09:23Z")

</div>

So I simplified my playbook a bit. I disabled selinux on both servers and I’m still getting an error.

---

<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:** [October 17, 2018, 6:42am UTC](https://forum.ansible.com/t/trying-to-copy-script-from-local-to-remote-and-run/28769/4 "2018-10-17T06:42:19Z")

</div>

Tilde for home directory is a concept for the shell not Ansible, you need to use relative or absolute path.

---

<div class="post-metadata">

**Author:** ![Chris\_Bidwell](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@Chris\_Bidwell](https://forum.ansible.com/u/Chris_Bidwell)\
**Post date:** [October 17, 2018, 11:26am UTC](https://forum.ansible.com/t/trying-to-copy-script-from-local-to-remote-and-run/28769/5 "2018-10-17T11:26:35Z")

</div>

I’ve tried that with the same results. Hmm

---

<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:** [October 17, 2018, 11:41am UTC](https://forum.ansible.com/t/trying-to-copy-script-from-local-to-remote-and-run/28769/6 "2018-10-17T11:41:16Z")

</div>

The best is probably starting with basic.

src: in copy module is where the file is on the machine ansible-playbook is executed aka Ansible controller, dest: is where to copy the file on remote host.  
The file need to be accessible by the user executing ansible-playbook.

Since you have turned off SELinux that should not cause any problem, so if you still have problem after checking the above it would run  
&nbsp;&nbsp;&nbsp;strace -o strace.log ansible-playbook ......  
And check the strace.log to see if that has some clues.

---

<div class="post-metadata">

**Author:** ![Chris\_Bidwell](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@Chris\_Bidwell](https://forum.ansible.com/u/Chris_Bidwell)\
**Post date:** [October 17, 2018, 3:01pm UTC](https://forum.ansible.com/t/trying-to-copy-script-from-local-to-remote-and-run/28769/7 "2018-10-17T15:01:01Z")

</div>

So I found that by putting the file in question within the local ansible roles directory, it worked. It somehow didn’t have access to the directory it resided in (Which is actually the users home directory!) Anyway, very strange! Thanks so much for your help in narrowing down the culprits.
