diff --git a/changelogs/fragments/5664-redhat_subscription-credentials-when-needed.yaml b/changelogs/fragments/5664-redhat_subscription-credentials-when-needed.yaml new file mode 100644 index 0000000000..c300b957ce --- /dev/null +++ b/changelogs/fragments/5664-redhat_subscription-credentials-when-needed.yaml @@ -0,0 +1,4 @@ +minor_changes: + - redhat_subscription - credentials (``username``, ``activationkey``, and so on) are required now + only if a system needs to be registered, or ``force_register`` is specified + (https://github.com/ansible-collections/community.general/pull/5664). diff --git a/plugins/modules/redhat_subscription.py b/plugins/modules/redhat_subscription.py index d5f343f23b..45b3cafb3d 100644 --- a/plugins/modules/redhat_subscription.py +++ b/plugins/modules/redhat_subscription.py @@ -26,6 +26,11 @@ notes: config file and default to None. - It is possible to interact with C(subscription-manager) only as root, so root permissions are required to successfully run this module. + - Since community.general 6.5.0, credentials (that is, I(username) and I(password), + I(activationkey), or I(token)) are needed only in case the the system is not registered, + or I(force_register) is specified; this makes it possible to use the module to tweak an + already registered system, for example attaching pools to it (using I(pool), or I(pool_ids)), + and modifying the C(syspurpose) attributes (using I(syspurpose)). requirements: - subscription-manager - Optionally the C(dbus) Python library; this is usually included in the OS @@ -1073,7 +1078,7 @@ def main(): ['activationkey', 'environment'], ['activationkey', 'auto_attach'], ['pool', 'pool_ids']], - required_if=[['state', 'present', ['username', 'activationkey', 'token'], True]], + required_if=[['force_register', True, ['username', 'activationkey', 'token'], True]], ) if getuid() != 0: @@ -1155,6 +1160,8 @@ def main(): else: module.exit_json(changed=False, msg="System already registered.") else: + if not username and not activationkey and not token: + module.fail_json(msg="state is present but any of the following are missing: username, activationkey, token") try: rhsm.enable() rhsm.configure(**module.params) diff --git a/tests/unit/plugins/modules/test_redhat_subscription.py b/tests/unit/plugins/modules/test_redhat_subscription.py index bc1bfffe66..4bf2729163 100644 --- a/tests/unit/plugins/modules/test_redhat_subscription.py +++ b/tests/unit/plugins/modules/test_redhat_subscription.py @@ -35,10 +35,15 @@ def patch_redhat_subscription(mocker): @pytest.mark.parametrize('patch_ansible_module', [{}], indirect=['patch_ansible_module']) @pytest.mark.usefixtures('patch_ansible_module') -def test_without_required_parameters(capfd, patch_redhat_subscription): +def test_without_required_parameters_unregistered(mocker, capfd, patch_redhat_subscription): """ Failure must occurs when all parameters are missing """ + mock_run_command = mocker.patch.object( + basic.AnsibleModule, + 'run_command', + return_value=(1, 'This system is not yet registered.', '')) + with pytest.raises(SystemExit): redhat_subscription.main() out, err = capfd.readouterr() @@ -47,6 +52,27 @@ def test_without_required_parameters(capfd, patch_redhat_subscription): assert 'state is present but any of the following are missing' in results['msg'] +@pytest.mark.parametrize('patch_ansible_module', [{}], indirect=['patch_ansible_module']) +@pytest.mark.usefixtures('patch_ansible_module') +def test_without_required_parameters_registered(mocker, capfd, patch_redhat_subscription): + """ + System already registered, no parameters required (state=present is the + default) + """ + mock_run_command = mocker.patch.object( + basic.AnsibleModule, + 'run_command', + return_value=(0, 'system identity: b26df632-25ed-4452-8f89-0308bfd167cb', '')) + + with pytest.raises(SystemExit): + redhat_subscription.main() + out, err = capfd.readouterr() + results = json.loads(out) + assert 'changed' in results + if 'msg' in results: + assert results['msg'] == 'System already registered.' + + TEST_CASES = [ # Test the case, when the system is already registered [ @@ -73,6 +99,24 @@ TEST_CASES = [ 'msg': 'System already registered.' } ], + # Already registered system without credentials specified + [ + { + 'state': 'present', + }, + { + 'id': 'test_already_registered_system', + 'run_command.calls': [ + ( + ['/testbin/subscription-manager', 'identity'], + {'check_rc': False}, + (0, 'system identity: b26df632-25ed-4452-8f89-0308bfd167cb', '') + ) + ], + 'changed': False, + 'msg': 'System already registered.' + } + ], # Test simple registration using username and password [ { @@ -744,9 +788,6 @@ Entitlement Type: Physical [ { 'state': 'present', - 'username': 'admin', - 'password': 'admin', - 'org_id': 'admin', 'pool_ids': [{'ff8080816b8e967f016b8e99632804a6': 2}, {'ff8080816b8e967f016b8e99747107e9': 4}] }, {