Ansible的`set_fact`根本不起作用

时间:2015-10-27 22:29:50

标签: ansible-playbook

我正在尝试设置一个Ansible playbook,它将在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   

请注意,jsonstring都不存在,其预期值也不存在;并且ansible_fqdn未按预期更改值。

我知道set_fact不应该在主机上设置永久的事实,但它们不应该是在剧本中持久吗? < / p>

1 个答案:

答案 0 :(得分:1)

setup不包含您设置的事实,仅包含默认值。