From fee4a0df94390e6cfe05f2b45110ee4eafef04c8 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 27 Mar 2019 19:43:28 -0400 Subject: [PATCH] Handle errors correctly in rhn_channel (#54194) * Handle errors correctly in rhn_channel * also fail if required info is not available --- .../fragments/better_rhn_channel_errors.yml | 2 ++ .../modules/packaging/os/rhn_channel.py | 26 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/better_rhn_channel_errors.yml diff --git a/changelogs/fragments/better_rhn_channel_errors.yml b/changelogs/fragments/better_rhn_channel_errors.yml new file mode 100644 index 0000000000..2e421fdee9 --- /dev/null +++ b/changelogs/fragments/better_rhn_channel_errors.yml @@ -0,0 +1,2 @@ +bugfixes: + - handle xmlrpc errors in the correct fashion for rhn_channel diff --git a/lib/ansible/modules/packaging/os/rhn_channel.py b/lib/ansible/modules/packaging/os/rhn_channel.py index 2c49090cb3..ca725a5aa0 100644 --- a/lib/ansible/modules/packaging/os/rhn_channel.py +++ b/lib/ansible/modules/packaging/os/rhn_channel.py @@ -59,6 +59,7 @@ EXAMPLES = ''' delegate_to: localhost ''' +from ansible.module_utils._text import to_text from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.six.moves import xmlrpc_client @@ -114,14 +115,29 @@ def main(): password = module.params['password'] # initialize connection - client = xmlrpc_client.Server(saturl) - session = client.auth.login(user, password) + client = xmlrpc_client.ServerProxy(saturl) + try: + session = client.auth.login(user, password) + except Exception as e: + module.fail_json(msg="Unable to establish session with Sattelite server: %s " % to_text(e)) + + if not session: + module.fail_json(msg="Failed to establish session with Sattelite server.") # get systemid - sys_id = get_systemid(client, session, systname) + try: + sys_id = get_systemid(client, session, systname) + except Exception as e: + module.fail_json(msg="Unable to get system id: %s " % to_text(e)) + + if not sys_id: + module.fail_json(msg="Failed to get system id.") # get channels for system - chans = base_channels(client, session, sys_id) + try: + chans = base_channels(client, session, sys_id) + except Exception as e: + module.fail_json(msg="Unable to get channel information: %s " % to_text(e)) try: if state == 'present': @@ -137,6 +153,8 @@ def main(): else: unsubscribe_channels(channelname, client, session, systname, sys_id) module.exit_json(changed=True, msg="Channel %s removed" % channelname) + except Exception as e: + module.fail_json('Unable to %s channel (%s): %s' % ('add' if state == 'present' else 'remove', channelname, to_text(e))) finally: client.auth.logout(session)