How to separate the output of register into dict - ansible

I have the following output:
TASK [debug] ***********************
ok: [localhost] => {
"output": {
"changed": true,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": true,
"failed": false,
"item": {
"NAME": "server1",
"SEC_SYS": "CONT2"
},
"rc": 0,
"stderr": "",
"stderr_lines": [],
"stdout": "vm1,Not Activated\nvm2,Not Activated\nvm3,Running\nvm4,Running\nvm5,Not Activated\nvm6,Running\n",
"stdout_lines": [
"vm1,Not Activated",
"vm2,Not Activated",
"vm3,Running",
"vm4,Running",
"vm5,Not Activated",
"vm6,Running"
]
},
{
"ansible_loop_var": "item",
"changed": true,
"failed": false,
"item": {
"NAME": "server2",
"SEC_SYS": "CONT1"
},
"rc": 0,
"stderr": "",
"stderr_lines": [],
"stdout": "vm1,Running\nvm2,Running\nvm3,Not Activated\nvm4,Not Activated\nvm5,Running\nvm6,Not Activated\n",
"stdout_lines": [
"vm1,Running",
"vm2,Running",
"vm3,Not Activated",
"vm4,Not Activated",
"vm5,Running",
"vm6,Not Activated"
]
},
{
"ansible_loop_var": "item",
"changed": true,
"failed": false,
"item": {
"NAME": "server3",
"SEC_SYS": "CONT2"
},
"rc": 0,
"stderr": "",
"stderr_lines": [],
"stdout": "vm1,Not Activated\nvm2,Not Activated\nvm3,Running\nvm4,Running\nvm5,Not Activated\nvm6,Running\n",
"stdout_lines": [
"vm1,Not Activated",
"vm2,Not Activated",
"vm3,Running",
"vm4,Running",
"vm5,Not Activated",
"vm6,Running"
]
}
]
}
}
I want to turn into the following list:
vm_off:
- SEC_SYS: CONT2
VM_NA:
- vm1
- vm2
- vm5
- SEC_SYS: CONT1
VM_NA:
- vm3
- vm4
- vm6
Then sum the proc of the Not Activated VM, comparing from the list:
vmlist:
- name: vm1
proc: 0.5
- name: vm2
proc: 0.7
- name: vm3
proc: 1.0
- name: vm4
proc: 0.5
- name: vm5
proc: 0.5
- name: vm6
proc: 0.5
The expected output will be:
proc_steal:
- server: CONT1
proc: 1.7
- server: CONT2
proc: 2.0
I cant separate the stdout_lines into a dict because it appear an error:
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'results'\n\nThe error appears to be in '/etc/ansible/val.yml': line 17, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - set_fact:\n ^ here\n"}
My .yml:
- hosts: localhost
gather_facts: no
vars:
vmservers:
- NAME: server1
SEC_SYS: CONT2
- NAME: server2
SEC_SYS: CONT1
- NAME: server3
SEC_SYS: CONT2
tasks:
- name: get not activated vm
ansible.builtin.script: "vm.sh {{ item.SEC_SYS }}"
register: output
loop: "{{ vmservers }}"
- set_fact:
vm_all: "{{ vm_all | default([]) + [item.results.stdout_lines.split(',')] }}"
with_items: "{{ output }}"
- debug:
var: output
- debug:
var: vm_all
the vm.sh just take a list of vm and its state:
#!/bin/bash
if [ "$1" = "CONT1" ]; then
case $1 in
"CONT1") cat cont1.txt;;
esac
else
case $1 in
"CONT2") cat cont2.txt;;
esac
fi
root:ansible# cat cont1.txt
vm1,Running
vm2,Running
vm3,Not Activated
vm4,Not Activated
vm5,Running
vm6,Not Activated

