- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
Ansible lookup dhcp options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2021 10:38 AM
Hello everyone,
i try to lookup the 'domain_name_servers' dhcp option of a given network. is it possible to query or filter the lookup to return only the options with name equals 'domain_name_servers'?
- name: "lookup dhcp_options" set_fact: dhcp_options: "{{ lookup('nios', 'network', filter={'network': item.1.network}, return_fields=['options'], provider=nios_provider) }}" with_subelements: - "{{ svms }}" - lifs
is there a general document what statements a nios lookup can take? or is it just a wrapped api call and there is a api reference?
Thanks!
Solved! Go to Solution.
Re: Ansible lookup dhcp options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2021 01:51 AM
Hi,
The lookup does act like a wrapper to query underlying API objects. You can find documentation about API objects as part of your grid at - https://<grid-master>/wapidoc.
This link here will take you directly to the network object - https://<grid-master>/wapidoc/objects/network.html#options
Per this document, it looks like the options parameter is not searchable, and when you ask for the field options to be returned, it will return the whole structure.
You would need to do some screening and print only the values you need using Ansible.
Regards,
Krishna Vasudevan
Re: Ansible lookup dhcp options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2021 04:10 AM
Yepp i came up with this "pretty" looking task. I had to use selectattr('name', '==', 'domain-name-servers') on the lookup results. Maybe there is a more elegant and better readable solution.
vars:
broadcast_domains: {
DomainA: {
subnet: '172.31.0.0/24'
},
DomainB: {
subnet: '10.200.252.0/23'
}
}
tasks:
- name: "combine dhcp_options into broadcast_domains" set_fact: broadcast_domains: "{{ broadcast_domains | combine( { item.key: { 'subnet': item.value.subnet, 'domain_name_servers' : (lookup('nios', 'network', filter={'network': item.value.subnet}, return_fields=['options'], provider=nios_provider).options | selectattr('name', '==', 'domain-name-servers') | first).value | default(), 'routers' : (lookup('nios', 'network', filter={'network': item.value.subnet}, return_fields=['options'], provider=nios_provider).options | selectattr('name', '==', 'routers') | first).value | default() } } ) }}" loop: "{{ broadcast_domains | dict2items }}" when: (item.value.subnet is defined)
- name: " display broadcast_domains"
debug:
msg: "{{ broadcast_domains }}"
the Output
ok: [MyHost] => { "msg": { "DomainA": { "domain_name_servers": "", "routers": "", "subnet": "172.31.0.0/24", }, "DomainB": { "domain_name_servers": "10.200.252.49,10.200.252.50", "routers": "10.200.253.254", "subnet": "10.200.252.0/23", } } }