Skip to content

Commit 554287e

Browse files
committed
Add ability to use variable to set host attribute
1 parent e5ef25f commit 554287e

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

plugins/action/icinga2_zones_conf.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def get_best_endpoint_attrs(inventory, name, host_options=list(), port_variable=
6363
attrs["host"] = name
6464
attrs["port"] = "5665"
6565

66-
for host_option in [
66+
for host_option in host_options + [
6767
#"ansible_fqdn",
6868
"ansible_host",
6969
"inventory_hostname",
@@ -80,7 +80,7 @@ def get_best_endpoint_attrs(inventory, name, host_options=list(), port_variable=
8080

8181

8282

83-
def get_endpoints_from_zones(zones, name, inventory):
83+
def get_endpoints_from_zones(zones, name, inventory, upper_host_options=list(), middle_host_options=list(), lower_host_options=list(), port_variable=None):
8484
# Return emtpy list if no zones are given
8585
if not zones:
8686
return list()
@@ -96,17 +96,26 @@ def get_endpoints_from_zones(zones, name, inventory):
9696
endpoint = dict()
9797
endpoint["name"] = endpoint_name
9898

99-
if (
100-
# If connection to own parent
101-
(zone["name"] == own_parent_zone_name) or
99+
# If connection to own parent
100+
if zone["name"] == own_parent_zone_name:
101+
host_attributes = get_best_endpoint_attrs(inventory, endpoint_name, upper_host_options, port_variable)
102102

103-
# If connection to HA partner
104-
(zone["name"] == own_zone_name and endpoint_name != name) or
103+
# If connection to HA partner
104+
elif zone["name"] == own_zone_name and endpoint_name != name:
105+
host_attributes = get_best_endpoint_attrs(inventory, endpoint_name, middle_host_options, port_variable)
105106

106-
# If connection to direct children
107-
("parent" in zone and zone["parent"] == own_zone_name)
108-
):
109-
endpoint.update(get_best_endpoint_attrs(inventory, endpoint_name))
107+
# If connection to direct children
108+
elif "parent" in zone and zone["parent"] == own_zone_name:
109+
host_attributes = get_best_endpoint_attrs(inventory, endpoint_name, lower_host_options, port_variable)
110+
print("HIER", endpoint_name, name)
111+
print(host_attributes)
112+
113+
# If no direct connection needed
114+
else:
115+
host_attributes = None
116+
117+
if host_attributes:
118+
endpoint.update(host_attributes)
110119

111120
endpoints.append(endpoint)
112121

@@ -164,6 +173,20 @@ def run(self, tmp=None, task_vars=None):
164173
#### Variables needed for processing
165174
hierarchy = merge_hash(module_args.pop("hierarchy", dict()), dict())
166175
global_zones = module_args.pop("global_zones", list())
176+
177+
# Get connection variables for host attribute
178+
upper_host_variables = module_args.pop("upper", list())
179+
if isinstance(upper_host_variables, str):
180+
upper_host_variables = [upper_host_variables]
181+
182+
middle_host_variables = module_args.pop("middle", list())
183+
if isinstance(middle_host_variables, str):
184+
middle_host_variables = [middle_host_variables]
185+
186+
lower_host_variables = module_args.pop("lower", list())
187+
if isinstance(lower_host_variables, str):
188+
lower_host_variables = [lower_host_variables]
189+
167190
ansible_inventory_hostname = task_vars["inventory_hostname"]
168191
ansible_groups = task_vars["groups"]
169192
ansible_host_groups = list(task_vars["group_names"])
@@ -181,7 +204,14 @@ def run(self, tmp=None, task_vars=None):
181204
icinga2_zones = get_zones_from_hierarchy(sub_hierarchy, ansible_groups)
182205

183206
# Get all endpoints for each known zone
184-
icinga2_endpoints = get_endpoints_from_zones(icinga2_zones, ansible_inventory_hostname, task_vars)
207+
icinga2_endpoints = get_endpoints_from_zones(
208+
icinga2_zones,
209+
ansible_inventory_hostname,
210+
task_vars,
211+
upper_host_options=upper_host_variables,
212+
middle_host_options=middle_host_variables,
213+
lower_host_options=lower_host_variables
214+
)
185215

186216
# Get all global zones
187217
for zone in global_zones:

0 commit comments

Comments
 (0)