user779159 user779159 - 1 year ago 130
Bash Question

How to run a shell function as a command in Ansible?

I'm using

(, which is essentially a shell script that you source into your shell and then call, for example,
nvm install [version]
. But no matter how I try and call that function, ansible can't seem to find it.

I've tried using the
modules. I've tried using
. I've tried using
sudo -iu
like in, but it doesn't work for me. It must be possible though since it works in that file.

How can I run any shell function in Ansible? In this case I've got a
in my .zshrc which allows me to execute
commands from the interactive shell fine.

Answer Source

You'll need to use the shell module, because you want to run shell commands, and you'll need to source in the nvm script into that environment. Something like:

- shell: |
    source /path/to/nvm
    nvm install ...

Whether or not you use become depends on whether or not you want to run the commands as root (or another user).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download