Skip to content

Commit 38e20fa

Browse files
authored
Inventory: Add ansible_host_dns_name option for ansible_host to use dns_name (#434)
1 parent 66a285b commit 38e20fa

File tree

7 files changed

+73
-7
lines changed

7 files changed

+73
-7
lines changed

plugins/inventory/nb_inventory.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@
178178
- Setting interfaces will also fetch IP addresses and the dns_name host_var will be set.
179179
type: boolean
180180
default: False
181+
ansible_host_dns_name:
182+
description:
183+
- If True, sets DNS Name (fetched from primary_ip) to be used in ansible_host variable, instead of IP Address.
184+
type: boolean
185+
default: False
181186
compose:
182187
description: List of custom ansible host vars to create from the device object fetched from NetBox
183188
default: {}
@@ -427,7 +432,7 @@ def group_extractors(self):
427432
{"interfaces": self.extract_interfaces,}
428433
)
429434

430-
if self.interfaces or self.dns_name:
435+
if self.interfaces or self.dns_name or self.ansible_host_dns_name:
431436
extractors.update(
432437
{"dns_name": self.extract_dns_name,}
433438
)
@@ -1063,7 +1068,7 @@ def lookup_processes_secondary(self):
10631068
lookups = []
10641069

10651070
# IP addresses are needed for either interfaces or dns_name options
1066-
if self.interfaces or self.dns_name:
1071+
if self.interfaces or self.dns_name or self.ansible_host_dns_name:
10671072
lookups.append(self.refresh_ipaddresses)
10681073

10691074
return lookups
@@ -1369,6 +1374,13 @@ def _fill_host_variables(self, host, hostname):
13691374
if extracted_primary_ip:
13701375
self.inventory.set_variable(hostname, "ansible_host", extracted_primary_ip)
13711376

1377+
if self.ansible_host_dns_name:
1378+
extracted_dns_name = self.extract_dns_name(host=host)
1379+
if extracted_dns_name:
1380+
self.inventory.set_variable(
1381+
hostname, "ansible_host", extracted_dns_name
1382+
)
1383+
13721384
extracted_primary_ip4 = self.extract_primary_ip4(host=host)
13731385
if extracted_primary_ip4:
13741386
self.inventory.set_variable(hostname, "primary_ip4", extracted_primary_ip4)
@@ -1508,5 +1520,6 @@ def parse(self, inventory, loader, path, cache=True):
15081520
self.vm_query_filters = self.get_option("vm_query_filters")
15091521
self.virtual_chassis_name = self.get_option("virtual_chassis_name")
15101522
self.dns_name = self.get_option("dns_name")
1523+
self.ansible_host_dns_name = self.get_option("ansible_host_dns_name")
15111524

15121525
self.main()

tests/integration/targets/inventory-latest/files/test-inventory-options.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"tags": []
8282
},
8383
"VC1": {
84-
"ansible_host": "172.16.180.12",
84+
"ansible_host": "nexus.example.com",
8585
"custom_fields": {},
8686
"device_type": "nexus-parent",
8787
"display": "Test Nexus One",

tests/integration/targets/inventory-latest/files/test-inventory-options.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ services: False
1717
group_names_raw: True
1818
virtual_chassis_name: True
1919
dns_name: True
20+
ansible_host_dns_name: True
2021

2122
group_by:
2223
- site

tests/integration/targets/inventory-v2.9/files/test-inventory-options.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"tags": []
8282
},
8383
"VC1": {
84-
"ansible_host": "172.16.180.12",
84+
"ansible_host": "nexus.example.com",
8585
"custom_fields": {},
8686
"device_type": "nexus-parent",
8787
"display": "Test Nexus One",

tests/integration/targets/inventory-v2.9/files/test-inventory-options.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ services: False
1717
group_names_raw: True
1818
virtual_chassis_name: True
1919
dns_name: True
20+
ansible_host_dns_name: True
2021

2122
group_by:
2223
- site

tests/unit/inventory/test_data/group_extractors/data.json

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"interfaces": false,
55
"services": false,
66
"dns_name": false,
7+
"ansible_host_dns_name": false,
78
"expected": [
89
"disk",
910
"memory",
@@ -35,14 +36,56 @@
3536
"manufacturers",
3637
"services",
3738
"interfaces",
38-
"dns_name"
39+
"dns_name",
40+
"ansible_host_dns_name"
3941
]
4042
},
4143
{
4244
"plurals": true,
4345
"interfaces": true,
4446
"services": true,
4547
"dns_name": true,
48+
"ansible_host_dns_name": true,
49+
"expected": [
50+
"disk",
51+
"memory",
52+
"vcpus",
53+
"config_context",
54+
"custom_fields",
55+
"region",
56+
"cluster",
57+
"cluster_group",
58+
"cluster_type",
59+
"is_virtual",
60+
"sites",
61+
"tenants",
62+
"racks",
63+
"tags",
64+
"device_roles",
65+
"platforms",
66+
"device_types",
67+
"manufacturers",
68+
"services",
69+
"interfaces",
70+
"dns_name"
71+
],
72+
"not_expected": [
73+
"site",
74+
"tenant",
75+
"rack",
76+
"tag",
77+
"role",
78+
"platform",
79+
"device_type",
80+
"manufacturer"
81+
]
82+
},
83+
{
84+
"plurals": true,
85+
"interfaces": true,
86+
"services": true,
87+
"dns_name": false,
88+
"ansible_host_dns_name": true,
4689
"expected": [
4790
"disk",
4891
"memory",

tests/unit/inventory/test_nb_inventory.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,24 @@ def does_not_raise():
142142

143143

144144
@pytest.mark.parametrize(
145-
"plurals, services, interfaces, dns_name, expected, not_expected",
145+
"plurals, services, interfaces, dns_name, ansible_host_dns_name, expected, not_expected",
146146
load_relative_test_data("group_extractors"),
147147
)
148148
def test_group_extractors(
149-
inventory_fixture, plurals, services, interfaces, dns_name, expected, not_expected
149+
inventory_fixture,
150+
plurals,
151+
services,
152+
interfaces,
153+
dns_name,
154+
ansible_host_dns_name,
155+
expected,
156+
not_expected,
150157
):
151158
inventory_fixture.plurals = plurals
152159
inventory_fixture.services = services
153160
inventory_fixture.interfaces = interfaces
154161
inventory_fixture.dns_name = dns_name
162+
inventory_fixture.ansible_host_dns_name = ansible_host_dns_name
155163
extractors = inventory_fixture.group_extractors
156164

157165
for key in expected:

0 commit comments

Comments
 (0)