# Setup custom facts under Windows Help?

**URL:** <https://forum.ansible.com/t/setup-custom-facts-under-windows-help/22217>\
**Category:** Ansible Developer\
**Tags:** windows\
**Created:** [August 5, 2016, 4:59am UTC](https://forum.ansible.com/t/setup-custom-facts-under-windows-help/22217 "2016-08-05T04:59:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bob\_Tanner](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Bob\_Tanner](https://forum.ansible.com/u/Bob_Tanner)\
**Post date:** [August 5, 2016, 4:59am UTC](https://forum.ansible.com/t/setup-custom-facts-under-windows-help/22217/1 "2016-08-05T04:59:20Z")

</div>

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](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](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.

---

<div class="post-metadata">

**Author:** ![nikobelia](https://avatars.discourse-cdn.com/v4/letter/n/e9a140/32.png) [@nikobelia](https://forum.ansible.com/u/nikobelia)\
**Post date:** [August 8, 2016, 1:49pm UTC](https://forum.ansible.com/t/setup-custom-facts-under-windows-help/22217/2 "2016-08-08T13:49:37Z")

</div>

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:

```auto

"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](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

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [August 8, 2016, 2:46pm UTC](https://forum.ansible.com/t/setup-custom-facts-under-windows-help/22217/3 "2016-08-08T14:46:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nikobelia](https://avatars.discourse-cdn.com/v4/letter/n/e9a140/32.png) [@nikobelia](https://forum.ansible.com/u/nikobelia)\
**Post date:** [August 9, 2016, 2:03pm UTC](https://forum.ansible.com/t/setup-custom-facts-under-windows-help/22217/4 "2016-08-09T14:03:45Z")

</div>

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 🙂

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
