0

I have the following ansible playbooks:

runWithDotSlash.yml

---
 - hosts: user@myhost.local
   tasks:
   - name: try running a script with dot slash
     command: ./script.sh

runWithSource.yml

---
 - hosts: user@myhost.local
   tasks:
   - name: try running a script with source
     command: source script.sh

When I ssh into user@myhost.local 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?

phuclv
  • 37,963
  • 15
  • 156
  • 475
user11466558
  • 127
  • 3
  • 10

1 Answers1

4

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

Alassane Ndiaye
  • 4,427
  • 1
  • 10
  • 19
  • in sh the command is `.` (dot). But calling either `.` or `source` with a new shell like that makes zero sense because `source` is supposed to run in the **current shell** – phuclv Jun 30 '21 at 03:54