# variables passed to modules getting escaped

**URL:** <https://forum.ansible.com/t/variables-passed-to-modules-getting-escaped/22028>\
**Category:** Ansible Project\
**Created:** [July 15, 2016, 9:48am UTC](https://forum.ansible.com/t/variables-passed-to-modules-getting-escaped/22028 "2016-07-15T09:48:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Kueck](https://avatars.discourse-cdn.com/v4/letter/p/e19b73/32.png) [@Philippe\_Kueck](https://forum.ansible.com/u/Philippe_Kueck)\
**Post date:** [July 15, 2016, 9:48am UTC](https://forum.ansible.com/t/variables-passed-to-modules-getting-escaped/22028/1 "2016-07-15T09:48:38Z")

</div>

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  
> &nbsp;&nbsp;gather\_facts: no  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: "variable testing"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testmodule:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a\_foo: '/a/regular/expression(/.\*)?'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a\_bar: 'a string with "special" chars'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a\_baz: 'a simple string'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a\_yatta: 'word'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b\_foo: "/a/regular/expression(/.\*)?"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b\_bar: "a string with 'special' chars"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b\_baz: "a simple string"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 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
