How to execute local script in remote server using Ansible

To run local script in remote server we can use the Ansible modules copy and shell.

Sample yaml snippet is given below.

- name: Ensure local script is copied to the remote node
  copy:
    src: /tmp/script.sh
    dest: /tmp/script.sh
    owner: root
    mode: 0744

- name: Run copied script on the remote node
  shell: /tmp/script.sh > output.txt
  args:
    chdir: /tmp
    creates: output.txt 

Here in the first part, Ansible copying the script from local folder to remote server with required ownership and permissions.

In the second part, Ansible executing the script and saving the output to the mentioned file.

That's all…

Leave a Reply

Your email address will not be published. Required fields are marked *