I create the dict vmoff2 using the following:
- name: create Not Activated VM list
set_fact:
vmoff: >-
{{ vmoff | default([])
+ [{
'sec_sys': item.item.SEC_SYS,
'vm_na': dict(item.stdout_lines
| from_yaml
| select()
| map('split', ',')
| list)
}]
}}
loop: "{{ output.results }}"
loop_control:
label: "{{ item.item.SEC_SYS }}"
And eliminate the duplicate output with:
- name: eliminate duplicate
set_fact:
vmoff2: "{{ vmoff2 | default([]) + [{'sec_sys': item.1.0.sec_sys, 'vm_na': item.1.0.vm_na }] }}"
loop: "{{ vmoff|groupby('sec_sys') }}"
Finally i have the following dict:
"vmoff2": [
{
"sec_sys": "CONT1",
"vm_na": {
"vm1": "Running",
"vm2": "Running",
"vm3": "Not Activated",
"vm4": "Not Activated",
"vm5": "Running",
"vm6": "Not Activated"
}
},
{
"sec_sys": "CONT2",
"vm_na": {
"vm1": "Not Activated",
"vm2": "Not Activated",
"vm3": "Running",
"vm4": "Running",
"vm5": "Not Activated",
"vm6": "Running"
}
}
]
---------------
"vmprof2": [
{
"proc": {
"vm1": "0.5",
"vm2": "0.7",
"vm3": "1.0",
"vm4": "0.5",
"vm5": "0.5",
"vm6": "0.5"
},
"sec_sys": "CONT1"
},
{
"proc": {
"vm1": "1.0",
"vm2": "0.7",
"vm3": "1.0",
"vm4": "0.7",
"vm5": "0.5",
"vm6": "0.7"
},
"sec_sys": "CONT2"
}
]
I want to sum the proc of the Not activated VM, and have the following output:
proc_steal:
- server: CONT1
proc: 1.7
- server: CONT2
proc: 2.4
I tried the following:
- name: create proc_steal list
set_fact:
proc_steal: >-
{{ proc_steal | default([])
+ [{
'sec_sys': item.sec_sys,
'proc': vmprof2
| selectattr('sec_sys', '==', item.sec_sys)
| selectattr('proc', '==', item.vm_na)
| sum(attribute='value')
| float
}]
}}
loop: "{{ vmoff2 }}"
loop_control:
label: "{{ item.sec_sys }}"
but i get:
"proc_steal": [
{
"proc": 0.0,
"sec_sys": "CONT1"
},
{
"proc": 0.0,
"sec_sys": "CONT2"
}
]
}
Is not making the sum, maybe because is not float, but when i tried to put float i have an error, any guess?

Related

Ansible_devices filter dicts for "holders" and "size" value

