Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ios_user issues #44904

Merged
merged 2 commits into from Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 11 additions & 16 deletions lib/ansible/modules/network/ios/ios_user.py
Expand Up @@ -178,7 +178,6 @@
from copy import deepcopy

import re
import json
import base64
import hashlib

Expand Down Expand Up @@ -234,20 +233,22 @@ def add(command, want, x):

def add_ssh(command, want, x=None):
command.append('ip ssh pubkey-chain')
command.append(' no username %s' % want['name'])
if x:
command.append(' username %s' % want['name'])
command.append(' key-hash %s' % x)
command.append(' exit')
command.append(' exit')
command.append('username %s' % want['name'])
command.append('key-hash %s' % x)
command.append('exit')
else:
command.append('no username %s' % want['name'])
command.append('exit')

for update in updates:
want, have = update

if want['state'] == 'absent':
commands.append(user_del_cmd(want['name']))
add_ssh(commands, want)
continue
if have['sshkey']:
add_ssh(commands, want)
else:
commands.append(user_del_cmd(want['name']))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is in else condition.
If this user have sshkey in settings, it will remove user's sshkey at first time, and remove user at second time?


if needs_update(want, have, 'view'):
add(commands, want, 'view %s' % want['view'])
Expand Down Expand Up @@ -292,7 +293,7 @@ def parse_privilege(data):
def map_config_to_obj(module):
data = get_config(module, flags=['| section username'])

match = re.findall(r'^username (\S+)', data, re.M)
match = re.findall(r'(?:^(?:u|\s{2}u))sername (\S+)', data, re.M)
if not match:
return list()

Expand Down Expand Up @@ -450,12 +451,6 @@ def main():

result['commands'] = commands

# the ios cli prevents this by rule so capture it and display
# a nice failure message
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:
load_config(module, commands)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/targets/ios_user/tests/cli/basic.yaml
Expand Up @@ -80,5 +80,5 @@
that:
- 'result.changed == true'
- '"no username ansibletest1" in result.commands[0]["command"]'
- '"no username ansibletest2" in result.commands[4]["command"]'
- '"no username ansibletest3" in result.commands[8]["command"]'
- '"no username ansibletest2" in result.commands[1]["command"]'
- '"no username ansibletest3" in result.commands[2]["command"]'
14 changes: 5 additions & 9 deletions test/units/modules/network/ios/test_ios_user.py
Expand Up @@ -61,10 +61,7 @@ def test_ios_user_delete(self):
{
"command": "no username ansible", "answer": "y", "newline": False,
"prompt": "This operation will remove all username related configurations with same name",
},
'ip ssh pubkey-chain',
' no username ansible',
' exit'
}
]

result_cmd = []
Expand Down Expand Up @@ -124,11 +121,10 @@ def test_ios_user_set_sshkey(self):
set_module_args(dict(name='ansible', sshkey='dGVzdA=='))
commands = [
'ip ssh pubkey-chain',
' no username ansible',
' username ansible',
' key-hash ssh-rsa 098F6BCD4621D373CADE4E832627B4F6',
' exit',
' exit'
'username ansible',
'key-hash ssh-rsa 098F6BCD4621D373CADE4E832627B4F6',
'exit',
'exit'
]
result = self.execute_module(changed=True, commands=commands)
self.assertEqual(result['commands'], commands)