Jordi
Jordi

Reputation: 23237

Ansible: perform shell script

This is my ansible snippet:

- name: Moves backup files into mssql location environment
      script: ./restore-backups.sh {{ ansible_default_ipv4.address|default(ansible_all_ipv4_addresses[0]) }}
      args:
        executable: bash

I'm trying to perform an script is located under home directory:

~ ls -lh
total 1.3M
-rw-r--r--. 1 vagrant vagrant  190 Nov  7 21:32 restore-backups.sh

Ansible is getting me:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Could not find or access './restore-backups.sh'\nSearched in:\n\t/vagrant/provisioning/files/./restore-backups.sh\n\t/vagrant/provisioning/./restore-backups.sh\n\t/vagrant/provisioning/files/./restore-backups.sh\n\t/vagrant/provisioning/./restore-backups.sh on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}

I don't quite figure out what's wrong.

Any ideas?

Upvotes: 0

Views: 88

Answers (1)

gary lopez
gary lopez

Reputation: 1954

Ansible is looking for /vagrant/provisioning/./restore-backups.sh. You don't need to use ./ to execute script and you can set the directory of your script with chdir parameter. Try as below

- name: Moves backup files into mssql location environment
  script: restore-backups.sh {{ ansible_default_ipv4.address|default(ansible_all_ipv4_addresses[0]) }}
    chdir: /home/vagrant #Home vagrant path
    args:
        executable: bash

Link to documentation of script module, there are some examples how use it. https://docs.ansible.com/ansible/latest/collections/ansible/builtin/script_module.html

Upvotes: 1

Related Questions