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 'defaults' option in the nxos_config module #51076

Merged
merged 2 commits into from
Jan 23, 2019
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
10 changes: 5 additions & 5 deletions lib/ansible/modules/network/nxos/nxos_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,12 @@
from ansible.module_utils.network.common.utils import to_list


def get_running_config(module, config=None):
def get_running_config(module, config=None, flags=None):
contents = module.params['running_config']
if not contents:
if not module.params['defaults'] and config:
if config:
contents = config
else:
flags = ['all']
contents = get_config(module, flags=flags)
return contents

Expand Down Expand Up @@ -395,13 +394,14 @@ def main():
path = module.params['parents']
connection = get_connection(module)
contents = None
flags = ['all'] if module.params['defaults'] else []
replace_src = module.params['replace_src']
if replace_src:
if module.params['replace'] != 'config':
module.fail_json(msg='replace: config is required with replace_src')

if module.params['backup'] or (module._diff and module.params['diff_against'] == 'running'):
contents = get_config(module)
contents = get_config(module, flags=flags)
config = NetworkConfig(indent=2, contents=contents)
if module.params['backup']:
result['__backup__'] = contents
Expand All @@ -412,7 +412,7 @@ def main():

commit = not module.check_mode
candidate = get_candidate(module)
running = get_running_config(module, contents)
running = get_running_config(module, contents, flags=flags)
if replace_src:
commands = candidate.split('\n')
result['commands'] = result['updates'] = commands
Expand Down
24 changes: 24 additions & 0 deletions test/units/modules/network/nxos/test_nxos_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,27 @@ def test_nxos_config_save_changed_false(self):
self.assertEqual(self.save_config.call_count, 0)
self.assertEqual(self.get_config.call_count, 0)
self.assertEqual(self.load_config.call_count, 0)

def test_nxos_config_defaults_false(self):
set_module_args(dict(lines=['hostname localhost'], defaults=False))
result = self.execute_module(changed=True)
self.assertEqual(self.get_config.call_count, 1)
self.assertEqual(self.get_config.call_args[1], dict(flags=[]))

def test_nxos_config_defaults_true(self):
set_module_args(dict(lines=['hostname localhost'], defaults=True))
result = self.execute_module(changed=True)
self.assertEqual(self.get_config.call_count, 1)
self.assertEqual(self.get_config.call_args[1], dict(flags=['all']))

def test_nxos_config_defaults_false_backup_true(self):
set_module_args(dict(lines=['hostname localhost'], defaults=False, backup=True))
result = self.execute_module(changed=True)
self.assertEqual(self.get_config.call_count, 1)
self.assertEqual(self.get_config.call_args[1], dict(flags=[]))

def test_nxos_config_defaults_true_backup_true(self):
set_module_args(dict(lines=['hostname localhost'], defaults=True, backup=True))
result = self.execute_module(changed=True)
self.assertEqual(self.get_config.call_count, 1)
self.assertEqual(self.get_config.call_args[1], dict(flags=['all']))