Setup custom facts under Windows Help?

Trying to setup custom facts for Windows. In this case, the powershell info that is shown in $PSVersionTable.

I have tasks to put it all into place and looking at the -vvvv output I do see the information encoded into setup module output. But’s encoded differently then the native facts, example below highlighted in red.

ok: [win10fusion] => {
“ansible_facts”: {
“ansible_PSVersionTable”: “{"Major":5,"Minor":0,"Build":10586,"Revision":494,"MajorRevision":0,"MinorRevision":494}”,
“ansible_architecture”: “64-bit”,
“ansible_bios_date”: “07/01/2015”,
“ansible_bios_version”: “6.00”,
“ansible_date_time”: {
“date”: “2016-08-04”,
“day”: “04”,
“epoch”: “1470354713.15218”,
“hour”: “23”,
“iso8601”: “2016-08-05T04:51:53Z”,
“iso8601_basic”: “20160804T235153120933”,
“iso8601_basic_short”: “20160804T235153”,
“iso8601_micro”: “2016-08-05T04:51:53.120933Z”,
“minute”: “51”,
“month”: “08”,
“second”: “53”,
“time”: “23:51:53”,
“tz”: “Central Standard Time”,
“tz_offset”: “-05:00”,
“weekday”: “Thursday”,
“weekday_number”: “4”,
“weeknumber”: “31”,
“year”: “2016”
},

http://jsonlint.com says the output is valid json, even with the quotes are being escaped.

And it’s encoded as a string?

Here is the powershell script that generates the facts.

$PSVersionTable.PSVersion | ConvertTo-Json -Compress

I thought ansible_PSVersionTable would be a dictionary, like:

“ansible_PSVersionTable”: {"Major":5,"Minor":0,"Build":10586,"Revision":494,"MajorRevision":0,"MinorRevision":494}

The documentation notes says, “… Ansible will take care of this …”

http://docs.ansible.com/ansible/setup_module.html

If the target host is Windows you can now use fact_path. Make sure that this path exists on the target host. Files in this path MUST be PowerShell scripts (*.ps1) and their output must be formattable in JSON (Ansible will take care of this). Test the output of your scripts. This option was added in Ansible 2.1.

What does that mean?

Do I not have to “| ConvertTo-Json” ?

Thanks.

Your guess is right, there’s no converting to JSON needed. The ansible_psversion variable below is just the output of a plain $PSVersionTable.PSVersion, and the module splits it nicely for me:


"ansible_powershell_version": 4,
**"ansible_psversion": {**
**"Build": -1,**
**"Major": 4,**
**"MajorRevision": -1,**
**"Minor": 0,**
**"MinorRevision": -1,**
**"Revision": -1**
**},**
"ansible_system": "Win32NT",

The module expects to get an object back from Powershell, and it splits up the elements accordingly:

foreach ($FactsFile in $FactsFiles) {
$out = & $($FactsFile.FullName)
Set-Attr $result.ansible_facts “ansible_$(($FactsFile.Name).Split(‘.’)[0])” $out

[ at https://github.com/ansible/ansible-modules-core/blob/devel/windows/setup.ps1#L39 ]

But when ‘ConvertTo-Json’ structures it as a single JSON-formatted string, it shrugs and goes ‘ok I got a single string back, I’ll just display that’.

Side note: there is a default fact ansible_powershell_version variable, which just gives the major number back. I wonder if it’d be a complex change to make that return a dictionary instead, like ansible_python.version does?

Best,
Nikki

Nice find,
If there’s a need for the entire psversion object we can definetely add that. Now with all the various psv5 version numbers out there that might actually be a very good idea.

Well, we know from this thread that at least one person cares enough about the full version string to grab it as a custom fact :slight_smile:

I’ve never needed Ansible to be aware of the full version so far, but it does seem like it could be useful. There’s a couple of new cmdlets in 5.1, for example, so having Ansible aware of which revision of a version a system’s running could end up being useful for writing conditionals.

best,

Nikki