Я пытаюсь настроить игру Ansible, которая будет запускать сценарий PowerShell на сервере Windows, set_fact
результатов на нем и вернуть все факты, используя setup
.Ansible `set_fact` не работает вообще
Вот мой playbook.yml
:
---
- hosts: windows
gather_facts: no
tasks:
- name: Get a PowerShell script to work
script: files/gather-windows-facts.ps1
register: ps1_script
- debug: var=ps1_script
- name: Put the PS output into the host's facts
set_fact:
string: "HALLO WELT!"
json: "{{ ps1_script.stdout }}"
ansible_fqdn: EXISTING_VARIABLE_CHANGED
register: store_facts
- debug: var=store_facts
- name: Get the facts from the host
setup: {}
register: setup_step
- debug: var=setup_step
Мой PowerShell скрипт files/gather-windows-facts.ps1
просто манекен в данный момент; она устанавливает переменную JSON и помещает его на стандартный вывод:
ConvertFrom-Json "{HELLO: 'WORLD!'}" | Set-Variable object
Get-Variable object -ValueOnly | ConvertTo-Json
Я бегу этот сценарий:
ansible-playbook -i inventory playbook.yml
И это то, что я получаю:
PLAY [windows] ****************************************************************
GATHERING FACTS ***************************************************************
ok: [10.10.10.10]
TASK: [Get a PowerShell script to work] ***************************************
changed: [10.10.10.10]
TASK: [debug var=ps1_script] **************************************************
ok: [10.10.10.10] => {
"var": {
"ps1_script": {
"changed": true,
"invocation": {
"module_args": "files/gather-windows-facts.ps1",
"module_name": "script"
},
"rc": 0,
"stderr": "",
"stdout": "{\r\n \"HELLO\": \"WORLD!\"\r\n}\r\n",
"stdout_lines": [
"{",
" \"HELLO\": \"WORLD!\"",
"}"
]
}
}
}
TASK: [Put the PS output into the host's facts] *******************************
ok: [10.10.10.10]
TASK: [debug var=store_facts] *************************************************
ok: [10.10.10.10] => {
"var": {
"store_facts": {
"ansible_facts": {
"ansible_fqdn": "EXISTING_VARIABLE_CHANGED",
"json": {
"HELLO": "WORLD!"
},
"string": "HALLO WELT!"
},
"invocation": {
"module_args": "",
"module_name": "set_fact"
}
}
}
}
TASK: [Get the facts from the host] *******************************************
ok: [10.10.10.10]
TASK: [debug var=setup_step] **************************************************
ok: [10.10.10.10] => {
"var": {
"setup_step": {
"ansible_facts": {
"ansible_distribution": "Microsoft Windows NT 6.3.9600.0",
"ansible_distribution_version": "6.3.9600.0",
"ansible_fqdn": "vagrant-2012-r2",
"ansible_hostname": "VAGRANT-2012-R2",
"ansible_interfaces": [
{
"default_gateway": "10.0.2.2",
"dns_domain": "datacom.net.nz",
"interface_index": 12,
"interface_name": "Intel(R) PRO/1000 MT Desktop Adapter"
},
{
"default_gateway": null,
"dns_domain": null,
"interface_index": 14,
"interface_name": "Intel(R) PRO/1000 MT Desktop Adapter #2"
}
],
"ansible_ip_addresses": [
"10.0.2.15",
"fe80::e488:b85c:5262:ff86",
"10.10.10.10",
"fe80::f9f9:a58a:ec2a:701d"
],
"ansible_os_family": "Windows",
"ansible_powershell_version": 4,
"ansible_system": "Win32NT",
"ansible_totalmem": 2147483648
},
"changed": false,
"invocation": {
"module_args": "",
"module_name": "setup"
}
}
}
}
PLAY RECAP ********************************************************************
10.10.10.10 : ok=7 changed=1 unreachable=0 failed=0
Обратите внимание, что json
и string
оба отсутствуют, как и их предполагаемые значения; и ansible_fqdn
не изменилось его значение как и ожидалось.
Я знаю set_fact
не должен установить факты на постоянной основе на хосте, но они не должны быть стойкими в сборник пьес?
При дальнейших исследованиях кажется, что переменные на самом деле устанавливаются на хосте, но, похоже, 'setup' не возвращает их. Получение значения 'hostvars [inventory_hostname]' дает мне ожидаемые результаты (а также результаты промежуточных шагов, которые я не уверен, что хочу). Является ли моя проблема с 'setup' вместо этого? – PJSCopeland