diff options
author | Ganesh Nalawade <ganesh634@gmail.com> | 2017-10-11 15:28:42 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-11 15:28:42 +0530 |
commit | e001fb7c149cd2784d0a3e54fe79e6d1b97ad919 (patch) | |
tree | 00edaace2e7ef6618046dcbed62a4a0918d59835 | |
parent | daa2810410c530d69e221786592d51bdb80e5bfc (diff) | |
download | ansible-e001fb7c149cd2784d0a3e54fe79e6d1b97ad919.tar.gz |
Fix rollback in junos_config (#31424) (#31563)
* 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
(cherry picked from commit 88da95bb77cf20f56035e7ba5d0230c0a2785f2b)
* Update changelog
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/ansible/module_utils/junos.py | 4 | ||||
-rw-r--r-- | lib/ansible/modules/network/junos/junos_config.py | 17 | ||||
-rw-r--r-- | test/integration/targets/junos_config/tests/netconf/single.yaml | 31 | ||||
-rw-r--r-- | test/units/modules/network/junos/test_junos_config.py | 11 |
5 files changed, 56 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index fbb8cb7b11..253c7cbb82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -123,6 +123,7 @@ Ansible Changes By Release * Fix for aws_s3 metadata to use the correct parameters when uploading a file (https://github.com/ansible/ansible/issues/31232) * Fix for the yum module when installing from file/url crashes (https://github.com/ansible/ansible/pull/31529) * Improved error messaging for Windows become/runas when username is bogus (https://github.com/ansible/ansible/pull/31551) +* Fix rollback feature in junos_config to now allow configuration rollback on device (https://github.com/ansible/ansible/pull/31424) <a id="2.4"></a> diff --git a/lib/ansible/module_utils/junos.py b/lib/ansible/module_utils/junos.py index ac571c2d99..86911677a4 100644 --- a/lib/ansible/module_utils/junos.py +++ b/lib/ansible/module_utils/junos.py @@ -178,9 +178,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 diff --git a/lib/ansible/modules/network/junos/junos_config.py b/lib/ansible/modules/network/junos/junos_config.py index f5c3399cfb..c853bfa7ae 100644 --- a/lib/ansible/modules/network/junos/junos_config.py +++ b/lib/ansible/modules/network/junos/junos_config.py @@ -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 diff --git a/test/integration/targets/junos_config/tests/netconf/single.yaml b/test/integration/targets/junos_config/tests/netconf/single.yaml index a222deb0ee..9ea90778bd 100644 --- a/test/integration/targets/junos_config/tests/netconf/single.yaml +++ b/test/integration/targets/junos_config/tests/netconf/single.yaml @@ -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: diff --git a/test/units/modules/network/junos/test_junos_config.py b/test/units/modules/network/junos/test_junos_config.py index f06889714d..ece27ec1d8 100644 --- a/test/units/modules/network/junos/test_junos_config.py +++ b/test/units/modules/network/junos/test_junos_config.py @@ -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') |