| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
from __future__ import (absolute_import, division, print_function) |
| 7 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
--- |
| 12 |
name: nios_lookup |
| 13 |
short_description: Query Infoblox NIOS objects |
| 14 |
version_added: "1.0.0" |
| 15 |
description: |
| 16 |
- Uses the Infoblox WAPI API to fetch NIOS specified objects. This lookup |
| 17 |
supports adding additional keywords to filter the return data and specify |
| 18 |
the desired set of returned fields. |
| 19 |
requirements: |
| 20 |
- infoblox-client |
| 21 |
|
| 22 |
options: |
| 23 |
_terms: |
| 24 |
description: |
| 25 |
- The name of the network object to be returned from the Infoblox appliance. |
| 26 |
required: True |
| 27 |
type: str |
| 28 |
return_fields: |
| 29 |
description: The list of field names to return for the specified object. |
| 30 |
type: list |
| 31 |
elements: str |
| 32 |
filter: |
| 33 |
description: A dict object that is used to filter the returned objects. |
| 34 |
type: dict |
| 35 |
extattrs: |
| 36 |
description: A dict object that is used to filter based on extensible attributes. |
| 37 |
type: dict |
| 38 |
''' |
| 39 |
|
| 40 |
EXAMPLES = """ |
| 41 |
- name: fetch all networkview objects |
| 42 |
ansible.builtin.set_fact: |
| 43 |
networkviews: "{{ lookup('infoblox.nios_modules.nios_lookup', 'networkview', provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}" |
| 44 |
|
| 45 |
- name: fetch the default dns view |
| 46 |
ansible.builtin.set_fact: |
| 47 |
dns_views: "{{ lookup('infoblox.nios_modules.nios_lookup', 'view', filter={'name': 'default'}, |
| 48 |
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}" |
| 49 |
|
| 50 |
# all of the examples below use credentials that are set using env variables |
| 51 |
# export INFOBLOX_HOST=nios01 |
| 52 |
# export INFOBLOX_USERNAME=admin |
| 53 |
# export INFOBLOX_PASSWORD=admin |
| 54 |
|
| 55 |
- name: fetch all host records and include extended attributes |
| 56 |
ansible.builtin.set_fact: |
| 57 |
host_records: "{{ lookup('infoblox.nios_modules.nios_lookup', 'record:host', return_fields=['extattrs', 'name', 'view', 'comment']}) }}" |
| 58 |
|
| 59 |
|
| 60 |
- name: use env variables to pass credentials |
| 61 |
ansible.builtin.set_fact: |
| 62 |
networkviews: "{{ lookup('infoblox.nios_modules.nios_lookup', 'networkview') }}" |
| 63 |
|
| 64 |
- name: get a host record |
| 65 |
ansible.builtin.set_fact: |
| 66 |
host: "{{ lookup('infoblox.nios_modules.nios_lookup', 'record:host', filter={'name': 'hostname.ansible.com'}) }}" |
| 67 |
|
| 68 |
- name: get the authoritative zone from a non default dns view |
| 69 |
ansible.builtin.set_fact: |
| 70 |
host: "{{ lookup('infoblox.nios_modules.nios_lookup', 'zone_auth', filter={'fqdn': 'ansible.com', 'view': 'ansible-dns'}) }}" |
| 71 |
""" |
| 72 |
|
| 73 |
RETURN = """ |
| 74 |
obj_type: |
| 75 |
description: |
| 76 |
- The object type specified in the terms argument |
| 77 |
returned: always |
| 78 |
type: list |
| 79 |
contains: |
| 80 |
obj_field: |
| 81 |
description: |
| 82 |
- One or more obj_type fields as specified by return_fields argument or |
| 83 |
the default set of fields as per the object type |
| 84 |
""" |
| 85 |
|
| 86 |
from ansible.plugins.lookup import LookupBase |
| 87 |
from ansible.errors import AnsibleError |
| 88 |
from ..module_utils.api import WapiLookup |
| 89 |
from ..module_utils.api import normalize_extattrs, flatten_extattrs |
| 90 |
|
| 91 |
|
| 92 |
class LookupModule(LookupBase): |
| 93 |
|
| 94 |
def run(self, terms, variables=None, **kwargs): |
| 95 |
try: |
| 96 |
obj_type = terms[0] |
| 97 |
except IndexError: |
| 98 |
raise AnsibleError('the object_type must be specified') |
| 99 |
|
| 100 |
return_fields = kwargs.pop('return_fields', None) |
| 101 |
filter_data = kwargs.pop('filter', {}) |
| 102 |
extattrs = normalize_extattrs(kwargs.pop('extattrs', {})) |
| 103 |
provider = kwargs.pop('provider', {}) |
| 104 |
wapi = WapiLookup(provider) |
| 105 |
res = wapi.get_object(obj_type, filter_data, return_fields=return_fields, extattrs=extattrs) |
| 106 |
if res is not None: |
| 107 |
for obj in res: |
| 108 |
if 'extattrs' in obj: |
| 109 |
obj['extattrs'] = flatten_extattrs(obj['extattrs']) |
| 110 |
else: |
| 111 |
res = [] |
| 112 |
return res |