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
    src: /tmp/
    dest: /tmp/
    owner: root
    mode: 0744

- name: Run copied script on the remote node
  shell: /tmp/ > output.txt
    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