Sourcing a file before executing commands in Ansible

一笑奈何 提交于 2019-11-29 06:20:34

Regarding the "no such file" error:

source is an internal shell command (see for example Bash Builtin Commands), not an external program which you can run. There is no executable named source in your system and that's why you get No such file or directory error.

Instead of the command module use shell which will execute the source command inside a shell.


Regarding the sourcing problem:

In a with_items loop Ansible will run the shell twice and both processes will be independent of each other. Variables set in one will not be seen by the other.

You should run the two commands in one shell process, for example with:

- name: Install node {{ nvm.node_version }}
  shell: "source /home/centos/.nvm/nvm.sh && nvm install {{ nvm.node_version }}"
  tags: nvm

Other remarks:

Use {{ ansible_env.HOME }} instead of ~ in the git task. Either one will work here, but tilde expansion is the functionality of shell and you are writing code for Ansible.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!