2020-03-09 09:11:07 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 20:12:10 +00:00
|
|
|
# Copyright (c) 2016, Olivier Boukili <boukili.olivier@gmail.com>
|
2022-08-05 10:28:29 +00:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 09:11:07 +00:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
2024-12-27 13:29:15 +00:00
|
|
|
DOCUMENTATION = r"""
|
2020-03-09 09:11:07 +00:00
|
|
|
module: apache2_mod_proxy
|
|
|
|
author: Olivier Boukili (@oboukili)
|
|
|
|
short_description: Set and/or get members' attributes of an Apache httpd 2.4 mod_proxy balancer pool
|
|
|
|
description:
|
2024-12-27 13:29:15 +00:00
|
|
|
- Set and/or get members' attributes of an Apache httpd 2.4 mod_proxy balancer pool, using HTTP POST and GET requests. The
|
|
|
|
httpd mod_proxy balancer-member status page has to be enabled and accessible, as this module relies on parsing this page.
|
Add attributes to apache2, cobbler, dimensiondata, icinga2, lxca, pritunl, and spectrum modules (#5963)
Add attributes to apache2, cobbler, dimensiondata, icinga2, lxca, pritunl, and spectrum modules.
2023-02-24 08:24:37 +00:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
2024-12-27 13:29:15 +00:00
|
|
|
requirements:
|
|
|
|
- Python package C(BeautifulSoup).
|
Add attributes to apache2, cobbler, dimensiondata, icinga2, lxca, pritunl, and spectrum modules (#5963)
Add attributes to apache2, cobbler, dimensiondata, icinga2, lxca, pritunl, and spectrum modules.
2023-02-24 08:24:37 +00:00
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 09:11:07 +00:00
|
|
|
options:
|
|
|
|
balancer_url_suffix:
|
2020-10-31 12:53:57 +00:00
|
|
|
type: str
|
2020-03-09 09:11:07 +00:00
|
|
|
description:
|
2024-12-27 13:29:15 +00:00
|
|
|
- Suffix of the balancer pool URL required to access the balancer pool status page (for example V(balancer_vhost[:port]/balancer_url_suffix)).
|
2020-03-09 09:11:07 +00:00
|
|
|
default: /balancer-manager/
|
|
|
|
balancer_vhost:
|
2020-10-31 12:53:57 +00:00
|
|
|
type: str
|
2020-03-09 09:11:07 +00:00
|
|
|
description:
|
2024-12-27 13:29:15 +00:00
|
|
|
- (IPv4|IPv6|FQDN):port of the Apache httpd 2.4 mod_proxy balancer pool.
|
2020-03-09 09:11:07 +00:00
|
|
|
required: true
|
|
|
|
member_host:
|
2020-10-31 12:53:57 +00:00
|
|
|
type: str
|
2020-03-09 09:11:07 +00:00
|
|
|
description:
|
2024-12-27 13:29:15 +00:00
|
|
|
- (IPv4|IPv6|FQDN) of the balancer member to get or to set attributes to. Port number is autodetected and should not
|
2025-01-23 10:03:51 +00:00
|
|
|
be specified here.
|
|
|
|
- If undefined, the M(community.general.apache2_mod_proxy) module will return a members list of dictionaries of all the current
|
2024-12-27 13:29:15 +00:00
|
|
|
balancer pool members' attributes.
|
2020-03-09 09:11:07 +00:00
|
|
|
state:
|
2025-01-23 05:45:45 +00:00
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
choices: [present, absent, enabled, disabled, drained, hot_standby, ignore_errors]
|
2020-03-09 09:11:07 +00:00
|
|
|
description:
|
2025-01-23 05:45:45 +00:00
|
|
|
- Desired state of the member host.
|
|
|
|
- States can be simultaneously invoked by separating them with a comma (for example V(state=drained,ignore_errors)),
|
|
|
|
but it is recommended to specify them as a proper YAML list.
|
|
|
|
- States V(present) and V(absent) must be used without any other state.
|
2020-03-09 09:11:07 +00:00
|
|
|
tls:
|
|
|
|
description:
|
|
|
|
- Use https to access balancer management page.
|
|
|
|
type: bool
|
2022-08-24 18:00:11 +00:00
|
|
|
default: false
|
2020-03-09 09:11:07 +00:00
|
|
|
validate_certs:
|
|
|
|
description:
|
|
|
|
- Validate ssl/tls certificates.
|
|
|
|
type: bool
|
2022-08-24 18:00:11 +00:00
|
|
|
default: true
|
2024-12-27 13:29:15 +00:00
|
|
|
"""
|
2020-03-09 09:11:07 +00:00
|
|
|
|
2024-12-27 13:29:15 +00:00
|
|
|
EXAMPLES = r"""
|
2020-05-15 10:27:06 +00:00
|
|
|
- name: Get all current balancer pool members attributes
|
2020-07-13 19:50:31 +00:00
|
|
|
community.general.apache2_mod_proxy:
|
2020-03-09 09:11:07 +00:00
|
|
|
balancer_vhost: 10.0.0.2
|
|
|
|
|
2020-05-15 10:27:06 +00:00
|
|
|
- name: Get a specific member attributes
|
2020-07-13 19:50:31 +00:00
|
|
|
community.general.apache2_mod_proxy:
|
2020-03-09 09:11:07 +00:00
|
|
|
balancer_vhost: myws.mydomain.org
|
|
|
|
balancer_suffix: /lb/
|
|
|
|
member_host: node1.myws.mydomain.org
|
|
|
|
|
|
|
|
# Enable all balancer pool members:
|
2020-05-15 10:27:06 +00:00
|
|
|
- name: Get attributes
|
2020-07-13 19:50:31 +00:00
|
|
|
community.general.apache2_mod_proxy:
|
2020-03-09 09:11:07 +00:00
|
|
|
balancer_vhost: '{{ myloadbalancer_host }}'
|
|
|
|
register: result
|
2020-05-15 10:27:06 +00:00
|
|
|
|
|
|
|
- name: Enable all balancer pool members
|
2020-07-13 19:50:31 +00:00
|
|
|
community.general.apache2_mod_proxy:
|
2020-03-09 09:11:07 +00:00
|
|
|
balancer_vhost: '{{ myloadbalancer_host }}'
|
|
|
|
member_host: '{{ item.host }}'
|
|
|
|
state: present
|
|
|
|
with_items: '{{ result.members }}'
|
|
|
|
|
|
|
|
# Gracefully disable a member from a loadbalancer node:
|
2020-05-15 10:27:06 +00:00
|
|
|
- name: Step 1
|
2020-07-13 19:50:31 +00:00
|
|
|
community.general.apache2_mod_proxy:
|
2020-03-09 09:11:07 +00:00
|
|
|
balancer_vhost: '{{ vhost_host }}'
|
|
|
|
member_host: '{{ member.host }}'
|
|
|
|
state: drained
|
|
|
|
delegate_to: myloadbalancernode
|
2020-05-15 10:27:06 +00:00
|
|
|
|
|
|
|
- name: Step 2
|
2020-07-16 11:42:12 +00:00
|
|
|
ansible.builtin.wait_for:
|
2020-03-09 09:11:07 +00:00
|
|
|
host: '{{ member.host }}'
|
|
|
|
port: '{{ member.port }}'
|
|
|
|
state: drained
|
|
|
|
delegate_to: myloadbalancernode
|
2020-05-15 10:27:06 +00:00
|
|
|
|
|
|
|
- name: Step 3
|
2020-07-13 19:50:31 +00:00
|
|
|
community.general.apache2_mod_proxy:
|
2020-03-09 09:11:07 +00:00
|
|
|
balancer_vhost: '{{ vhost_host }}'
|
|
|
|
member_host: '{{ member.host }}'
|
|
|
|
state: absent
|
|
|
|
delegate_to: myloadbalancernode
|
2024-12-27 13:29:15 +00:00
|
|
|
"""
|
2020-03-09 09:11:07 +00:00
|
|
|
|
2024-12-27 13:29:15 +00:00
|
|
|
RETURN = r"""
|
2020-03-09 09:11:07 +00:00
|
|
|
member:
|
2025-01-23 10:03:51 +00:00
|
|
|
description: Specific balancer member information dictionary, returned when the module is invoked with O(member_host) parameter.
|
2024-12-27 13:29:15 +00:00
|
|
|
type: dict
|
|
|
|
returned: success
|
|
|
|
sample:
|
|
|
|
{"attributes":
|
|
|
|
{"Busy": "0",
|
|
|
|
"Elected": "42",
|
|
|
|
"Factor": "1",
|
|
|
|
"From": "136K",
|
|
|
|
"Load": "0",
|
|
|
|
"Route": null,
|
|
|
|
"RouteRedir": null,
|
|
|
|
"Set": "0",
|
|
|
|
"Status": "Init Ok ",
|
|
|
|
"To": " 47K",
|
|
|
|
"Worker URL": null
|
|
|
|
},
|
|
|
|
"balancer_url": "http://10.10.0.2/balancer-manager/",
|
|
|
|
"host": "10.10.0.20",
|
|
|
|
"management_url": "http://10.10.0.2/lb/?b=mywsbalancer&w=http://10.10.0.20:8080/ws&nonce=8925436c-79c6-4841-8936-e7d13b79239b",
|
|
|
|
"path": "/ws",
|
|
|
|
"port": 8080,
|
|
|
|
"protocol": "http",
|
|
|
|
"status": {
|
|
|
|
"disabled": false,
|
|
|
|
"drained": false,
|
|
|
|
"hot_standby": false,
|
|
|
|
"ignore_errors": false
|
2020-03-09 09:11:07 +00:00
|
|
|
}
|
2024-12-27 13:29:15 +00:00
|
|
|
}
|
2020-03-09 09:11:07 +00:00
|
|
|
members:
|
2025-01-23 10:03:51 +00:00
|
|
|
description: List of member (defined above) dictionaries, returned when the module is invoked with no O(member_host) and
|
|
|
|
O(state) args.
|
2024-12-27 13:29:15 +00:00
|
|
|
returned: success
|
|
|
|
type: list
|
|
|
|
sample:
|
|
|
|
[{"attributes": {
|
|
|
|
"Busy": "0",
|
|
|
|
"Elected": "42",
|
|
|
|
"Factor": "1",
|
|
|
|
"From": "136K",
|
|
|
|
"Load": "0",
|
|
|
|
"Route": null,
|
|
|
|
"RouteRedir": null,
|
|
|
|
"Set": "0",
|
|
|
|
"Status": "Init Ok ",
|
|
|
|
"To": " 47K",
|
|
|
|
"Worker URL": null
|
|
|
|
},
|
|
|
|
"balancer_url": "http://10.10.0.2/balancer-manager/",
|
|
|
|
"host": "10.10.0.20",
|
|
|
|
"management_url": "http://10.10.0.2/lb/?b=mywsbalancer&w=http://10.10.0.20:8080/ws&nonce=8925436c-79c6-4841-8936-e7d13b79239b",
|
|
|
|
"path": "/ws",
|
|
|
|
"port": 8080,
|
|
|
|
"protocol": "http",
|
|
|
|
"status": {
|
|
|
|
"disabled": false,
|
|
|
|
"drained": false,
|
|
|
|
"hot_standby": false,
|
|
|
|
"ignore_errors": false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{"attributes": {
|
|
|
|
"Busy": "0",
|
|
|
|
"Elected": "42",
|
|
|
|
"Factor": "1",
|
|
|
|
"From": "136K",
|
|
|
|
"Load": "0",
|
|
|
|
"Route": null,
|
|
|
|
"RouteRedir": null,
|
|
|
|
"Set": "0",
|
|
|
|
"Status": "Init Ok ",
|
|
|
|
"To": " 47K",
|
|
|
|
"Worker URL": null
|
|
|
|
},
|
|
|
|
"balancer_url": "http://10.10.0.2/balancer-manager/",
|
|
|
|
"host": "10.10.0.21",
|
|
|
|
"management_url": "http://10.10.0.2/lb/?b=mywsbalancer&w=http://10.10.0.21:8080/ws&nonce=8925436c-79c6-4841-8936-e7d13b79239b",
|
|
|
|
"path": "/ws",
|
|
|
|
"port": 8080,
|
|
|
|
"protocol": "http",
|
|
|
|
"status": {
|
|
|
|
"disabled": false,
|
|
|
|
"drained": false,
|
|
|
|
"hot_standby": false,
|
|
|
|
"ignore_errors": false}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
"""
|
2020-03-09 09:11:07 +00:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
2025-01-23 19:33:38 +00:00
|
|
|
from ansible_collections.community.general.plugins.module_utils import deps
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-04-01 05:50:24 +00:00
|
|
|
from ansible.module_utils.urls import fetch_url
|
|
|
|
from ansible.module_utils.six import iteritems
|
|
|
|
|
2025-01-23 19:33:38 +00:00
|
|
|
with deps.declare("BeautifulSoup"):
|
2020-03-09 09:11:07 +00:00
|
|
|
from BeautifulSoup import BeautifulSoup
|
|
|
|
|
|
|
|
# balancer member attributes extraction regexp:
|
2025-01-23 19:32:59 +00:00
|
|
|
EXPRESSION = re.compile(r"(b=([\w\.\-]+)&w=(https?|ajp|wss?|ftp|[sf]cgi)://([\w\.\-]+):?(\d*)([/\w\.\-]*)&?[\w\-\=]*)")
|
2020-03-09 09:11:07 +00:00
|
|
|
# Apache2 server version extraction regexp:
|
2025-01-23 19:32:59 +00:00
|
|
|
APACHE_VERSION_EXPRESSION = re.compile(r"SERVER VERSION: APACHE/([\d.]+)")
|
2020-03-09 09:11:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def regexp_extraction(string, _regexp, groups=1):
|
|
|
|
""" Returns the capture group (default=1) specified in the regexp, applied to the string """
|
2025-01-23 19:32:59 +00:00
|
|
|
regexp_search = _regexp.search(string)
|
2020-03-09 09:11:07 +00:00
|
|
|
if regexp_search:
|
|
|
|
if regexp_search.group(groups) != '':
|
2025-01-26 11:44:59 +00:00
|
|
|
return regexp_search.group(groups)
|
2020-03-09 09:11:07 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
class BalancerMember(object):
|
|
|
|
""" Apache 2.4 mod_proxy LB balancer member.
|
|
|
|
attributes:
|
|
|
|
read-only:
|
|
|
|
host -> member host (string),
|
|
|
|
management_url -> member management url (string),
|
|
|
|
protocol -> member protocol (string)
|
|
|
|
port -> member port (string),
|
|
|
|
path -> member location (string),
|
|
|
|
balancer_url -> url of this member's parent balancer (string),
|
|
|
|
attributes -> whole member attributes (dictionary)
|
|
|
|
module -> ansible module instance (AnsibleModule object).
|
|
|
|
writable:
|
|
|
|
status -> status of the member (dictionary)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, management_url, balancer_url, module):
|
2025-01-23 19:32:59 +00:00
|
|
|
self.host = regexp_extraction(management_url, EXPRESSION, 4)
|
2025-01-26 11:44:59 +00:00
|
|
|
self.management_url = management_url
|
2020-03-09 09:11:07 +00:00
|
|
|
self.protocol = regexp_extraction(management_url, EXPRESSION, 3)
|
|
|
|
self.port = regexp_extraction(management_url, EXPRESSION, 5)
|
|
|
|
self.path = regexp_extraction(management_url, EXPRESSION, 6)
|
2025-01-26 11:44:59 +00:00
|
|
|
self.balancer_url = balancer_url
|
2020-03-09 09:11:07 +00:00
|
|
|
self.module = module
|
|
|
|
|
|
|
|
def get_member_attributes(self):
|
|
|
|
""" Returns a dictionary of a balancer member's attributes."""
|
|
|
|
|
2025-01-23 05:45:33 +00:00
|
|
|
resp, info = fetch_url(self.module, self.management_url)
|
2020-03-09 09:11:07 +00:00
|
|
|
|
2025-01-23 05:45:33 +00:00
|
|
|
if info['status'] != 200:
|
2025-01-26 11:44:59 +00:00
|
|
|
self.module.fail_json(msg="Could not get balancer_member_page, check for connectivity! {0}".format(info))
|
2020-03-09 09:11:07 +00:00
|
|
|
else:
|
|
|
|
try:
|
2025-01-23 05:45:33 +00:00
|
|
|
soup = BeautifulSoup(resp)
|
2022-08-12 09:07:30 +00:00
|
|
|
except TypeError as exc:
|
2025-01-26 11:44:59 +00:00
|
|
|
self.module.fail_json(msg="Cannot parse balancer_member_page HTML! {0}".format(exc))
|
2020-03-09 09:11:07 +00:00
|
|
|
else:
|
|
|
|
subsoup = soup.findAll('table')[1].findAll('tr')
|
|
|
|
keys = subsoup[0].findAll('th')
|
|
|
|
for valuesset in subsoup[1::1]:
|
|
|
|
if re.search(pattern=self.host, string=str(valuesset)):
|
|
|
|
values = valuesset.findAll('td')
|
2024-09-08 12:22:37 +00:00
|
|
|
return {keys[x].string: values[x].string for x in range(0, len(keys))}
|
2020-03-09 09:11:07 +00:00
|
|
|
|
|
|
|
def get_member_status(self):
|
|
|
|
""" Returns a dictionary of a balancer member's status attributes."""
|
|
|
|
status_mapping = {'disabled': 'Dis',
|
|
|
|
'drained': 'Drn',
|
|
|
|
'hot_standby': 'Stby',
|
|
|
|
'ignore_errors': 'Ign'}
|
2025-01-26 11:44:59 +00:00
|
|
|
actual_status = self.attributes['Status']
|
2024-09-01 18:22:53 +00:00
|
|
|
status = {mode: patt in actual_status for mode, patt in iteritems(status_mapping)}
|
2020-03-09 09:11:07 +00:00
|
|
|
return status
|
|
|
|
|
|
|
|
def set_member_status(self, values):
|
|
|
|
""" Sets a balancer member's status attributes amongst pre-mapped values."""
|
|
|
|
values_mapping = {'disabled': '&w_status_D',
|
|
|
|
'drained': '&w_status_N',
|
|
|
|
'hot_standby': '&w_status_H',
|
|
|
|
'ignore_errors': '&w_status_I'}
|
|
|
|
|
|
|
|
request_body = regexp_extraction(self.management_url, EXPRESSION, 1)
|
2025-01-23 05:45:33 +00:00
|
|
|
values_url = "".join("{0}={1}".format(url_param, 1 if values[mode] else 0) for mode, url_param in values_mapping.items())
|
2021-04-01 05:50:24 +00:00
|
|
|
request_body = "{0}{1}".format(request_body, values_url)
|
2020-03-09 09:11:07 +00:00
|
|
|
|
2025-01-23 05:45:33 +00:00
|
|
|
response, info = fetch_url(self.module, self.management_url, data=request_body)
|
|
|
|
if info['status'] != 200:
|
|
|
|
self.module.fail_json(msg="Could not set the member status! " + self.host + " " + info['status'])
|
2020-03-09 09:11:07 +00:00
|
|
|
|
|
|
|
attributes = property(get_member_attributes)
|
|
|
|
status = property(get_member_status, set_member_status)
|
|
|
|
|
2025-01-22 19:05:12 +00:00
|
|
|
def as_dict(self):
|
|
|
|
return {
|
|
|
|
"host": self.host,
|
|
|
|
"status": self.status,
|
|
|
|
"protocol": self.protocol,
|
|
|
|
"port": self.port,
|
|
|
|
"path": self.path,
|
|
|
|
"attributes": self.attributes,
|
|
|
|
"management_url": self.management_url,
|
|
|
|
"balancer_url": self.balancer_url
|
|
|
|
}
|
|
|
|
|
2020-03-09 09:11:07 +00:00
|
|
|
|
|
|
|
class Balancer(object):
|
|
|
|
""" Apache httpd 2.4 mod_proxy balancer object"""
|
|
|
|
|
2025-01-26 11:44:59 +00:00
|
|
|
def __init__(self, host, suffix, module, tls=False):
|
2020-03-09 09:11:07 +00:00
|
|
|
if tls:
|
2025-01-26 11:44:59 +00:00
|
|
|
self.base_url = 'https://{0}'.format(host)
|
|
|
|
self.url = 'https://{0}{1}'.format(host, suffix)
|
2020-03-09 09:11:07 +00:00
|
|
|
else:
|
2025-01-26 11:44:59 +00:00
|
|
|
self.base_url = 'http://{0}'.format(host)
|
|
|
|
self.url = 'http://{0}{1}'.format(host, suffix)
|
2020-03-09 09:11:07 +00:00
|
|
|
self.module = module
|
|
|
|
self.page = self.fetch_balancer_page()
|
|
|
|
|
|
|
|
def fetch_balancer_page(self):
|
|
|
|
""" Returns the balancer management html page as a string for later parsing."""
|
2025-01-26 11:44:59 +00:00
|
|
|
resp, info = fetch_url(self.module, self.url)
|
2025-01-23 05:45:33 +00:00
|
|
|
if info['status'] != 200:
|
2025-01-26 11:44:59 +00:00
|
|
|
self.module.fail_json(msg="Could not get balancer page! HTTP status response: {0}".format(info['status']))
|
2020-03-09 09:11:07 +00:00
|
|
|
else:
|
2025-01-23 05:45:33 +00:00
|
|
|
content = resp.read()
|
2020-03-09 09:11:07 +00:00
|
|
|
apache_version = regexp_extraction(content.upper(), APACHE_VERSION_EXPRESSION, 1)
|
|
|
|
if apache_version:
|
|
|
|
if not re.search(pattern=r"2\.4\.[\d]*", string=apache_version):
|
|
|
|
self.module.fail_json(msg="This module only acts on an Apache2 2.4+ instance, current Apache2 version: " + str(apache_version))
|
|
|
|
return content
|
2025-01-26 11:44:59 +00:00
|
|
|
|
|
|
|
self.module.fail_json(msg="Could not get the Apache server version from the balancer-manager")
|
2020-03-09 09:11:07 +00:00
|
|
|
|
|
|
|
def get_balancer_members(self):
|
|
|
|
""" Returns members of the balancer as a generator object for later iteration."""
|
|
|
|
try:
|
|
|
|
soup = BeautifulSoup(self.page)
|
|
|
|
except TypeError:
|
2025-01-26 11:44:59 +00:00
|
|
|
self.module.fail_json(msg="Cannot parse balancer page HTML! {0}".format(self.page))
|
2020-03-09 09:11:07 +00:00
|
|
|
else:
|
|
|
|
for element in soup.findAll('a')[1::1]:
|
|
|
|
balancer_member_suffix = str(element.get('href'))
|
|
|
|
if not balancer_member_suffix:
|
|
|
|
self.module.fail_json(msg="Argument 'balancer_member_suffix' is empty!")
|
|
|
|
else:
|
2025-01-26 11:44:59 +00:00
|
|
|
yield BalancerMember(self.base_url + balancer_member_suffix, self.url, self.module)
|
2020-03-09 09:11:07 +00:00
|
|
|
|
|
|
|
members = property(get_balancer_members)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
""" Initiates module."""
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2020-10-31 12:53:57 +00:00
|
|
|
balancer_vhost=dict(required=True, type='str'),
|
2020-03-09 09:11:07 +00:00
|
|
|
balancer_url_suffix=dict(default="/balancer-manager/", type='str'),
|
|
|
|
member_host=dict(type='str'),
|
2025-01-23 05:45:45 +00:00
|
|
|
state=dict(type='list', elements='str', choices=['present', 'absent', 'enabled', 'disabled', 'drained', 'hot_standby', 'ignore_errors']),
|
2020-03-09 09:11:07 +00:00
|
|
|
tls=dict(default=False, type='bool'),
|
|
|
|
validate_certs=dict(default=True, type='bool')
|
|
|
|
),
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
2025-01-23 19:33:38 +00:00
|
|
|
deps.validate(module)
|
2020-03-09 09:11:07 +00:00
|
|
|
|
|
|
|
if module.params['state'] is not None:
|
2025-01-23 05:45:45 +00:00
|
|
|
states = module.params['state']
|
2020-03-09 09:11:07 +00:00
|
|
|
if (len(states) > 1) and (("present" in states) or ("enabled" in states)):
|
|
|
|
module.fail_json(msg="state present/enabled is mutually exclusive with other states!")
|
|
|
|
else:
|
|
|
|
states = ['None']
|
|
|
|
|
|
|
|
mybalancer = Balancer(module.params['balancer_vhost'],
|
|
|
|
module.params['balancer_url_suffix'],
|
|
|
|
module=module,
|
|
|
|
tls=module.params['tls'])
|
|
|
|
|
|
|
|
if module.params['member_host'] is None:
|
|
|
|
json_output_list = []
|
|
|
|
for member in mybalancer.members:
|
2025-01-22 19:05:12 +00:00
|
|
|
json_output_list.append(member.as_dict())
|
2020-03-09 09:11:07 +00:00
|
|
|
module.exit_json(
|
|
|
|
changed=False,
|
|
|
|
members=json_output_list
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
changed = False
|
|
|
|
member_exists = False
|
|
|
|
member_status = {'disabled': False, 'drained': False, 'hot_standby': False, 'ignore_errors': False}
|
2025-01-26 11:44:59 +00:00
|
|
|
for mode in member_status:
|
2020-03-09 09:11:07 +00:00
|
|
|
for state in states:
|
|
|
|
if mode == state:
|
|
|
|
member_status[mode] = True
|
|
|
|
elif mode == 'disabled' and state == 'absent':
|
|
|
|
member_status[mode] = True
|
|
|
|
|
|
|
|
for member in mybalancer.members:
|
2025-01-26 11:44:59 +00:00
|
|
|
if str(member.host) == module.params['member_host']:
|
2020-03-09 09:11:07 +00:00
|
|
|
member_exists = True
|
|
|
|
if module.params['state'] is not None:
|
|
|
|
member_status_before = member.status
|
|
|
|
if not module.check_mode:
|
|
|
|
member_status_after = member.status = member_status
|
|
|
|
else:
|
|
|
|
member_status_after = member_status
|
|
|
|
if member_status_before != member_status_after:
|
|
|
|
changed = True
|
2025-01-22 19:05:12 +00:00
|
|
|
json_output = member.as_dict()
|
2020-03-09 09:11:07 +00:00
|
|
|
if member_exists:
|
|
|
|
module.exit_json(
|
|
|
|
changed=changed,
|
|
|
|
member=json_output
|
|
|
|
)
|
|
|
|
else:
|
2025-01-26 11:44:59 +00:00
|
|
|
module.fail_json(
|
|
|
|
msg='{member_host} is not a member of the balancer {balancer_vhost}!'.format(
|
|
|
|
member_host=module.params['member_host'],
|
|
|
|
balancer_vhost=module.params['balancer_vhost'],
|
|
|
|
)
|
|
|
|
)
|
2020-03-09 09:11:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|