# Sourcing a script on a remote host to use the functions in the script

**URL:** <https://forum.ansible.com/t/sourcing-a-script-on-a-remote-host-to-use-the-functions-in-the-script/30225>\
**Category:** Ansible Project\
**Created:** [April 30, 2019, 3:32pm UTC](https://forum.ansible.com/t/sourcing-a-script-on-a-remote-host-to-use-the-functions-in-the-script/30225 "2019-04-30T15:32:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bren85](https://avatars.discourse-cdn.com/v4/letter/b/bcef8e/32.png) [@bren85](https://forum.ansible.com/u/bren85)\
**Post date:** [April 30, 2019, 3:32pm UTC](https://forum.ansible.com/t/sourcing-a-script-on-a-remote-host-to-use-the-functions-in-the-script/30225/1 "2019-04-30T15:32:23Z")

</div>

I am trying to source a script on a remote host so I can use the functions in it.  
I have tried:

shell: source [filename.sh] && [function name]  
args:  
executable: /bin/bash

and a lot of variations on this.

Now I have managed to source the script locally by adding the source command to the .bash\_profile file.

The functions are available to me when i log into the remote host and run the functions manually from the terminal.

When I send:

shell: [function name]

to the remote host, it comes up:

“/bin/bash: [function name]: command not found”

Both control and remote node are running /bin/bash  
Is there something I am missing?

---

<div class="post-metadata">

**Author:** ![Sudheer\_S1](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Sudheer\_S1](https://forum.ansible.com/u/Sudheer_S1)\
**Post date:** [April 30, 2019, 3:46pm UTC](https://forum.ansible.com/t/sourcing-a-script-on-a-remote-host-to-use-the-functions-in-the-script/30225/2 "2019-04-30T15:46:59Z")

</div>

Yes, you need a login shell. The Ansible shell module does not use a login shell.

Try:

shell: bash -lc 'source [filename.sh] && [function name]'

---

<div class="post-metadata">

**Author:** ![bren85](https://avatars.discourse-cdn.com/v4/letter/b/bcef8e/32.png) [@bren85](https://forum.ansible.com/u/bren85)\
**Post date:** [April 30, 2019, 3:56pm UTC](https://forum.ansible.com/t/sourcing-a-script-on-a-remote-host-to-use-the-functions-in-the-script/30225/3 "2019-04-30T15:56:21Z")

</div>

Thanks for the quick reply,

That didn’t work either, still comes up with the same error:

“module\_args”: {  
“\_raw\_params”: “bash -lc ‘source /root/vBNG/env.sh && ins’”,  
“\_uses\_shell”: true,  
“argv”: null,  
“chdir”: null,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“stdin”: null,  
“warn”: false  
}  
},  
“msg”: “non-zero return code”,  
“rc”: 127,  
“start”: “2019-04-30 16:54:34.129834”,  
“stderr”: “bash: ins: command not found”,  
“stderr\_lines”: [  
“bash: ins: command not found”

---

<div class="post-metadata">

**Author:** ![Sudheer\_S1](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Sudheer\_S1](https://forum.ansible.com/u/Sudheer_S1)\
**Post date:** [May 1, 2019, 2:39pm UTC](https://forum.ansible.com/t/sourcing-a-script-on-a-remote-host-to-use-the-functions-in-the-script/30225/4 "2019-05-01T14:39:02Z")

</div>

I have not tested this. This if off the top of my head:

You may have to avoid using && and call it like "bash -lc 'source /root/vBNG/env.sh ins'".

Or put everything in a single script including the the `source` command.
