| 1 |
from __future__ import (absolute_import, division, print_function) |
| 2 |
__metaclass__ = type |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
DOCUMENTATION = r''' |
| 8 |
name: nios_inventory |
| 9 |
author: |
| 10 |
- Will Tome (@willtome) |
| 11 |
short_description: Infoblox inventory plugin |
| 12 |
version_added: "1.0.0" |
| 13 |
description: |
| 14 |
- This plugin allows you to query the Infoblox Grid for host records and |
| 15 |
use the response data to populate the inventory file. |
| 16 |
options: |
| 17 |
host: |
| 18 |
description: |
| 19 |
- Specifies the DNS host name or address for connecting to the remote |
| 20 |
instance of NIOS WAPI over REST. |
| 21 |
- Value can also be specified using C(INFOBLOX_HOST) environment |
| 22 |
variable. |
| 23 |
type: string |
| 24 |
required: True |
| 25 |
env: |
| 26 |
- name: INFOBLOX_HOST |
| 27 |
username: |
| 28 |
description: |
| 29 |
- Configures the username to use to authenticate the connection to |
| 30 |
the remote instance of NIOS. |
| 31 |
- Value can also be specified using C(INFOBLOX_USERNAME) environment |
| 32 |
variable. |
| 33 |
type: string |
| 34 |
required: True |
| 35 |
env: |
| 36 |
- name: INFOBLOX_USERNAME |
| 37 |
password: |
| 38 |
description: |
| 39 |
- Specifies the password to use to authenticate the connection to |
| 40 |
the remote instance of NIOS. |
| 41 |
- Value can also be specified using C(INFOBLOX_PASSWORD) environment |
| 42 |
variable. |
| 43 |
type: string |
| 44 |
env: |
| 45 |
- name: INFOBLOX_PASSWORD |
| 46 |
extattrs: |
| 47 |
description: |
| 48 |
- Allows you to filter the returned host record based on the |
| 49 |
extensible attributes assigned to them. |
| 50 |
default: {} |
| 51 |
type: dict |
| 52 |
hostfilter: |
| 53 |
description: |
| 54 |
- Accepts a key/value pair and uses it to filter the |
| 55 |
host records to be returned. |
| 56 |
default: {} |
| 57 |
type: dict |
| 58 |
requirements: |
| 59 |
- python >= 3.4 |
| 60 |
- infoblox-client |
| 61 |
''' |
| 62 |
|
| 63 |
EXAMPLES = r''' |
| 64 |
plugin: infoblox.nios_modules.nios_inventory |
| 65 |
host: blox.example.com |
| 66 |
username: admin |
| 67 |
''' |
| 68 |
|
| 69 |
|
| 70 |
from ansible.plugins.inventory import BaseInventoryPlugin |
| 71 |
from ..module_utils.api import WapiInventory |
| 72 |
from ..module_utils.api import normalize_extattrs, flatten_extattrs |
| 73 |
from ansible.errors import AnsibleError |
| 74 |
|
| 75 |
|
| 76 |
class InventoryModule(BaseInventoryPlugin): |
| 77 |
NAME = 'nios_inventory' |
| 78 |
|
| 79 |
def parse(self, inventory, loader, path, cache=True): |
| 80 |
super(InventoryModule, self).parse(inventory, loader, path) |
| 81 |
self._read_config_data(path) |
| 82 |
|
| 83 |
provider = {'host': self.get_option('host'), |
| 84 |
'username': self.get_option('username'), |
| 85 |
'password': self.get_option('password')} |
| 86 |
|
| 87 |
wapi = WapiInventory(provider) |
| 88 |
|
| 89 |
host_filter = self.get_option('hostfilter') |
| 90 |
extattrs = normalize_extattrs(self.get_option('extattrs')) |
| 91 |
return_fields = ['name', 'view', 'extattrs', 'ipv4addrs'] |
| 92 |
|
| 93 |
hosts = wapi.get_object('record:host', host_filter, extattrs=extattrs, return_fields=return_fields) or [] |
| 94 |
|
| 95 |
if not hosts: |
| 96 |
raise AnsibleError("host record is not present") |
| 97 |
|
| 98 |
for host in hosts: |
| 99 |
group_name = self.inventory.add_group(host['view']) |
| 100 |
host_name = self.inventory.add_host(host['name']) |
| 101 |
self.inventory.add_child(group_name, host_name) |
| 102 |
|
| 103 |
self.inventory.set_variable(host_name, 'view', host['view']) |
| 104 |
self.inventory.set_variable(host_name, 'ipv4addrs', [item['ipv4addr'] for item in host['ipv4addrs']]) |
| 105 |
|
| 106 |
for key, value in flatten_extattrs(host['extattrs']).items(): |
| 107 |
self.inventory.set_variable(host_name, key, value) |