0
votes

I have an application which creates a directory actually named %UserProfile%. I cannot figure how to make Ansible understand and work with that name.

I want to amongst other things create directory D:\Downloads\CloneApp\cloneapptorun\Backup\Windows Documents\%UserProfile%.

I cannot find the way to tell ansible to do that which works. I have tried many combinations. Below code is the base structure I use:

- name: Create local staging cloneapp package directory
  win_file:
    path: "{{ remote_cloneapp_package_folder }}"
    state: directory
- name: Create local staging cloneapp package userprofile documents documents directory
  win_file:
    path: /unsafe "{{ remote_cloneapp_package_folder }}\\Backup\Windows Documents\\{%raw%}%UserProfile%{%endraw%}"
    state: directory

Giving:
fatal: [192.168.2.96]: FAILED! => {"changed": false, "msg": "Get-AnsibleParam: Parameter 'path' has an invalid path '/unsafe \"c:\\Downloads\\CloneApp\\cloneapptorun\\\\Backup\\Windows Documents\\\\C:\\Users\\mbengtss\"' specified."}

I have tried many combinations on the last path part. Like:

  1. path: "{{ remote_cloneapp_package_folder }}\Backup\Windows Documents\{%raw%}.%UserProfile.%{%endraw%}"

    Giving incorrect syntax, not runnable, directly from ansible

  2. path: '{{ remote_cloneapp_package_folder }}\Backup\Windows Documents{{ %raw% }}%UserProfile%{{ %endraw% }}'

    Giving: fatal: [192.168.2.96]: FAILED! => {"msg": "template error while templating string: unexpected '%'. String: {{ remote_cloneapp_package_folder }}\Backup\Windows Documents\{{ %raw% }}%UserProfile%{{ %endraw% }}"}

  3. path: {{ remote_cloneapp_package_folder }}\Backup\Windows Documents{{ % }}UserProfile{{ % }}

    Giving: fatal: [192.168.2.96]: FAILED! => {"msg": "template error while templating string: unexpected '%'. String: {{ remote_cloneapp_package_folder }}\Backup\Windows Documents\{{ % }}UserProfile{{ % }}"}

  4. path: '{{ remote_cloneapp_package_folder }}\Backup\Windows Documents\%UserProfile%'

    Giving: fatal: [192.168.2.96]: FAILED! => {"changed": false, "msg": "The given path's format is not supported."}

  5. path: '"{{ remote_cloneapp_package_folder }}\Backup\Windows Documents\%UserProfile%"'

    Giving: fatal: [192.168.2.96]: FAILED! => {"changed": false, "msg": "The given path's format is not supported."}

  6. path: "{{ remote_cloneapp_package_folder }}\Backup\Windows Documents\\%UserProfile\%"

    Giving: incorrect syntax, not runnable, directly from ansible

None of the tries lead to Ansible creating the wanted directory, although the first directory part is created as wanted.

How do I define the last path string so it works? I do want the directory to remain as it is called. So the question is not about changing directory name, it is about getting Ansible to understand and create the directory name as it is currently named.

--- Mats ---

1
Hi Mats, welcome to SO! You will want to include the actual output ansible emits when it tries to do what you asked, since we cannot just guess what behavior you are experiencing. You'll find the how to ask page helpful, with especial emphasis on the MCVE part. Good luck! - mdaniel
Thank you for an answer. I have now added outputs for the examples given. Please note, I have tried other combinations. I really hope for someone able to tell a combination that works. - Mats Bengtsson

1 Answers

0
votes

Whew, that one was brutal, but I learned a ton about ansible as a side-effect of setting up a test environment for your question.

The answer seems to be to take advantage of that % environment expansion behavior to inject a value that is, itself, not subject to environment expansion:

- win_file:
     path: 'c:\temp\%sneaky%'
     state: directory
  environment:
     # any name will do, so long as it is not an existing env-var
     sneaky: '%UserProfile%'

However, depending on how many other places you are making reference to that very oddly named directory, you will likely have much, much better luck naming it something reasonable and then only renaming it to the "desired" name at the very last minute (taking inspiration from the win_command docs, which allow using args: stdin: to escape quoting hell):

- win_command: powershell.exe -
  args:
    stdin: 'mv c:\temp\something c:\temp\%UserProfile%'

yes, I realize one could also use win_command to create the directory in that first code snippet, but I was more interested in finding if there was any way to do what you originally asked