I created a Powershell script in order to produce custom Ansible facts on target Windows machine listing installed software. For now I can't get my software list properly parsed within my playbook.
I can see the raw content of custom facts, but it seems like JSON is not properly parsed because I can't get a proper list object to be processed by a loop statement. A fatal error occurs:
The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'name'\n\n
All custom facts (meaning Windows software list generated by Powershell custom fact) are stored automatically by Ansible within an autogenerated variable/attribute of ansible_facts called 'ansible_softwarelist' ('ansible_' suffix + name of my fact file without extension).
Custom fact (Powershell)
$software = get-wmiobject -class Win32_Product | select-object name,version,vendor
$software_count = ($software | measure).count
$software_list | % { `
$i++
if ($i -lt ($software_count-1))
{
$separator = ","
}
else
{
$separator = ""
}
write-host "{`"name`":`"$($_.name)`",`"version`":`"$($_.version)`",`"vendor`":`"$($_.vendor)`"}$separator"
}
Raw Powershell output
{"name":"Software 1","version":"14.0.7015.1000","vendor":"Vendor 1"},
{"name":"Software 2","version":"14.1.1000","vendor":"Vendor 1"},
{"name":"Software 3","version":"1.5.2","vendor":"Vendor 1"}
Ansible playbook
tasks:
- name: "Deploy Powershell script (custom Windows facts)"
win_copy:
src: "/etc/ansible/files/facts/softwarelist.ps1"
dest: "C:\\remotedir\\softwarelist.ps1"
- name: "Gather custom facts"
setup:
fact_path: "C:\\remotedir"
- name: "View software list in Ansible by name"
debug:
msg: "{{ item.name }}"
loop: "{{ ansible_softwarelist }}"