0

I'm trying to set up an Ansible playbook that will run a PowerShell script on a Windows server, set_fact the results on it, and return all the facts using setup.

Here's my 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

My PowerShell script files/gather-windows-facts.ps1 is just a dummy at the moment; it sets a JSON variable and puts it on stdout:

ConvertFrom-Json "{HELLO: 'WORLD!'}" | Set-Variable object

Get-Variable object -ValueOnly | ConvertTo-Json

I'm running this script:

ansible-playbook -i inventory playbook.yml

And this is what I get:

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   

Notice that json and string are both absent, as are their intended values; and ansible_fqdn hasn't changed its value as expected either.

I know set_fact isn't supposed to set facts permanently on the host, but aren't they supposed to be persistent within a playbook?

PJSCopeland
  • 2,818
  • 1
  • 26
  • 40
  • On further investigation, it seems the variables are in fact being set on the host, but it seems `setup` doesn't return them. Getting the value of `hostvars[inventory_hostname]` gives me the expected results (as well as the results of the interim steps, which I'm not sure I want). Is my problem with `setup` instead? – PJSCopeland Oct 28 '15 at 00:58

1 Answers1

1

setup doesn't include the facts you've set, only the defaults.

PJSCopeland
  • 2,818
  • 1
  • 26
  • 40