# Unable to get double-backslashes right on windows

**URL:** <https://forum.ansible.com/t/unable-to-get-double-backslashes-right-on-windows/23673>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [February 7, 2017, 6:18pm UTC](https://forum.ansible.com/t/unable-to-get-double-backslashes-right-on-windows/23673 "2017-02-07T18:18:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [February 7, 2017, 6:18pm UTC](https://forum.ansible.com/t/unable-to-get-double-backslashes-right-on-windows/23673/1 "2017-02-07T18:18:27Z")

</div>

In my role I have one step which sets up some pathing:

- name: Set consul folder var  
set\_fact:  
consul\_folder: “{{ ansible\_env[‘DatafilesPath’] }}\apps\consul”

This works in Ansible and I can use the ‘consul\_folder’ variable in other plays and all is good.  
I also need to set this path in a config file which gets templated onto the target server. The relevant parts of the json looks like this:

{  
“datacenter”: “{{ consul\_dc\_name }}”,  
“data\_dir”: “{{ consul\_folder }}\data”,

As far as I’m in ansible the data\_dir is a valid path, as all backslashes get normalized into their “real” value. However, on the target node this file ends up like this:

{  
“datacenter”: “default”,  
“data\_dir”: “C:\apps\consul\data”,

From this I gather that Ansible does _not_ normalize json data.

The problem I have is that I need the target json to contain the “double backslash” so that it’s a valid json file.  
The alternative I see right now is to double-double the backslashes but that will get incredibly messy incredibly fast.

I have tried using a jinja2 filter in my template json:

{  
“datacenter”: “{{ consul\_dc\_name }}”,  
“data\_dir”: “{{ consul\_folder | win\_dirname}}\data”,

but the result is the same.

Is there a way for me to get around this so that the double quotes are retained and thus the template json kept valid?

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [February 7, 2017, 6:21pm UTC](https://forum.ansible.com/t/unable-to-get-double-backslashes-right-on-windows/23673/2 "2017-02-07T18:21:49Z")

</div>

> For the record, I also tried the builtin jinja replace filter:

{  
“datacenter”: “{{ consul\_dc\_name }}”,  
“data\_dir”: “{{ consul\_folder |replace(‘',’\') }}\data”,

But that just caused Ansible to choke.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 7, 2017, 7:33pm UTC](https://forum.ansible.com/t/unable-to-get-double-backslashes-right-on-windows/23673/3 "2017-02-07T19:33:58Z")

</div>

I believe the filter to\_json should work

"data\_dir": "{{ consul\_folder | to\_json }}\\data",

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [February 7, 2017, 10:01pm UTC](https://forum.ansible.com/t/unable-to-get-double-backslashes-right-on-windows/23673/4 "2017-02-07T22:01:11Z")

</div>

good idea! I’ll try!
