Since 查找插件 https://docs.ansible.com/ansible/latest/plugins/lookup.html
像所有人一样模板化 https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_templating.html#playbooks-templating...在 Ansible 控制机上执行并评估...
the envLookup – 读取环境变量的值 https://docs.ansible.com/ansible/latest/collections/ansible/builtin/env_lookup.html
允许您在调用 Ansible 时查询控制器上可用的环境变量。
要收集远程节点上的环境,建议gather_facts有关env https://stackoverflow.com/a/71692925/6771046。这也适用于 Windows 节点。
---
- hosts: windows
become: false
gather_facts: true
gather_subset:
- "env"
- "!all"
- "!min"
tasks:
- name: Show Gathered Facts
debug:
msg: "{{ ansible_facts }}"
答案下是否可以在 Ansible 中仅收集特定事实? https://stackoverflow.com/a/71692925/6771046还展示了如何调试。对于 Windows 节点,它可能看起来像
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"
之后的任务就像
- name: Show Specific Fact
debug:
msg: "{{ ansible_facts.env.USERPROFILE }}"
是可能的,并且可以提供所请求的信息。
由于几乎没有关于从 Windows 远程节点收集的内容的可用文档,因此我喜欢引用以下示例数据结构:
- Windows Node 收集的事实YAML https://github.com/automateyournetwork/WindowsFacts/blob/master/documentation/servers/WINDOWS/yaml/server_facts.yml
- Windows Node 收集的事实JSON https://github.com/automateyournetwork/WindowsFacts/blob/master/documentation/servers/WINDOWS/json/server_facts.json
它们来自一个使用 Ansible 收集 Windows 事实的 Ansible 手册setup module https://github.com/automateyournetwork/WindowsFacts/tree/master。它还在一些示例中展示了如何利用事实。