-2

I need to get the Windows path variable %UserProfile% into an Ansible playbook; e.g. $env:USERPROFILE.

If my Managed Node was a Linux host, I imagine something like ...

vars:
- wPath: "{{ lookup( 'env','USERPROFILE')}}"

... would work.

But the above returns some bracketed jiberish when the Managed Node is Windows and such is useless to me.

How do I get such Windows variable into Ansible?

U880D
  • 8,601
  • 6
  • 24
  • 40
GlennRA
  • 57
  • 3
  • 5
    Lookups always run on the controller, so they are never the correct way to get information from managed nodes. Have you looked at the facts (https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_vars_facts.html#ansible-facts) from the target to see if the information you want is included there? – flowerysong May 24 '23 at 01:45
  • 1
    Can you provide the output of `gather_facts: true` and `debug: var: ansible_facts` so that the data structure and content can be seen? See in example [Ansible - Get Facts from Remote Windows Hosts](https://stackoverflow.com/questions/38962577/). – U880D May 24 '23 at 06:37
  • Great articles! I found what i was looking for. All i have to do now is figure out how to dereference it ;-) ... Thanks! – GlennRA May 24 '23 at 13:55
  • "_I found what I was looking for._", that's good to hear. Would you be able to share some sample output and data structures, as well your solution, maybe as answer, so that others can learn from it too? – U880D May 24 '23 at 15:05

2 Answers2

0

Since Lookup plugins

Like all templating ... execute and are evaluated on the Ansible Control Machine ...

the env lookup – Read the value of environment variables

Allows you to query the environment variables available on the Controller when you invoked Ansible.

To gather the environment on the Remote Node it is recommended to gather_facts about the env. This will work for Windows Nodes too.

---
- hosts: windows
  become: false

  gather_facts: true
  gather_subset:
    - "env"
    - "!all"
    - "!min"

  tasks:

  - name: Show Gathered Facts
    debug:
      msg: "{{ ansible_facts }}"

The answer under Is it possible to gather only specific facts in Ansible? show also how to debug. For a Windows Node it could look like

env:
  ALLUSERSPROFILE: "C:\ProgramData"
  APPDATA: "C:\Users\ansible-user\AppData\Roaming"
  CommonProgramFiles: "C:\Program Files\Common Files"
  CommonProgramFiles(x86): "C:\Program Files (x86)\Common Files"
  CommonProgramW6432: "C:\Program Files\Common Files"
  COMPUTERNAME: "ANSIBLE-WIN"
  ComSpec: "C:\WINDOWS\system32\cmd.exe"
  DriverData: "C:\Windows\System32\Drivers\DriverData"
  GIT_SSH_COMMAND: "C:\\WINDOWS\\System32\\OpenSSH\\ssh.exe"
  GIT_SSH_VARIANT: "ssh"
  HOMEDRIVE: "H:"
  HOMEPATH: "\"
  HOMESHARE: "\\dfs.internal.example.com\HomeDirs\Users_1\ansible-user"
  LOCALAPPDATA: "C:\Users\ansible-user\AppData\Local"
  LOGONSERVER: "\\AD"
  NUMBER_OF_PROCESSORS: "16"
  OS: "Windows_NT"
  Path: "C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPo..."
  PATHEXT: ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL"
  PROCESSOR_ARCHITECTURE: "AMD64"
  PROCESSOR_IDENTIFIER: "AMD64 Family 25 Model 80 Stepping 0, AuthenticAMD"
  PROCESSOR_LEVEL: "25"
  PROCESSOR_REVISION: "5000"
  ProgramData: "C:\ProgramData"
  ProgramFiles: "C:\Program Files"
  ProgramFiles(x86): "C:\Program Files (x86)"
  ProgramW6432: "C:\Program Files"
  PSModulePath: "C:\Users\ansible-user\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerSh..."
  PUBLIC: "C:\Users\Public"
  SESSIONNAME: "Console"
  SystemDrive: "C:"
  SystemRoot: "C:\WINDOWS"
  TEMP: "C:\Users\ansible-user\AppData\Local\Temp"
  TMP: "C:\Users\ansible-user\AppData\Local\Temp"
  UATDATA: "C:\WINDOWS\CCM\UATData\12345678-90AB-CDEF-0123-4567890ACDEF"
  USERDNSDOMAIN: "INTERNAL.EXAMPLE.COM"
  USERDOMAIN: "INTERNAL"
  USERDOMAIN_ROAMINGPROFILE: "INTERNAL"
  USERNAME: "ansible-user"
  USERPROFILE: "C:\Users\ansible-user"
  windir: "C:\WINDOWS"

After that tasks like

- name: Show Specific Fact
  debug:
    msg: "{{ ansible_facts.env.USERPROFILE }}"

are possible ans which would provide the requested information.

Since there is almost no documentation available about what is gathered from Windows Remote Nodes, I like to reference the sample data structure from

  • Windows Node gathered facts in YAML
  • Windows Node gathered facts in JSON

which are from an Ansible playbook that gathers Windows facts using the Ansible setup module. It shows also in some examples how to work with the facts.

U880D
  • 8,601
  • 6
  • 24
  • 40
0

There's probably a cleaner way to do this, however, i just used what i had once i got it debugged ...

gather_facts: yes
vars:
- upPath: "{{ ansible_facts['user_dir']}}"
...
...
- name: Flag for Shortcuts on Desktop
  ansible.windows.win_stat:
    path: '{{ upPath}}\Desktop\Control.lnk'
  register: Shortcuts

It was easy to assign the UserProfile path (upPath) thanks to flowerysongs contributions, the pain was using this within win_stat; very painful (for me ;-) ).

GlennRA
  • 57
  • 3