I'm trying to get the "holders" and "size" value of each dictionary. I managed this far but whatever i try after this does not work.
I'm interested in learning which kind of data structure this is and how to filter it.
Going through the Ansible docs gives me no real pointers, neither did posts on this site or google.
I'm first just trying to get at "holders" after i'll try to add "size" also.
Holders is a list in a dictionary, what am i missing here?
- name: "test ansible_devices"
hosts: localhost
tasks:
- debug:
var: "{{ ansible_devices|dict2items|selectattr('key','match','^sd.*')|map(attribute='value.partitions') }}"
output:
ok: [localhost] => {
"msg": [
{
"sdd1": {
"holders": [],
"links": {
"ids": [
"lvm-pv-uuid-xxx"
],
"labels": [],
"masters": [],
"uuids": []
},
"sectors": "4192256",
"sectorsize": 512,
"size": "2.00 GB",
"start": "2048",
"uuid": null
}
},
{
"sdb1": {
"holders": [
"vg_app-lv_app"
],
"links": {
"ids": [
"lvm-pv-uuid-xxx"
],
"labels": [],
"masters": [
"dm-2"
],
"uuids": []
},
"sectors": "20969472",
"sectorsize": 512,
"size": "10.00 GB",
"start": "2048",
"uuid": null
}
},
{
"sdc1": {
"holders": [
"vg_system-lv_system_home"
],
"links": {
"ids": [
"lvm-pv-uuid-xxx"
],
"labels": [],
"masters": [
"dm-3"
],
"uuids": []
},
"sectors": "2095071",
"sectorsize": 512,
"size": "1022.98 MB",
"start": "2048",
"uuid": null
}
},
{
"sda1": {
"holders": [],
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": [
"xxx"
]
},
"sectors": "2097152",
"sectorsize": 512,
"size": "1.00 GB",
"start": "2048",
"uuid": "xxx"
},
"sda2": {
"holders": [
"vg_system-lv_system_swap",
"vg_system-lv_system_log",
"vg_system-lv_system_root",
"vg_system-lv_system_home"
],
"links": {
"ids": [
"lvm-pv-uuid-xxx"
],
"labels": [],
"masters": [
"dm-0",
"dm-1",
"dm-3",
"dm-4"
],
"uuids": []
},
"sectors": "60815360",
"sectorsize": 512,
"size": "29.00 GB",
"start": "2099200",
"uuid": null
}
}
]
}
desired output:
"sdd1": {
"holders": [],
"size": "2.00 GB",
"sdb1": {
"holders": [
"vg_app-lv_app"
"size": "30.00 GB",
"sdc1": {
"holders": [
"vg_system-lv_system_home"
"size": "10.00 GB",
"sda1": {
"holders": [],
"size": "2.00 GB",
"sda2": {
"holders": [
"vg_system-lv_system_swap",
"vg_system-lv_system_log",
"vg_system-lv_system_root",
"vg_system-lv_system_home"
"size": "29.00 GB",
I tried:
- debug:
msg: "{{ansible_devices|dict2items|selectattr('key','match','^sd.*')|map(attribute='value.partitions')|map(attribute='holders') }}"
error:
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'holders'"}
I tried:
- debug:
msg: "{{ansible_devices|dict2items|selectattr('key','match','^sd.*')|map(attribute='value.partitions')|flatten|map(attribute='holders') }}"
error:
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'holders'"}
I tried:
- name: "test ansible_devices"
hosts: localhost
tasks:
- debug:
var: "{{ ansible_devices |dict2items|selectattr('key','match','^sd.*')|map(attribute='value.partitions')|selectattr('holders','match','vg_system-lv_system_log')}}"
error:
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'holders'\n\n"}
I tried:
- name: "test ansible_devices"
hosts: localhost
tasks:
- debug:
var: "{{ ansible_devices|dict2items|selectattr('key','match','^sd.*')|map(attribute='value.partitions')|json_query('holders') }}"
error:
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'holders'"}
I tried, just to see if i could get at any value with json_query:
- name: "test ansible_devices"
hosts: localhost
vars:
- query: "dm-1"
tasks:
- debug:
var: "{{ ansible_devices |json_query('query')}}"
error:
fatal: [localhost]: FAILED! => {"msg": "template error while templating string: Expected an expression, got 'end of print statement'. String: {{}}"}
I tried it also with a loop but that was a hopeless try with no results.
Given the partitions
partitions: "{{ ansible_devices|
dict2items|
selectattr('key','match','^sd.*')|
map(attribute='value.partitions') }}"
Get the keys, and values and create the dictionary
_keys: "{{ partitions|
json_query('[].keys(#)')|flatten }}"
_vals: "{{ partitions|
json_query('[].*.{holders: holders, size: size}')|flatten }}"
_dict: "{{ dict(_keys|zip(_vals)) }}"
Example of a complete playbook for testing
- hosts: localhost
vars:
_ansible_devices:
sda:
partitions:
sda1:
holders: []
sectors: 2097152
size: 1.00 GB
start: 2048
sda2:
holders: []
sectors: 60815360
size: 29.00 GB
start: 2099200
partitions: "{{ _ansible_devices|
dict2items|
selectattr('key','match','^sd.*')|
map(attribute='value.partitions') }}"
_keys: "{{ partitions|
json_query('[].keys(#)')|flatten }}"
_vals: "{{ partitions|
json_query('[].*.{holders: holders, size: size}')|flatten }}"
_dict: "{{ dict(_keys|zip(_vals)) }}"
tasks:
- debug:
var: partitions
- debug:
var: _keys
- debug:
var: _vals
- debug:
var: _dict

I want to sum value from dict

