diff --git a/changelogs/fragments/2142-apache2_mod_proxy-cleanup.yml b/changelogs/fragments/2142-apache2_mod_proxy-cleanup.yml new file mode 100644 index 0000000000..6a24f1afc3 --- /dev/null +++ b/changelogs/fragments/2142-apache2_mod_proxy-cleanup.yml @@ -0,0 +1,2 @@ +minor_changes: + - apache2_mod_proxy - refactored/cleaned-up part of the code (https://github.com/ansible-collections/community.general/pull/2142). diff --git a/plugins/modules/web_infrastructure/apache2_mod_proxy.py b/plugins/modules/web_infrastructure/apache2_mod_proxy.py index dcf1656fcf..2ab679aaf6 100644 --- a/plugins/modules/web_infrastructure/apache2_mod_proxy.py +++ b/plugins/modules/web_infrastructure/apache2_mod_proxy.py @@ -198,6 +198,10 @@ members: import re import traceback +from ansible.module_utils.basic import AnsibleModule, missing_required_lib +from ansible.module_utils.urls import fetch_url +from ansible.module_utils.six import iteritems + BEAUTIFUL_SOUP_IMP_ERR = None try: from BeautifulSoup import BeautifulSoup @@ -273,13 +277,8 @@ class BalancerMember(object): 'drained': 'Drn', 'hot_standby': 'Stby', 'ignore_errors': 'Ign'} - status = {} actual_status = str(self.attributes['Status']) - for mode in status_mapping.keys(): - if re.search(pattern=status_mapping[mode], string=actual_status): - status[mode] = True - else: - status[mode] = False + status = dict((mode, patt in actual_status) for mode, patt in iteritems(status_mapping)) return status def set_member_status(self, values): @@ -290,13 +289,10 @@ class BalancerMember(object): 'ignore_errors': '&w_status_I'} request_body = regexp_extraction(self.management_url, EXPRESSION, 1) - for k in values_mapping.keys(): - if values[str(k)]: - request_body = request_body + str(values_mapping[k]) + '=1' - else: - request_body = request_body + str(values_mapping[k]) + '=0' + values_url = "".join("{0}={1}".format(url_param, 1 if values[mode] else 0) for mode, url_param in iteritems(values_mapping)) + request_body = "{0}{1}".format(request_body, values_url) - response = fetch_url(self.module, self.management_url, data=str(request_body)) + response = fetch_url(self.module, self.management_url, data=request_body) if response[1]['status'] != 200: self.module.fail_json(msg="Could not set the member status! " + self.host + " " + response[1]['status']) @@ -309,11 +305,11 @@ class Balancer(object): def __init__(self, host, suffix, module, members=None, tls=False): if tls: - self.base_url = str(str('https://') + str(host)) - self.url = str(str('https://') + str(host) + str(suffix)) + self.base_url = 'https://' + str(host) + self.url = 'https://' + str(host) + str(suffix) else: - self.base_url = str(str('http://') + str(host)) - self.url = str(str('http://') + str(host) + str(suffix)) + self.base_url = 'http://' + str(host) + self.url = 'http://' + str(host) + str(suffix) self.module = module self.page = self.fetch_balancer_page() if members is None: @@ -444,7 +440,5 @@ def main(): module.fail_json(msg=str(module.params['member_host']) + ' is not a member of the balancer ' + str(module.params['balancer_vhost']) + '!') -from ansible.module_utils.basic import AnsibleModule, missing_required_lib -from ansible.module_utils.urls import fetch_url if __name__ == '__main__': main()