# Getting paths right in Windows

**URL:** <https://forum.ansible.com/t/getting-paths-right-in-windows/24424>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [May 9, 2017, 8:23am UTC](https://forum.ansible.com/t/getting-paths-right-in-windows/24424 "2017-05-09T08:23:36Z")\
**Posts on this page:** 6\
**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:** [May 9, 2017, 8:23am UTC](https://forum.ansible.com/t/getting-paths-right-in-windows/24424/1 "2017-05-09T08:23:36Z")

</div>

Hope someone can help me shed some light on this one:

Since Ansible is python-based, us Windows dudes generally have to stick an extra backslash anywhere we’re manipulating Windows paths. However, in some cases this causes unexpected behavior. In my current case, I need to inject a file path into a json file on a Windows box. This path is defined as such in an Ansible var:  
logfiles\_path: “F:\Logs”

In my template json file I add to this path, using the following:

“FilePath”:“{{ logfiles\_path }}\*”,

The goal is to populate the target json with  
“F:\Logs\*”

However, since Ansible kicks in the resulting file contains:  
“F:\Logfiles\*”

In other words, Ansible “normalises” the part of the path that comes from a variable, but not the part “outside” of the variable.

I’m not sure what the best way to solve this is - it would be great to have some builtin filters that would do “json normalization” of a string or something. How are people solving this?

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [May 9, 2017, 12:29pm UTC](https://forum.ansible.com/t/getting-paths-right-in-windows/24424/2 "2017-05-09T12:29:39Z")

</div>

I stick to using single backslashes (and not quotes) in YAML, or single quotes if you have to. And single quotes everywhere else. I never had the need to use double-backslashes.

I remember one issues (with YAML?), which is when using a trailing backslash. So I taught myself not to do this for Windows paths 🙂

It would be nice to document these best-practices as part of the Ansible Windows documentation once we have determined what's best.

---

<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:** [May 15, 2017, 7:38am UTC](https://forum.ansible.com/t/getting-paths-right-in-windows/24424/3 "2017-05-15T07:38:10Z")

</div>

I’m not seeing the behaviour you describe - maybe my playbook isn’t doing the same things as yours though?

# playbook:

$ cat trondpath.yml

---

<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:** [May 16, 2017, 10:28pm UTC](https://forum.ansible.com/t/getting-paths-right-in-windows/24424/4 "2017-05-16T22:28:49Z")

</div>

To me it looks like you’re getting the same as me (I had to zoom in my screen to be sure 🙂 ) - contents of templated json contains a single backslash after the drive letter (which is not valid)

These Json files might be read by applications that are not able to parse “forward-slashed” paths, so although Powershell would be fine with that format, it’s not always an option to use them in json files (so essentially this is not as much a problem with the Powershell-based Ansible modules as the fact that its hard to inject valid json into json files).

I’ll look into the win\_\* filter - I haven’t spent much time with those (I kinda wish they were a bit better documented)

I’ll keep this thread updated with my findings.

---

<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:** [May 17, 2017, 2:37pm UTC](https://forum.ansible.com/t/getting-paths-right-in-windows/24424/5 "2017-05-17T14:37:50Z")

</div>

Sorry, trying to help too early in the morning!. I had missed the single \ in the path.

Not tried but have noticed jinja2 has a ‘tojson’ filter that might help (new in 2.9 so maybe ‘pip install Jinja2 --upgrade’ might be needed, although I think installing ansible 2.3 from pip will drag this in, if I recall. Also there’s a ‘safe’ filter which might stop automatic escaping from happening.

[http://jinja.pocoo.org/docs/2.9/templates/](http://jinja.pocoo.org/docs/2.9/templates/)

Hope this helps,

Jon

---

<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:** [May 18, 2017, 10:33pm UTC](https://forum.ansible.com/t/getting-paths-right-in-windows/24424/6 "2017-05-18T22:33:02Z")

</div>

Thanks, will check those out!
