Hi,
I'm writing some modules/libraries for ansible (2.1.0.0) and was
wondering why variables are getting escaped and whether or not this is
an intended behaviour.
Let's have a simple example:
# testmodule
#!/bin/sh
cp -f $1 /tmp/testmodule
echo '{"changed": false, "msg": "OK"}'
# playbook
---
- hosts: localhost
gather_facts: no
tasks:
- name: "variable testing"
testmodule:
a_foo: '/a/regular/expression(/.*)?'
a_bar: 'a string with "special" chars'
a_baz: 'a simple string'
a_yatta: 'word'
b_foo: "/a/regular/expression(/.*)?"
b_bar: "a string with 'special' chars"
b_baz: "a simple string"
b_yatta: "word"
expected file content:
either
a_foo="/a/regular/expression(/.*)?"
a_bar="a string with \"special\" chars"
a_baz="a simple string"
a_yatta="word"
b_foo="/a/regular/expression(/.*)?"
b_bar="a string with 'special' chars"
b_baz="a simple string"
b_yatta="word"
or
a_foo='/a/regular/expression(/.*)?'
a_bar='a string with "special" chars'
a_baz='a simple string'
a_yatta='word'
b_foo='/a/regular/expression(/.*)?'
b_bar='a string with '\''special'\'' chars'
b_baz='a simple string'
b_yatta='word'
actual file content:
a_foo="'/a/regular/expression(/.*)?'"
a_bar="'a string with "special" chars'"
a_baz="'a simple string'"
a_yatta="word"
b_foo="'/a/regular/expression(/.*)?'"
b_bar="'a string with '"'"'special'"'"' chars'"
b_baz="'a simple string'"
b_yatta="word"
which results in:
$ source /tmp/testmodule
$ echo $a_foo
'/a/regular/expression(/.*)?' # <- extra single quotes
$ echo $a_bar
'a string with special chars' # <- extra single quotes and
# the double quotes are lost
$ echo $a_baz
'a simple string' # <- extra single quotes
$ echo $a_yatta
word # <- correct
$ echo $b_foo
'/a/regular/expression(/.*)?' # <- extra single quotes
$ echo $b_bar
'a string with '"special"' chars' # <- extra single quotes
$ echo $b_baz
'a simple string' # <- extra single quotes
$ echo $b_yatta
word # <- correct
this forces me to unescape the variables before using actually them,
either with eval:
$ eval echo $a_foo
/a/regular/expression(/.*)?
or, because eval doesn't work with $b_bar, with substitution
$ echo ${b_bar//\'/}
a string with "special" chars
Any thoughts? Am I missing something?
Kind regards
Philippe