I have the following output:
"vmoff2": [
{
"sec_sys": "CONT1",
"vm_na": {
"vm1": "Running",
"vm2": "Running",
"vm3": "Not Activated",
"vm4": "Not Activated",
"vm5": "Running",
"vm6": "Not Activated"
}
},
{
"sec_sys": "CONT2",
"vm_na": {
"vm1": "Not Activated",
"vm2": "Not Activated",
"vm3": "Running",
"vm4": "Running",
"vm5": "Not Activated",
"vm6": "Running"
}
}
]
---------------
"vmprof2": [
{
"proc": {
"vm1": "0.5",
"vm2": "0.7",
"vm3": "1.0",
"vm4": "0.5",
"vm5": "0.5",
"vm6": "0.5"
},
"sec_sys": "CONT1"
},
{
"proc": {
"vm1": "1.0",
"vm2": "0.7",
"vm3": "1.0",
"vm4": "0.7",
"vm5": "0.5",
"vm6": "0.7"
},
"sec_sys": "CONT2"
}
]
And want to sum the proc of the not activated VM, comparing with wmoff2, the out expected will be:
proc_steal:
- server: CONT1
proc: 1.7
- server: CONT2
proc: 2.4
I tried the following:
- name: create proc_steal list
set_fact:
proc_steal: >-
{{ proc_steal | default([])
+ [{
'sec_sys': item.sec_sys,
'proc': vmprof2
| selectattr('sec_sys', '==', item.sec_sys)
| selectattr('proc', '==', item.vm_na)
| sum(attribute='value')
| float
}]
}}
loop: "{{ vmoff2 }}"
loop_control:
label: "{{ item.sec_sys }}"
but i get:
"proc_steal": [
{
"proc": 0.0,
"sec_sys": "CONT1"
},
{
"proc": 0.0,
"sec_sys": "CONT2"
}
]
}
Is not making the sum, maybe because is not float, but when i tried to put float i got an error "float object is not iterable", so how can i sum that values?
For example, to sum all VM
- set_fact:
proc_steal: "{{ dict(_serv|zip(_proc)) }}"
vars:
_serv: "{{ vmprof2|json_query('[].sec_sys') }}"
_proc: "{{ vmprof2|json_query('[].proc.*')|
map('map', 'float')|
map('sum')|
map('round', 2)|list }}"
gives the dictionary
proc_steal:
CONT1: 3.7
CONT2: 4.6
You can convert it to a list if you want to
- set_fact:
proc_steal: "{{ dict(_serv|zip(_proc))|
dict2items(key_name='server', value_name='proc') }}"
vars:
_serv: "{{ vmprof2|json_query('[].sec_sys') }}"
_proc: "{{ vmprof2|json_query('[].proc.*')|
map('map', 'float')|
map('sum')|
map('round', 2)|list }}"
gives
proc_steal:
- proc: 3.7
server: CONT1
- proc: 4.6
server: CONT2
To sum the Not Activated VM only, create a dictionary first
- set_fact:
vm_na: "{{ dict(_serv|zip(_vmna)) }}"
vars:
_serv: "{{ vmoff2|json_query('[].sec_sys') }}"
_vmna: "{{ vmoff2|json_query('[].vm_na')|
map('dict2items')|
map('selectattr', 'value', 'eq', 'Not Activated')|
map('map', attribute='key') }}"
gives
vm_na:
CONT1:
- vm3
- vm4
- vm6
CONT2:
- vm1
- vm2
- vm5
Then use it to select the machines
- set_fact:
proc_steal: "{{ proc_steal|d({})|combine({item.sec_sys: _proc|float}) }}"
loop: "{{ vmprof2 }}"
vars:
_proc: "{{ vm_na[item.sec_sys]|map('extract', item.proc)|
map('float')|
sum|
round(2) }}"
gives the dictionary
proc_steal:
CONT1: 2.0
CONT2: 2.2
, or
- set_fact:
proc_steal: "{{ proc_steal|d([]) + [{'server': item.sec_sys,
'proc': _proc|float}] }}"
loop: "{{ vmprof2 }}"
vars:
_proc: "{{ vm_na[item.sec_sys]|map('extract', item.proc)|
map('float')|
sum|
round(2) }}"
gives the list if you want to
proc_steal:
- proc: 2.0
server: CONT1
- proc: 2.2
server: CONT2

Ansible get unique value in loop for register

