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

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/7309a7e6-3530-338f-0edc-296bf4f70264%40quarantine.de.
For more options, visit https://groups.google.com/d/optout.

Reply via email to