0

script file is not getting executed on target machine using ansible script module.

I have written a script file on control-node wants that to copy it on target machine and then from there i wants to run the script file using ansible script module.

playbook:
---
- name: execute script file on target
  hosts: all
  tasks:
    - name: copy script file
      copy:
        src: /home/ansibleuser/practice-ansible/pre-check.sh
        dest: /home/ansibleuser/
        mode: 0777
    - name: execute the script on destination servers
      script:
        cmd: /home/ansibleuser/pre-check.sh
      args:
       removes: /home/ansibleuser/pre-check.sh
      register: output
Error:
"changed": false,
    "msg": "Could not find or access '/home/ansibleuser/pre-check.sh' on the Ansible Controller.
If you are using a module and expect the file to exist on the remote, see the remote_src option"
symcbean
  • 21,009
  • 1
  • 31
  • 52

1 Answers1

1

The error message seems pretty clear. The script module works by first (a) copying the mentioned script from the control host (your local system) to the remote system and then (b) running it.

You don't need your copy task. You just need to fix the path in the script module:

- name: execute script file on target
  hosts: all
  tasks:
    - name: execute the script on destination servers
      script:
        cmd: /home/ansibleuser/practice-ansible/pre-check.sh
      register: output
larsks
  • 43,623
  • 14
  • 121
  • 180