Skip to content

Commit 7b6445a

Browse files
committed
Add global zones, tune endpoints
1 parent 8e8da8d commit 7b6445a

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

plugins/action/icinga2_zones_conf.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,26 @@ def get_sub_hierarchy(hierarchy, name, groups, parent=None):
5757

5858

5959

60-
def get_best_host_attr(inventory, name):
60+
def get_best_endpoint_attrs(inventory, name, host_options=list(), port_variable=None):
61+
# Return inventory hostname and port 5665 by default
62+
attrs = dict()
63+
attrs["host"] = name
64+
attrs["port"] = "5665"
65+
6166
for host_option in [
62-
"ansible_fqdn",
63-
"ansible_hostname",
67+
#"ansible_fqdn",
68+
"ansible_host",
6469
"inventory_hostname",
6570
]:
6671
if host_option in inventory["hostvars"][name]:
67-
return inventory["hostvars"][name][host_option]
68-
# Return inventory hostname by default
69-
return name
72+
attrs["host"] = inventory["hostvars"][name][host_option]
73+
break
74+
75+
# WIP
76+
if port_variable in inventory["hostvars"][name]:
77+
attrs["port"] = inventory["hostvars"][name][port_variable]
78+
79+
return attrs
7080

7181

7282

@@ -86,16 +96,17 @@ def get_endpoints_from_zones(zones, name, inventory):
8696
endpoint = dict()
8797
endpoint["name"] = endpoint_name
8898

89-
# If own parent
90-
if zone["name"] == own_parent_zone_name:
99+
if (
91100
# If connection to own parent
92-
endpoint["host"] = get_best_host_attr(inventory, endpoint_name)
93-
elif zone["name"] == own_zone_name and endpoint_name != name:
101+
(zone["name"] == own_parent_zone_name) or
102+
94103
# If connection to HA partner
95-
endpoint["host"] = get_best_host_attr(inventory, endpoint_name)
96-
elif "parent" in zone and zone["parent"] == own_zone_name:
104+
(zone["name"] == own_zone_name and endpoint_name != name) or
105+
97106
# If connection to direct children
98-
endpoint["host"] = get_best_host_attr(inventory, endpoint_name)
107+
("parent" in zone and zone["parent"] == own_zone_name)
108+
):
109+
endpoint.update(get_best_endpoint_attrs(inventory, endpoint_name))
99110

100111
endpoints.append(endpoint)
101112

@@ -151,7 +162,8 @@ def run(self, tmp=None, task_vars=None):
151162
#)
152163

153164
#### Variables needed for processing
154-
hierarchy = merge_hash(module_args.pop("hierarchy", False), dict())
165+
hierarchy = merge_hash(module_args.pop("hierarchy", dict()), dict())
166+
global_zones = module_args.pop("global_zones", list())
155167
ansible_inventory_hostname = task_vars["inventory_hostname"]
156168
ansible_groups = task_vars["groups"]
157169
ansible_host_groups = list(task_vars["group_names"])
@@ -171,6 +183,14 @@ def run(self, tmp=None, task_vars=None):
171183
# Get all endpoints for each known zone
172184
icinga2_endpoints = get_endpoints_from_zones(icinga2_zones, ansible_inventory_hostname, task_vars)
173185

186+
# Get all global zones
187+
for zone in global_zones:
188+
zone_object = {
189+
"name": zone,
190+
"global": True
191+
}
192+
icinga2_zones.append(zone_object)
193+
174194
# TO BE DONE: Global zones
175195
# get them from another input instead of hierarchy (e.g. global_zones: ['director-global'])
176196
# ...

0 commit comments

Comments
 (0)