Reputation: 163
I have the following playbook which is essentially working -
vars:
ansible_network_os: ios
IOSserials: []
tasks:
- name: Get all facts from ios devices
register: all_facts
ios_facts:
gather_subset: hardware
- name: Create list Serials
set_fact:
IOSserials: "{{IOSserials|default([]) + [{ 'name': all_facts.ansible_facts.ansible_net_hostname, 'IOS_serial': all_facts.ansible_facts.ansible_net_serialnum }] }}"
when: hostvars[inventory_hostname].serial != all_facts.ansible_facts.ansible_net_serialnum
- name: Display list
debug:
msg: "These switches have a difference in serial number {{ ansible_play_hosts_all|map('extract', hostvars, 'IOSserials')|list }}"
run_once: true
With the following result (I have one 'not equal' scenario in the switches):
TASK [Create list Serials] *****************************************************
skipping: [lab3650s1] => {"changed": false, "skip_reason": "Conditional result was False"}
skipping: [lab4500s1] => {"changed": false, "skip_reason": "Conditional result was False"}
ok: [lab3650s2] =>
{"ansible_facts": {"IOSserials": [{"IOS_serial": "FDO201XXXXD", "name": "lab3650s2"}]}, "changed": false}
TASK [Display list] ************************************************************
ok: [lab3650s1] => {
"msg": "These switches have a difference in serial number [Undefined, [{'name': 'lab3650s2', 'IOS_serial': 'FDO201XXXXD'}], Undefined]"
}
PLAY RECAP *********************************************************************
lab3650s1 : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
lab3650s2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
lab4500s1 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
I do not want the 'undefined' entries in the output list, I'd also like to understand why Ansible is inserting this in the list when its is skipping the set_fact for these entries.
Upvotes: 0
Views: 84
Reputation: 33221
That map
pipeline is missing the select
that would filter out the ones where extract
did not produce a meaningful value; you can see it trivially reproducible:
- set_fact:
thingy:
one:
apple: is red
two:
banana: is yellow
three:
apple: is green
- debug:
msg: >
{{ ["one", "two", "three"] | map("extract", thingy, "banana") | list }}
- debug:
msg: >
{{ ["one", "two", "three"]
| map("extract", thingy, "banana")
| select
| list }}
Upvotes: 1