inventory_hostname in path to create directory with win_file grief..

20 views
Skip to first unread message

William Dossett

unread,
May 23, 2018, 5:57:24 PM5/23/18
to Ansible Project
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.

Jordan Borean

unread,
May 23, 2018, 6:07:41 PM5/23/18
to Ansible Project
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

william...@gmail.com

unread,
May 24, 2018, 9:30:08 AM5/24/18
to ansible...@googlegroups.com

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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/54b57262-6739-4488-8c83-4203640849dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jordan Borean

unread,
May 24, 2018, 4:39:50 PM5/24/18
to Ansible Project
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.

Jordan Borean

unread,
May 24, 2018, 4:40:06 PM5/24/18
to Ansible Project
s/leading/trailing/
Reply all
Reply to author
Forward
0 new messages