Repository: ansible-collections/community.general

Issue:
rhn_register: add 'force' option to run even if system already registered
<!--- Verify first that your feature was not already discussed on GitHub -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
<!--- Describe the new feature/improvement briefly below -->
Add a 'force' option to rhn_register module, to allow re-registering a system.

##### ISSUE TYPE
- Feature Idea

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
rhn_register

##### ADDITIONAL INFORMATION
<!--- Describe how the feature would be used, why it is needed and what it would solve -->
rhnks_reg (the command that rhn_register wraps) has a --force option to allow a system to re-register even if it appears to already be registered. While the rhn_register module already uses this when register is called, the code cannot reach that point as it is skipped if the system is already registered.

There are two obvious use-cases for this:
- The host was previously registered, but the registration may have been removed on the server-side
- The host was previously registered with server A, but now should be registered with server B


<!--- Paste example playbooks or commands between quotes below -->
```yaml

- name: Force-register as user with password to ensure registration is current on server                                             
  community.general.rhn_register:
    state: present
    username: joe_user
    password: somepass
    server_url: https://xmlrpc.my.satellite/XMLRPC
    force: yes

```

<!--- HINT: You can also paste gist.github.com links for larger files -->



Discussion:
Files identified in the description:
* [`plugins/modules/packaging/os/rhn_register.py`](https://github.com/ansible-collections/community.general/blob/master/plugins/modules/packaging/os/rhn_register.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @FlossWare @alikins @barnabycourt @jlaska @vritant
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/modules/packaging/os/rhn_register.py b/plugins/modules/packaging/os/rhn_register.py
--- a/plugins/modules/packaging/os/rhn_register.py
+++ b/plugins/modules/packaging/os/rhn_register.py
@@ -49,6 +49,12 @@
         description:
             - Supply an profilename for use with registration.
         type: str
+    force:
+        description:
+            - Force registration, even if system is already registered.
+        type: bool
+        default: no
+        version_added: 2.0.0
     ca_cert:
         description:
             - Supply a custom ssl CA certificate file for use with registration.
@@ -114,6 +120,14 @@
     username: joe_user
     password: somepass
     channels: rhel-x86_64-server-6-foo-1,rhel-x86_64-server-6-bar-1
+
+- name: Force-register as user with password to ensure registration is current on server
+  community.general.rhn_register:
+    state: present
+    username: joe_user
+    password: somepass
+    server_url: https://xmlrpc.my.satellite/XMLRPC
+    force: yes
 '''
 
 RETURN = r'''
@@ -346,6 +360,7 @@ def main():
             ca_cert=dict(type='path', aliases=['sslcacert']),
             systemorgid=dict(type='str'),
             enable_eus=dict(type='bool', default=False),
+            force=dict(type='bool', default=False),
             nopackages=dict(type='bool', default=False),
             channels=dict(type='list', elements='str', default=[]),
         ),
@@ -365,6 +380,7 @@ def main():
     password = module.params['password']
 
     state = module.params['state']
+    force = module.params['force']
     activationkey = module.params['activationkey']
     profilename = module.params['profilename']
     sslcacert = module.params['ca_cert']
@@ -395,7 +411,7 @@ def main():
             module.fail_json(msg="Missing arguments, If registering without an activationkey, must supply username or password")
 
         # Register system
-        if rhn.is_registered:
+        if rhn.is_registered and not force:
             module.exit_json(changed=False, msg="System already registered.")
 
         try:


Test patch: