inventory_hostname in path to create directory with win_file grief..

Hi,

I thought I had this project nailed and then they tell me they need a directory created called:

c:\working\hostname

where hostname is the name of the computer…

I thought I could use:

  • name: make working dir
    win_file:
    path: “c:\working{{ inventory_hostname }}”
    state: directory

but it just keeps telling me it thinks I am probably missing a quote. I have seen examples sort of like this but with the variable at the beginning.

am I way off course?

Thanks as always.

Hi

You seem to be close, one thing to remember when dealing with Windows paths in double quotes is that you needs to properly escape the backslashes. E.g. your path could be one of the following (they all are the same after yaml parsing)

`

when not using quotes, no need to escape anything

path: C:\working{{ inventory_hostname }}

when using single quotes, you don’t need to escape backslashes

path: ‘C:\working{{ inventory_hostname }}’

when using double quotes, you need to escape backslashes, e.g. \n is newline with \n is “\n” as text

path: “C:\working\{{ inventory_hostname }}”

`

We have some guides around this here https://docs.ansible.com/ansible/latest/user_guide/windows_usage.html#path-formatting-for-windows but ultimately I try to avoid quotes if possiible and if I need to use them, then I favor single quotes.

Thanks

Jordan

Thank You Jordan.

I tried first without quotes and was getting an error…I usually try without quotes… anyway, then I tried it as with double quotes and escaping the \’s and it worked… very curious will have to try without quotes at all again and see if perhaps I had some other error and was misinterpreting it as it is my first time using system variables in anger.

Bill

If you start with a jinja2 block or have ": " (with the leading space) in the value then you have to quote the whole line, otherwise you usually can get away with it.

s/leading/trailing/