我正在尝试设置一个Ansible剧本,它将在Windows上运行一个PowerShell脚本,并在其上set_fact结果,并使用setup返回所有事实。
这是我的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变量并将其放在stdout上:
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不应该永久地在主机上设置事实,但它们不应该在一个剧本中持久存在吗?
发布于 2016-03-09 21:44:44
setup不包括您设置的事实,只包括默认设置。
https://stackoverflow.com/questions/33379608
复制相似问题