Hi friend below is my sample output from a regsiter name dataInfo
ok: [123.23.44.123] => {
"msg": [
{
"changed": true,
"item": [
{
"artifactName": "helloWorld.jar",
"status": "false"
},
"myGroup1"
],
"rc": 0,
"stderr_lines": [],
"stdout": "ok",
"stdout_lines": [
"ok"
]
},
{
"changed": true,
"item": [
{
"artifactName": "helloWorld.jar",
"status": "false"
},
"myGroup2"
],
"rc": 0,
"stderr_lines": [],
"stdout": "ok",
"stdout_lines": [
"ok"
]
}
Below is my code
- name: My ArtifactName Name
debug:
msg: "artifactName = {{ item.item[0].artifactName }}"
loop: "{{ dataInfo.results }}"
when: item.changed | bool == true and item.stdout == "ok"
How can i only display as below
artifactName = helloWorld.jar
rather than
artifactName = helloWorld.jar
artifactName = helloWorld.jar
I have shared my code as shown above. Please advice
As you are looping with dataInfo.results, the debug message will repeat for each "item". If you only want unique artifactName, then you can save it to a variable using json_query and choose to display only unique ones.
Something like this:
# Save list of artifactName found in dataInfo.results even with duplicates
- name: save artifact names
set_fact:
artifact_names: "{{ artifact_names | default([]) + [ item | json_query('item[].artifactName') ] }}"
loop: "{{ dataInfo.results }}"
# Show each item of artifact_names using unique filter to eliminate duplicates
- name: show unique artifact names
debug:
var: item
loop: "{{ artifact_names | flatten | unique }}"
Update:
Below set_fact:
artifact_names: "{{ artifact_names | default([]) + [ item.item[0].artifactName ] }}"
... will work if you get only 1 hash in item like this:
"item": [
{
"artifactName": "helloWorld.jar",
"status": "false"
},
],
But if it may have multiple hashes like below, it won't work:
"item": [
{
"artifactName": "helloWorld.jar",
"status": "false"
},
{
"artifactName": "someother.jar",
"status": "false"
},
...
],
If this is a possibility, then json_query should be used. It can be used exactly like the jq command.

Correct way to join a new Windows guest to a domain when deployed form a template

I am attempting to automate the deployment of a Windows guest in a VMware environment and I have been quite able to do so as long as I am happy with having to manually add it to the domain, which I am not. I have tried using the vmware_client customization and it does not appear to even be applied.
Ansible 2.9
Red Hat 8
YML
---
- hosts: all
gather_facts: false
vars_files:
- group_vars/all
- build_info/vars
tasks:
- debug:
var: "{{ dusername }}"
- name: Clone a virtual machine from Windows template and customize
vmware_guest:
annotation: "This machine was built from a template through a process triggered by an ansible playbook"
hostname: "{{ hostname }}"
username: "{{ username }}"
password: "{{ password }}"
validate_certs: "{{ validate_certs }}"
datacenter: "{{ datacenter }}"
cluster: "{{ cluster }}"
folder: "{{ folder }}"
name: "{{ bname }}"
template: "{{ template }}"
datastore: "{{ datastore}}"
state: poweredon
networks:
- name: "{{ bnetworks.netname }}"
ip: "{{ bnetworks.ip }}"
netmask: "{{ bnetworks.netmask }}"
gateway: "{{ bnetworks.gateway }}"
domain: "{{ bnetworks.domain }}"
start_connected: yes
dns_servers: "{{bnetworks.dns_servers}}"
dns_suffix: "{{bnetworks.dns_suffix}}"
customization:
hostname: "{{ bname }}"
domainadmin: "{{ dusername }}"
domainadminpassword: "{{ dpassword }}"
joindomain: "{{ bnetworks.domain }}"
fullname: "{{ ladminname }}"
wait_for_ip_address: yes
wait_for_customization: yes
with_dict: "{{ bnetworks }}"
delegate_to: localhost'
The result
{
"changed": true,
"instance": {
"module_hw": true,
"hw_name": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"hw_power_status": "poweredOn",
"hw_guest_full_name": "Microsoft Windows Server 2016 or later (64-bit)",
"hw_guest_id": "windows9Server64Guest",
"hw_product_uuid": "4227f81c-1b25-13fd-45f2-d9399408a5f6",
"hw_processor_count": 2,
"hw_cores_per_socket": 1,
"hw_memtotal_mb": 8192,
"hw_interfaces": [
"eth0"
],
"hw_datastores": [
"na2-tntr02-dat"
],
"hw_files": [
"[na2-tntr02-dat] ********/********.vmx",
"[na2-tntr02-dat] ********/********.nvram",
"[na2-tntr02-dat] ********/********.vmsd",
"[na2-tntr02-dat] ********/********.vmxf",
"[na2-tntr02-dat] ********/********.vmdk"
],
"hw_esxi_host": "na2-devesx01.********",
"hw_guest_ha_state": true,
"hw_is_template": false,
"hw_folder": "/NA2/vm/GLOBAL OPERATIONS/Storage",
"hw_version": "vmx-13",
"instance_uuid": "5027d0ef-7a89-73a6-1da9-6e15df083592",
"guest_tools_status": "guestToolsRunning",
"guest_tools_version": "11269",
"guest_question": null,
"guest_consolidation_needed": false,
"ipv4": "10.6.6.10",
"ipv6": null,
"annotation": "This machine was built from a template through a process triggered by an ansible playbook",
"customvalues": {},
"snapshots": [],
"current_snapshot": null,
"vnc": {},
"moid": "vm-21984",
"vimref": "vim.VirtualMachine:vm-21984",
"hw_cluster": "NA2-NonPROD",
"hw_eth0": {
"addresstype": "assigned",
"label": "Network adapter 1",
"macaddress": "00:50:56:a7:fc:87",
"ipaddresses": [
"fe80::4835:d689:f1a7:6dd4",
"10.6.6.10"
],
"macaddress_dash": "00-50-56-a7-fc-87",
"summary": "DVSwitch: 50 27 7a 36 e1 9e ae 1a-29 5a 4a 79 8d 2b 6f a3",
"portgroup_portkey": "191",
"portgroup_key": "dvportgroup-71"
}
},
"invocation": {
"module_args": {
"annotation": "This machine was built from a template through a process triggered by an ansible playbook",
"hostname": "na2-pdvcva01.********",
"username": "RP4VM#vsphere.local",
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"validate_certs": false,
"datacenter": "NA2",
"cluster": "NA2-NonPROD",
"folder": "/GLOBAL OPERATIONS/Storage",
"name": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"template": "NA2_ZWindows2016STD",
"datastore": "na2-tntr02-dat",
"state": "poweredon",
"networks": [
{
"name": "FIRM_NA|PROD_ap|STORAGE_epg",
"ip": "10.6.6.10",
"netmask": "255.255.255.0",
"gateway": "10.6.6.1",
"domain": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"start_connected": true,
"dns_servers": [
"10.6.2.16",
"10.2.2.17"
],
"dns_suffix": [
"VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"asia.global-legal.com",
"emea.global-legal.com"
],
"type": "static"
}
],
"customization": {
"hostname": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"domainadmin": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"domainadminpassword": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"joindomain": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"fullname": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
},
"wait_for_ip_address": true,
"wait_for_customization": true,
"port": 443,
"is_template": false,
"customvalues": [],
"name_match": "first",
"use_instance_uuid": false,
"disk": [],
"cdrom": [],
"hardware": {},
"force": false,
"state_change_timeout": 0,
"linked_clone": false,
"vapp_properties": [],
"proxy_host": null,
"proxy_port": null,
"uuid": null,
"guest_id": null,
"esxi_hostname": null,
"snapshot_src": null,
"resource_pool": null,
"customization_spec": null,
"convert": null
}
},
"_ansible_no_log": false,
"item": {
"key": "netname",
"value": "FIRM_NA|PROD_ap|STORAGE_epg"
},
"ansible_loop_var": "item",
"_ansible_item_label": {
"key": "netname",
"value": "FIRM_NA|PROD_ap|STORAGE_epg"
},
"_ansible_delegated_vars": {}
}
However the machine does not join the domain. I fully expect a fail because the domain credentials supplied do not have the authority to add the machine to the domain.

Ansible getent module's loop output results parsing

I am trying to check if a number of users are present or not in the managed node using getent module and create a list of users who are not present.
The piece of code is:
- getent:
database: passwd
key: "{{ item }}"
fail_key: no
register: x
loop:
- "user1"
- "user2"
- debug: var=x.results
- set_fact:
fail_list: "{{ x.results | }}"
I am stuck at this point.
Is there any way I can save the users who are not present to the variable fail_list as a list?
In the above example, user1 is not present and user2 is present in the managed node.
The ansible version I am using is 2.9 and the debug output is a list of dicts as below:
"x.results": [
{
"ansible_facts": {
"getent_passwd": {
"user1": null
}
},
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"invocation": {
"module_args": {
"database": "passwd",
"fail_key": false,
"key": "user1",
"service": null,
"split": null
}
},
"item": "user1",
"msg": "One or more supplied key could not be found in the database."
},
{
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python",
"getent_passwd": {
"user2": [
"x",
"0",
"0",
"user2",
"/home/user2",
"/bin/bash"
]
}
},
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"invocation": {
"module_args": {
"database": "passwd",
"fail_key": false,
"key": "user2",
"service": null,
"split": null
}
},
"item": "user2"
},
Run getent once and search the list of users. For example
- hosts: localhost
tasks:
- getent:
database: passwd
- debug:
msg: User {{ item }} exists.
loop:
- root
- user1
- user2
when: item in my_users
vars:
my_users: "{{ getent_passwd.keys()|list }}"
gives
ok: [localhost] => (item=root) => {
"msg": "User root exists."
}
skipping: [localhost] => (item=user1)
skipping: [localhost] => (item=user2)

Resources