Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape double-quotes in ansible variable

Tags:

I have an ansible playbook which accepts a variable, passing a variable with quotes in it(its needed), that variable will used to query against DB

Playbook

- name: Execute clear script
  script: scripts/clear-documents.sh {{ids}}

Command

ansible-playbook playbooks/maintenance.yml -i hosts -t clear -e ids=["foo", "bar"]

in this process script receives the input as [foo, bar] instead of ["foo", "bar"]

I tried escaping using backslash but that did not help

ansible-playbook playbooks/maintenance.yml -i hosts -t clear -e ids=[\"foo\", \"bar\"]

Adding double quotes in playbook, makes the input like "[foo,bar]" and not ["foo", "bar"]

script: scripts/clear-documents.sh "{{ids}}"

I searched a lot but did not get any proper solution, is there a way to handler this

Note: ansible version - 2.2.3.0

like image 739
user3764430 Avatar asked May 07 '19 15:05

user3764430


1 Answers

The thing you are looking for is quote, in combination with @JGK's correct usage of -e ids='["foo", "bar"]' because you were not quoting them on the way into ansible, and then you were not quoting them on the way out of ansible in that shell: task

- shell: scripts/clear-documents.sh {{ ids | quote }}
like image 115
mdaniel Avatar answered Oct 10 '22 22:10

mdaniel