diff --git a/lib/ansible/modules/network/ios/ios_user.py b/lib/ansible/modules/network/ios/ios_user.py index 5b66b124b17..18a798938d9 100644 --- a/lib/ansible/modules/network/ios/ios_user.py +++ b/lib/ansible/modules/network/ios/ios_user.py @@ -139,6 +139,7 @@ commands: """ import re +import json from functools import partial @@ -153,6 +154,14 @@ def validate_privilege(value, module): module.fail_json(msg='privilege must be between 1 and 15, got %s' % value) +def user_del_cmd(username): + return json.dumps({ + 'command': 'no username %s' % username, + 'prompt': 'This operation will remove all username related configurations with same name', + 'answer': 'y' + }) + + def map_obj_to_commands(updates, module): commands = list() state = module.params['state'] @@ -168,7 +177,7 @@ def map_obj_to_commands(updates, module): want, have = update if want['state'] == 'absent': - commands.append('no username %s' % want['name']) + commands.append(user_del_cmd(want['name'])) continue if needs_update(want, have, 'view'): @@ -185,7 +194,7 @@ def map_obj_to_commands(updates, module): if want['nopassword']: add(commands, want, 'nopassword') else: - add(commands, want, 'no username %s nopassword' % want['name']) + add(commands, want, user_del_cmd(want['name'])) return commands @@ -336,14 +345,15 @@ def main(): have_users = [x['name'] for x in have] for item in set(have_users).difference(want_users): if item != 'admin': - commands.append('no username %s' % item) + commands.append(user_del_cmd(item)) result['commands'] = commands # the ios cli prevents this by rule so capture it and display # a nice failure message - if 'no username admin' in commands: - module.fail_json(msg='cannot delete the `admin` account') + for cmd in commands: + if 'no username admin' in cmd: + module.fail_json(msg='cannot delete the `admin` account') if commands: if not module.check_mode: diff --git a/test/integration/targets/ios_user/tests/cli/basic.yaml b/test/integration/targets/ios_user/tests/cli/basic.yaml index 9278d31114a..ac977e304d6 100644 --- a/test/integration/targets/ios_user/tests/cli/basic.yaml +++ b/test/integration/targets/ios_user/tests/cli/basic.yaml @@ -1,4 +1,15 @@ --- +- name: tear down old users if they exist (Setup) + ios_user: + aggregate: + - name: ansibletest1 + - name: ansibletest2 + - name: ansibletest3 + state: absent + authorize: yes + provider: "{{ cli }}" + register: result + - name: Create user (SetUp) ios_user: name: ansibletest1 @@ -75,4 +86,6 @@ - assert: that: - 'result.changed == true' - - 'result.commands == ["no username ansibletest1", "no username ansibletest2", "no username ansibletest3"]' + - '"no username ansibletest1" in result.commands[0]' + - '"no username ansibletest2" in result.commands[1]' + - '"no username ansibletest3" in result.commands[2]' diff --git a/test/units/modules/network/ios/test_ios_user.py b/test/units/modules/network/ios/test_ios_user.py index bcc9fa6420b..5feaa50db2f 100644 --- a/test/units/modules/network/ios/test_ios_user.py +++ b/test/units/modules/network/ios/test_ios_user.py @@ -53,7 +53,17 @@ class TestIosUserModule(TestIosModule): def test_ios_user_delete(self): set_module_args(dict(name='ansible', state='absent')) result = self.execute_module(changed=True) - self.assertEqual(result['commands'], ['no username ansible']) + cmd = json.loads( + '{"answer": "y", ' + + '"prompt": "This operation will remove all username related ' + + 'configurations with same name", "command": "no username ansible"}' + ) + + result_cmd = [] + for i in result['commands']: + result_cmd.append(json.loads(i)) + + self.assertEqual(result_cmd, [cmd]) def test_ios_user_password(self): set_module_args(dict(name='ansible', password='test')) @@ -72,7 +82,17 @@ class TestIosUserModule(TestIosModule): def test_ios_user_purge(self): set_module_args(dict(purge=True)) result = self.execute_module(changed=True) - self.assertEqual(result['commands'], ['no username ansible']) + cmd = json.loads( + '{"answer": "y", ' + + '"prompt": "This operation will remove all username related ' + + 'configurations with same name", "command": "no username ansible"}' + ) + + result_cmd = [] + for i in result['commands']: + result_cmd.append(json.loads(i)) + + self.assertEqual(result_cmd, [cmd]) def test_ios_user_view(self): set_module_args(dict(name='ansible', view='test'))