Search code examples
bashansibledot-source

using the unix command "source" in ansible


I have the following ansible playbooks:

runWithDotSlash.yml

---
 - hosts: [email protected]
   tasks:
   - name: try running a script with dot slash
     command: ./script.sh

runWithSource.yml

---
 - hosts: [email protected]
   tasks:
   - name: try running a script with source
     command: source script.sh

When I ssh into [email protected] I am taken to the home directory of user and I can run script.sh both with dot slash and with source. However only the first playbook works.

I am running the playbooks with the following command:

ansible-playbook runWithDotSlash.yml
ansible-playbook runWithSource.yml

The second gives the following error message:

FAILED! => {"changed": false, "cmd": "source script.sh", "msg": "[Errno 2] No such file or directory", "rc": 2}

Here is script.sh which is in the home directory of user on myhost

#!/bin/bash
echo $1 > ansible_tempfile

Why does source not work? What can I do to make it work?


Solution

  • source is a bash command. By default, the command module uses sh. Calling bash -c 'source script.sh' should work.