I am trying to escape the output of cachedNodeModulesPath
with \"
According Manipulating strings you could probably use
{{ cachedNodeModulesPath | quote }}
To add quotes for shell usage
Furthermore, there are other notations possible in YAML. In example by using |
, literal block operator like
- name: Exec sh script
shell:
cmd: |
docker container run --rm --entrypoint='' -u root \
-v {{ codePath }}:/usr/src/app \
-w /usr/src/app \
node:16.4.0 /bin/bash -c 'mv {{ cachedNodeModulesPath | quote }} node_modules || YARN_CACHE_FOLDER=/yarn-cache yarn install'
register: result
... only partially tested because of lack of environment
At least, for a key value of
cachedNodeModulesPath: "/home/{{ ansible_user }}/test"
it will try to execute
/bin/bash -c 'mv /home/user/test node_modules || YARN_CACHE_FOLDER=/yarn-cache yarn install'
If double quotes are needed and not single quotes
vars:
codePath: "/home/{{ ansible_user }}"
cachedNodeModulesPath: "/home/domain user/test"
tasks:
- name: Exec sh script
shell:
cmd: |
docker container run --rm --entrypoint='' -u root \
-v {{ codePath }}:/usr/src/app \
-w /usr/src/app \
node:16.4.0 /bin/bash -c 'mv "{{ cachedNodeModulesPath }}" node_modules || YARN_CACHE_FOLDER=/yarn-cache yarn install'
register: result
will try to execute
/bin/bash -c 'mv "/home/domain user/test" node_modules || YARN_CACHE_FOLDER=/yarn-cache yarn install'
Similar Q&A