Trying to copy script from local to remote and run

83 views
Skip to first unread message

Chris Bidwell - NOAA Federal

unread,
Oct 11, 2018, 4:30:35 PM10/11/18
to ansible...@googlegroups.com
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
---
- name: Check for reboot
  hosts: testserver
  become: yes

  vars_files:
    - passwd.yml
    - vars.yml

  vars:
    script_dir: "/home/tsg/scripts"

  tasks:
    - name: Does script directory exist?
      stat:
        path: "{{ script_dir }}"
      register: dir_exists

    - name: Does script exist? 
      stat:
        path: "{{ script_dir }}/needs-restarting.py"
      register: fic

    - name: Create scripts dir
      file:
        state: directory
        path: "{{ script_dir }}"
        owner: tsg
        group: tsg
        mode: 0755
      when: dir_exists.stat.exists == false
 
    - name: Copy script
      copy:
        src: "{{ script_dir }}/needs-restarting.py"
        dest: "{{ script_dir }}/needs-restarting.py"
        owner: tsg
        group: tsg
        mode: 0755
      when: fic.stat.exists == false

    - name: Check for reboot
      command: "{{ script_dir }}/needs-restarting.py -r"
      register: reboot_reqd
      ignore_errors: true
      changed_when: false

    - name: "Rebooting {{ ansible_hostname }}"
      shell: sleep 1 && reboot
      async: 30
      poll: 1
      ignore_errors: true
      when: reboot_reqd.rc == 1

    - name: Wait for ssh to come back available
      wait_for:
        host: "{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}"
        port: 22
        search_regex: OpenSSH
        delay: 10
        timeout: 240
      vars:
        ansible_connection: local
      when: reboot_reqd.rc == 1

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

Kai Stian Olstad

unread,
Oct 15, 2018, 12:45:54 PM10/15/18
to ansible...@googlegroups.com
On Thursday, 11 October 2018 22.30.13 CEST 'Chris Bidwell - NOAA Federal' via Ansible Project wrote:
> cat check_for_reboot.yml
> ---
> - name: Check for reboot
> hosts: testserver
> become: yes
>
> vars_files:
> - passwd.yml
> - vars.yml
>
> vars:
> script_dir: "/home/tsg/scripts"
>
> tasks:
> - name: Does script directory exist?
> stat:
> path: "{{ script_dir }}"
> 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.


>
> - name: Does script exist?
> stat:
> path: "{{ script_dir }}/needs-restarting.py"
> 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.
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

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

- name: "Rebooting {{ ansible_hostname }}"
shell: sleep 2 && reboot
async: 1
poll: 0
when: reboot_reqd.rc == 1


> - name: Wait for ssh to come back available
> wait_for:
> host: "{{
> (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}"
> port: 22
> search_regex: OpenSSH
> delay: 10
> timeout: 240
> vars:
> ansible_connection: local
> 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.


--
Kai Stian Olstad


Chris Bidwell - NOAA Federal

unread,
Oct 16, 2018, 7:09:44 PM10/16/18
to ansible...@googlegroups.com
So I simplified my playbook a bit.  I disabled selinux on both servers and I'm still getting an error.  

---
- name: Check for reboot
  hosts: baseserver-lx
  become: yes

  vars_files:
    - passwd.yml
    - vars.yml

  vars:
    script_dir: "~/"
    script: "~/needs-restarting.py"

  tasks:
    - name: Copy script
      copy:
        src: "{{ script }}"
        dest: "{{ script }}"
        owner: root
        group: root
        mode: 0700

    - name: Check for reboot
      command: "{{ script }}"
      register: reboot_reqd
      ignore_errors: true
      changed_when: false

    - name: "Rebooting {{ ansible_hostname }}"
      shell: sleep 1 && reboot
      async: 30
      poll: 1
      ignore_errors: true
      when: 
        - reboot_reqd.rc == 1

    - name: Wait for ssh to come back available
      wait_for:
        host: "{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}"
        port: 22
        search_regex: OpenSSH
        delay: 10
        timeout: 240
      vars:
        ansible_connection: local
      when: reboot_reqd.rc == 1

ERROR:
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: [baseserver-lx]: FAILED! => {"changed": false, "msg": "Could not find or access '~/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"}


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1921487.jDjfVBoyrm%40x1.
For more options, visit https://groups.google.com/d/optout.


--

Chris Bidwell, CISSP
Space Weather Prediction Center
National Oceanic Atmospheric Administration
email: chris.b...@noaa.gov
office: 303-497-3204
mobile: 720-496-3126

Kai Stian Olstad

unread,
Oct 17, 2018, 2:42:33 AM10/17/18
to ansible...@googlegroups.com
On 17.10.2018 01:09, 'Chris Bidwell - NOAA Federal' via Ansible Project
wrote:
> So I simplified my playbook a bit. I disabled selinux on both servers
> and
> I'm still getting an error.
>
> ---
> - name: Check for reboot
> hosts: baseserver-lx
> become: yes
>
> vars_files:
> - passwd.yml
> - vars.yml
>
> vars:
> script_dir: "~/"
> script: "~/needs-restarting.py"

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


--
Kai Stian Olstad

Chris Bidwell - NOAA Federal

unread,
Oct 17, 2018, 7:26:45 AM10/17/18
to ansible...@googlegroups.com
I've tried that with the same results.  Hmm
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/37e8d06cf2ea27fee5845d9be21eb5d9%40olstad.com.

For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Oct 17, 2018, 7:41:35 AM10/17/18
to ansible...@googlegroups.com
On 17.10.2018 13:26, 'Chris Bidwell - NOAA Federal' via Ansible Project
wrote:
> I've tried that with the same results. Hmm

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
strace -o strace.log ansible-playbook ......
And check the strace.log to see if that has some clues.


--
Kai Stian Olstad

Chris Bidwell - NOAA Federal

unread,
Oct 17, 2018, 11:01:22 AM10/17/18
to ansible...@googlegroups.com
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.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/60557ae06200b67eaef10046b4c1eb7a%40olstad.com.
Reply all
Reply to author
Forward
0 new messages