Reputation: 1701
I would like to render "
as is in my Ansible code below.
I am trying to escape the output of cachedNodeModulesPath
with \"
expecting that the resulting command is like this mv "/src/somepath/nodemodules" || restofcommand
.
What I get instead is mv \"restofcommand
and my script fails during execution.
My Ansible code is:
- name: "yarn install {{ codePath }}"
shell: "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'"
Upvotes: 1
Views: 627
Reputation: 12009
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
Upvotes: 2