# Ansible quoting

**URL:** <https://forum.ansible.com/t/ansible-quoting/16559>\
**Category:** Ansible Project\
**Created:** [October 20, 2014, 4:21pm UTC](https://forum.ansible.com/t/ansible-quoting/16559 "2014-10-20T16:21:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Perzel](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Michael\_Perzel](https://forum.ansible.com/u/Michael_Perzel)\
**Post date:** [October 20, 2014, 4:21pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/1 "2014-10-20T16:21:55Z")

</div>

I am having trouble getting ansible to parse my variable values correctly.

I have defined a variable:

installArguments: “appPoolName=‘UiService’; dbPassword=‘password’; dbUserId=‘user’”

I then have a playbook that makes the following call:

- name: Deploy Launcher  
script: deployLauncher.ps1 -env {{ env }} -appName {{ appName }} -artifacts {{ artifacts }} -installArguments @{ ‘{{ installArguments }}’ } -deployNumber {{ deployNumber }} {{ switch }}

If I don’t include the single quotes around {{installArguments}} I get a complaint about the equal sign being unquoted.

deployLauncher.ps1 is meant to simply echo back the variables at this point.

param(  
[Parameter(Mandatory=$true,Position=0)][string] $env,  
[Parameter(Mandatory=$true,Position=1)][string] $server,  
[Parameter(Mandatory=$true,Position=2)][string] $appName,  
[Parameter(Mandatory=$true,Position=3)][string] $artifacts, #first contains install script  
[Parameter(Mandatory=$false,Position=4)][hashtable] $installArguments = @{},  
[Parameter(Mandatory=$true,Position=5)][string] $deployNumber,  
[Parameter(Mandatory=$false,Position=6)][switch] $switch  
)

#./testLauncher -env Local -server serverName -appName Name -artifacts artifacts.zip -installArguments @{key=‘value’} -deployNumber 123 -switch  
Write-Output “Env: $env Server: $server AppName: $appName Artifacts: $artifacts InstallArguments: $installArguments DeployNumber: $deployNumber Switch: $switch”

When I run the playbook I get the following powershell error.

TASK: [common | debug var=deployLauncher] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ok: [mdm-wsrv98] =\> { “deployLauncher”: { “changed”: true, “invocation”: { “module\_args”: “deployLauncher.ps1 -env LOCAL -appName UiService.Event -artifacts UiService.Event.zip -installArguments @{ ‘appPoolName=‘UiService’; dbPassword=‘password’; dbUserId=‘user’’ } -deployNumber 456 -switch”, “module\_name”: “script” }, “rc”: 0, “stderr”: “C:\Users\ansible\AppData\Local\Temp\ansible-tmp-1413821995.23-24997058650291\de\r\nployLauncher.ps1 : Cannot process argument transformation on parameter \r\n’installArguments’. Cannot convert the "@{" value of type "System.String" to \r\ntype "System.Collections.Hashtable".\r\n + CategoryInfo : InvalidData: (🙂 [deployLauncher.ps1], ParentCon \r\n tainsErrorRecordException\r\n + FullyQualifiedErrorId : ParameterArgumentTransformationError,deployLaunc \r\n her.ps1\r\n \r\n”, “stdout”: “”, “stdout\_lines”: } }

I think that my issue is that ansible is inserting a single quote after @{ and again after ‘user’ which isn’t valid powershell array syntax. Is there a better way to quote this so that these don’t get included?

Thanks!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 20, 2014, 8:56pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/2 "2014-10-20T20:56:37Z")

</div>

Possibly easier to use the win\_script module maybe?

Ansible shouldn’t be adding any extra spaces here though, particularly not in recent versions.

---

<div class="post-metadata">

**Author:** ![Michael\_Perzel](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Michael\_Perzel](https://forum.ansible.com/u/Michael_Perzel)\
**Post date:** [October 21, 2014, 6:03pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/3 "2014-10-21T18:03:12Z")

</div>

Can you provide a link to the win\_script module? I don’t see it in the ansible documentation.

I’m using ansible 1.7.2. I’ve confirmed the issue is caused by my variable including an equal sign. I need to quote my variable {{ “var” }} in the playbook, but when this variable is used in the script module the quotes are included. For example,

var: @{key=value}

script: powershell.ps1 {{ “var” }}

The call to the windows box is:  
powershell.ps1 “@{key=value}”

instead of:  
powershell.ps1 @{key=value}

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 22, 2014, 3:53am UTC](https://forum.ansible.com/t/ansible-quoting/16559/4 "2014-10-22T03:53:17Z")

</div>

Sorry, I think I mispoke here.

“script” is one of those that works on both platforms.

What you have above is more of a quoting question with what arguments you are passing to the script, not the contents, so that won’t help…

We can investigate, but perhaps others might know the answer to this one?

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [October 22, 2014, 1:38pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/5 "2014-10-22T13:38:11Z")

</div>

> I don’t have a direct answer for this - rightly or wrongly the ps1 scripts I have so far don’t take any arguments - I consider them to be specific to my roles and at the moment they embed some details that it would probably be best to have as parameters. However, it looks to me like your deployLauncher.ps1 really wants to be a very general purpose deployment tool and maybe it would be better off as an ansible module. That way you could perhaps take advantage of ansible’s existing syntax for specifying module arguments.

As an aside I believe @Trond Hindenes has a win\_package module in the works which might do some of what you want.

---

<div class="post-metadata">

**Author:** ![Michael\_Perzel](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Michael\_Perzel](https://forum.ansible.com/u/Michael_Perzel)\
**Post date:** [October 22, 2014, 2:28pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/6 "2014-10-22T14:28:56Z")

</div>

I’ll look into this. My work around thus far is using a template for my deploy script. Instead of passing the deploy script arguments, I just assign them at the top:

$build = {{ build }}  
$pass = {{ password }}  
$arguments = @{ {{ arguments }} }

This then becomes:

$build = 42  
$pass = password  
$arguments = @{ key1=value1; key2= value2 }

Using the template has given me complete control over the syntax. In our paradigm we have an entirely generic “deploy” powershell script that stages files, unzips them and similar things. It then calls an “install” script that handles all the app specific items. The install scripts are bundled with the specific app.

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [October 24, 2014, 5:52am UTC](https://forum.ansible.com/t/ansible-quoting/16559/7 "2014-10-24T05:52:27Z")

</div>

That method of passing arguments is apparently called “splatting” ([http://technet.microsoft.com/en-us/magazine/gg675931.aspx](http://technet.microsoft.com/en-us/magazine/gg675931.aspx)).

I have a branch where I’ve been able to get it working:

> [https://github.com/cchurch/ansible/tree/powershell\_splatting](https://github.com/cchurch/ansible/tree/powershell_splatting)

I’ve also added integration tests to show examples of it in use:

---

<div class="post-metadata">

**Author:** ![Michael\_Perzel](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Michael\_Perzel](https://forum.ansible.com/u/Michael_Perzel)\
**Post date:** [October 24, 2014, 8:52pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/8 "2014-10-24T20:52:39Z")

</div>

Your idea with jinja filter is what I originally tried to get working. I never managed it but I think its the ideal solution (converting yaml hash to a powershell one).

If I follow your patch you added a parameter that controls whether or not arguments get quoted (and set it to false for powershell scripts)? I’m not particularly worried about nefarious activity within our system so this could work for us.

---

<div class="post-metadata">

**Author:** ![Michael\_Perzel](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Michael\_Perzel](https://forum.ansible.com/u/Michael_Perzel)\
**Post date:** [October 24, 2014, 10:12pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/9 "2014-10-24T22:12:06Z")

</div>

I tried taking your changes but it failed with:  
~~C:\Users\ansible\AppData\Local\Temp\ansible-tmp-1414187414.25-97343749557638\de\_x000D\_\_x000A\_~~~~ ployLauncher.ps1 : A positional parameter cannot be found that accepts _x000D\_\_x000A_ ~~~~argument ‘False’._x000D\_\_x000A_~~~~ At line:1 char:1\_x000D\_\_x000A\_ ~~~~+ & _x000D\_\_x000A_~~~~ C:\Users\ansible\AppData\Local\Temp\ansible-tmp-1414187414.25-97343749557638\ _x000D\_\_x000A_ ~~~~…_x000D\_\_x000A_~~~~ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~_x000D\_\_x000A_~_x000D\_\_x000A_~~~~ + CategoryInfo : InvalidArgument: (🙂 [deployLauncher.ps1], Param _x000D\_\_x000A_ ~~~~eterBindingException\_x000D\_\_x000A\_~~~~ + FullyQualifiedErrorId : PositionalParameterNotFound,deployLauncher.ps1\_x000D\_\_x000A\_ ~~~~_x000D\_\_x000A_~~

Looking at the diff between your code and mine there’s a few other differences. I’m running 1.7.2, I assume your branched off something newer.I’ll see if I can sort through the issue.

Thanks

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [October 25, 2014, 7:23pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/10 "2014-10-25T19:23:32Z")

</div>

Could you create a gist ([https://gist.github.com/](https://gist.github.com/)) with the relevant lines from your inventory variables, playbook and script (removing any sensitive information)? I’m not quite able to piece together a full example of what you’re running from the email thread.

---

<div class="post-metadata">

**Author:** ![Michael\_Perzel](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Michael\_Perzel](https://forum.ansible.com/u/Michael_Perzel)\
**Post date:** [October 25, 2014, 8:30pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/11 "2014-10-25T20:30:44Z")

</div>

Take a look at this gist: [https://gist.github.com/perzizzle/b3e13af9e94d9fbb7970/download](https://gist.github.com/perzizzle/b3e13af9e94d9fbb7970/download)

It contains a simple playbook that calls a deploy.ps1 that should echo back installArguments (which is a powershell array). I tried a few different ways of quoting it with your patch applied but I get argument transformation errors. Next I’ll trying doing a git pull on your entire branch, so far I had just been modifying the files you edited. I tested calling deploy.ps1 from a windows server and it does work if provided valid powershell syntax.

.\deploy.ps1 -installArguments @{key=“value”}

I’ll take a look at your tests and make sure I’m not doing anything different.

---

<div class="post-metadata">

**Author:** ![Michael\_Perzel](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Michael\_Perzel](https://forum.ansible.com/u/Michael_Perzel)\
**Post date:** [October 25, 2014, 9:15pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/12 "2014-10-25T21:15:02Z")

</div>

Alright, I made your changes in your 1.7.2 branch and it works if the variables are defined with quotes in the following order (I updated the gist to reflect this).

vars:  
installArguments: “@{key=‘value’}”

although it echo’s the hash value correctly (it is just “value” maybe not the best naming) it returns something in stderr. At this point I have no clue what it means but I’m searching the google.

TASK: [debug var=deploy] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [mdm-wsrv99.surescripts-dev.qa] =\> {  
“deploy”: {  
“changed”: true,  
“invocation”: {  
“module\_args”: “deploy.ps1 -installArguments "@{key=‘value’}"”,  
“module\_name”: “script”  
},  
“rc”: 0,  
“stderr”: “#\< CLIXML\r\n\<Objs Version="1.1.0.1" xmlns="[http://schemas.microsoft.com/powershell/2004/04\](http://schemas.microsoft.com/powershell/2004/04%5C)”\>\<Obj S="progress" RefId="0"\>\<TN RefId="0"\>System.Management.Automation.PSCustomObjectSystem.Object\<I64 N="SourceId"\>1\<PR N="Record"\>Preparing modules for first use.0-1-1Completed-1 “,  
“stdout”: “Install Arguments: value\n”,  
“stdout\_lines”: [  
“Install Arguments: value”  
]  
}  
}  
ok: [mdm-wsrv98.surescripts-dev.qa] =\> {  
“deploy”: {  
“changed”: true,  
“invocation”: {  
“module\_args”: “deploy.ps1 -installArguments "@{key=‘value’}"”,  
“module\_name”: “script”  
},  
“rc”: 0,  
“stderr”: “#\< CLIXML\r\n\<Objs Version="1.1.0.1" xmlns="[http://schemas.microsoft.com/powershell/2004/04\](http://schemas.microsoft.com/powershell/2004/04%5C)”\>\<Obj S="progress" RefId="0"\>\<TN RefId="0"\>System.Management.Automation.PSCustomObjectSystem.Object\<I64 N="SourceId"\>1\<PR N="Record"\>Preparing modules for first use.0-1-1Completed-1 ”,  
“stdout”: “Install Arguments: value\n”,  
“stdout\_lines”: [  
“Install Arguments: value”  
]  
}  
}

---

<div class="post-metadata">

**Author:** ![Michael\_Perzel](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Michael\_Perzel](https://forum.ansible.com/u/Michael_Perzel)\
**Post date:** [November 6, 2014, 9:37pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/13 "2014-11-06T21:37:04Z")

</div>

Are you going to submit a pull request for this into devel? It definitely solved my issue.

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [November 6, 2014, 11:26pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/14 "2014-11-06T23:26:24Z")

</div>

Absolutely, just haven't had a chance to get back to Windows-related things  
this week.

---

<div class="post-metadata">

**Author:** ![Michael\_Perzel](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Michael\_Perzel](https://forum.ansible.com/u/Michael_Perzel)\
**Post date:** [January 26, 2015, 7:47pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/15 "2015-01-26T19:47:42Z")

</div>

I ended up creating a new filter that could transform

installArguments:

key: value

key2: value2

and return “@{‘key2’=‘value2’;‘key’=‘value’;}” I can share if anyone is interested.

I just upgraded to ansible 1.8 and made your fix to powershell.py and winrm.py manually again. Is there any target version to get this into the main branch? I poked around in github but couldn’t find way to see if you had submitted a pull request for this.

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [February 4, 2015, 7:43am UTC](https://forum.ansible.com/t/ansible-quoting/16559/16 "2015-02-04T07:43:35Z")

</div>

Hi Michael,

I had submitted a PR ([https://github.com/ansible/ansible/pull/9602](https://github.com/ansible/ansible/pull/9602)) -- I'll  
see if I can get that merged soon.

---

<div class="post-metadata">

**Author:** ![Michael\_Perzel](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Michael\_Perzel](https://forum.ansible.com/u/Michael_Perzel)\
**Post date:** [February 23, 2015, 5:39pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/17 "2015-02-23T17:39:02Z")

</div>

This isn’t directly related to this thread but it was discussed earlier so it was the best place for it.

I created a filter that converts a yaml dict to a powershell hash. I’m trying to write some tests for it but I’m not sure how to run just my specific test. The file playbook and vars are at ansible/test/integration/roles/test\_powershell\_filter/ and the filter is at ansible/lib/ansible/runner/filter\_plugins.

Test playbook:  
[https://gist.github.com/perzizzle/8fb875895b218772eede](https://gist.github.com/perzizzle/8fb875895b218772eede)  
Test variables  
[https://gist.github.com/perzizzle/dc73025a2232f2da9b88](https://gist.github.com/perzizzle/dc73025a2232f2da9b88)  
Filter  
[https://gist.github.com/perzizzle/117a6b139e2a97194d5f](https://gist.github.com/perzizzle/117a6b139e2a97194d5f)

When I run “make tests” a whole bunch of them succeed but one fails related to postgres. Is it possible to run just my one test I’ve created?

Thanks,

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [February 23, 2015, 7:33pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/18 "2015-02-23T19:33:11Z")

</div>

Add your new test role to test/integration/test\_winrm.yml with a matching tag, then run:

---

<div class="post-metadata">

**Author:** ![Michael\_Perzel](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Michael\_Perzel](https://forum.ansible.com/u/Michael_Perzel)\
**Post date:** [February 23, 2015, 9:16pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/19 "2015-02-23T21:16:45Z")

</div>

I updated test\_winrm.yml to include my role but when I tried to run

ansible-source/ansible\> TEST\_FLAGS=“-t test\_powershell\_filter” make test\_winrm  
make: \*\*\* No rule to make target `test\_winrm’. Stop.

Did you need to make changes to the Makefile in order for this to work?

I updated my playbook. I had intended command: echo … I’ll update test\_win\_script as well once I get this working.

---

<div class="post-metadata">

**Author:** ![Michael\_Perzel](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Michael\_Perzel](https://forum.ansible.com/u/Michael_Perzel)\
**Post date:** [February 23, 2015, 9:37pm UTC](https://forum.ansible.com/t/ansible-quoting/16559/20 "2015-02-23T21:37:03Z")

</div>

I changed my directory so that test\_winrm was in the current directory. But now I get an error message complaining it can’t find the win\_file module. I assume its not finding my ansible-module checkout.

ansible/test/integration\> TEST\_FLAGS=“-t test\_powershell\_filter -i /etc/ansible/hosts” make test\_winrm  
ansible-playbook test\_winrm.yml -i inventory.winrm -e @integration\_config.yml -v -t test\_powershell\_filter -i /etc/ansible/hosts  
[WARNING]: The version of gmp you have installed has a known issue regarding  
timing vulnerabilities when used with pycrypto. If possible, you should update  
it (i.e. yum update gmp).

ERROR: win\_file is not a legal parameter in an Ansible task or handler  
make: \*\*\* [test\_winrm] Error 1

I’ll keep working through this…

[Next page](https://forum.ansible.com/t/ansible-quoting/16559.md?page=2)
