Fix rollback in junos_config (#31424)

* Fix rollback in junos_config

Fixes #30778

*  Call `load_configuration` with rollback id in case
   the id is given as input
*  Pass rollback id to `get_diff()` to fetch diff from device

* Fix unit test
pull/31558/head
Ganesh Nalawade 7 years ago committed by GitHub
parent 2ed46e04f4
commit 88da95bb77

@ -174,9 +174,9 @@ def locked_config(module):
unlock_configuration(module)
def get_diff(module):
def get_diff(module, rollback='0'):
reply = get_configuration(module, compare=True, format='text')
reply = get_configuration(module, compare=True, format='text', rollback=rollback)
# if warning is received from device diff is empty.
if isinstance(reply, list):
return None

@ -191,7 +191,7 @@ import json
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.junos import get_diff, load_config, get_configuration
from ansible.module_utils.junos import commit_configuration, discard_changes, locked_config
from ansible.module_utils.junos import junos_argument_spec
from ansible.module_utils.junos import junos_argument_spec, load_configuration
from ansible.module_utils.junos import check_args as junos_check_args
from ansible.module_utils.netconf import send_request
from ansible.module_utils.six import string_types
@ -227,8 +227,8 @@ def zeroize(ele):
return send_request(ele, Element('request-system-zeroize'))
def rollback(ele):
return get_diff(ele)
def rollback(ele, id='0'):
return get_diff(ele, id)
def guess_format(config):
@ -346,9 +346,16 @@ def main():
result['__backup__'] = match.text.strip()
if module.params['rollback']:
rollback_id = module.params['rollback']
if rollback_id:
diff = rollback(module, rollback_id)
if commit:
diff = rollback(module)
kwargs = {
'comment': module.params['comment']
}
with locked_config(module):
load_configuration(module, rollback=rollback_id)
commit_configuration(module, **kwargs)
if module._diff:
result['diff'] = {'prepared': diff}
result['changed'] = True

@ -41,6 +41,37 @@
that:
- "result.changed == true"
- name: teardown for rollback test
junos_config:
lines:
- delete system syslog file test1
provider: "{{ netconf }}"
- name: Configure syslog file
junos_config:
lines:
- set system syslog file test1 any any
provider: "{{ netconf }}"
register: result
- assert:
that:
- "result.changed == true"
- result.diff.prepared | search("\+ *file test1")
- result.diff.prepared | search("\+ *any any")
- name: Rollback junos config
junos_config:
rollback: 1
provider: "{{ netconf }}"
register: result
- assert:
that:
- "result.changed == true"
- result.diff.prepared | search("\+ *file test1")
- result.diff.prepared | search("\+ *any any")
- name: teardown
junos_config:
lines:

@ -36,6 +36,9 @@ class TestJunosConfigModule(TestJunosModule):
self.mock_load_config = patch('ansible.modules.network.junos.junos_config.load_config')
self.load_config = self.mock_load_config.start()
self.mock_load_configuration = patch('ansible.modules.network.junos.junos_config.load_configuration')
self.load_configuration = self.mock_load_configuration.start()
self.mock_lock_configuration = patch('ansible.module_utils.junos.lock_configuration')
self.lock_configuration = self.mock_lock_configuration.start()
@ -59,6 +62,7 @@ class TestJunosConfigModule(TestJunosModule):
self.mock_commit_configuration.stop()
self.mock_get_diff.stop()
self.mock_send_request.stop()
self.load_configuration.stop()
def load_fixtures(self, commands=None, format='text', changed=False):
self.get_config.return_value = load_fixture('get_configuration_rpc_reply.txt')
@ -101,9 +105,14 @@ class TestJunosConfigModule(TestJunosModule):
self.assertEqual(kwargs['confirm_timeout'], 40)
def test_junos_config_rollback(self):
set_module_args(dict(rollback=10))
rollback = 10
set_module_args(dict(rollback=rollback))
self.execute_module(changed=True)
self.assertEqual(self.get_diff.call_count, 1)
self.assertEqual(self.load_configuration.call_count, 1)
self.assertEqual(self.commit_configuration.call_count, 1)
load_configuration_args = self.load_configuration.call_args
self.assertEqual(rollback, load_configuration_args[1].get('rollback'))
def test_junos_config_src_text(self):
src = load_fixture('junos_config.text', content='str')

Loading…
Cancel
Save