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 rollback option in cli_config module #44834

Merged
merged 3 commits into from
Aug 29, 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
25 changes: 18 additions & 7 deletions lib/ansible/modules/network/cli/cli_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
options:
config:
description:
- The config to be pushed to the network device. This is a
required argument.
required: true
- The config to be pushed to the network device. This argument
is mutually exclusive with C(rollback) and either one of the
option should be given as input.
type: 'str'
commit:
description:
Expand All @@ -51,6 +51,7 @@
argument. If the specified rollback identifier does not
exist on the remote device, the module will fail. To rollback
to the most recent commit, set the C(rollback) argument to 0.
This option is mutually exclusive with C(config).
commit_comment:
description:
- The C(commit_comment) argument specifies a text string to be used
Expand Down Expand Up @@ -157,7 +158,7 @@ def validate_args(module, capabilities):
not capabilities['device_operations']['supports_replace']):
module.fail_json(msg='replace is not supported on this platform')

if (module.params['rollback'] and
if (module.params['rollback'] is not None and
not capabilities['device_operations']['supports_rollback']):
module.fail_json(msg='rollback is not supported on this platform')

Expand Down Expand Up @@ -193,7 +194,7 @@ def run(module, capabilities, connection, candidate, running):
banner_diff = {}

replace = module.params['replace']
rollback = module.params['rollback']
rollback_id = module.params['rollback']
commit_comment = module.params['commit_comment']
multiline_delimiter = module.params['multiline_delimiter']
diff_replace = module.params['diff_replace']
Expand All @@ -207,7 +208,12 @@ def run(module, capabilities, connection, candidate, running):
elif replace in ('no', 'false', 'False'):
replace = False

if capabilities['device_operations']['supports_onbox_diff']:
if rollback_id is not None:
resp = connection.rollback(rollback_id, commit)
if 'diff' in resp:
result['changed'] = True

elif capabilities['device_operations']['supports_onbox_diff']:
if diff_replace:
module.warn('diff_replace is ignored as the device supports onbox diff')
if diff_match:
Expand Down Expand Up @@ -280,7 +286,7 @@ def main():
"""main entry point for execution
"""
argument_spec = dict(
config=dict(required=True, type='str'),
config=dict(type='str'),
commit=dict(type='bool'),
replace=dict(type='str'),
rollback=dict(type='int'),
Expand All @@ -292,7 +298,12 @@ def main():
diff_ignore_lines=dict(type='list')
)

mutually_exclusive = [('config', 'rollback')]
required_one_of = [['config', 'rollback']]

module = AnsibleModule(argument_spec=argument_spec,
mutually_exclusive=mutually_exclusive,
required_one_of=required_one_of,
supports_check_mode=True)

result = {'changed': False}
Expand Down
9 changes: 9 additions & 0 deletions lib/ansible/plugins/cliconf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,15 @@ def discard_changes(self):
"""
return self._connection.method_not_found("discard_changes is not supported by network_os %s" % self._play_context.network_os)

def rollback(self, rollback_id, commit=True):
"""

:param rollback_id: The commit id to which configuration should be rollbacked
:param commit: Flag to indicate if changes should be committed or not
:return: Returns diff between before and after change.
"""
pass

def copy_file(self, source=None, destination=None, proto='scp', timeout=30):
"""Copies file over scp/sftp to remote device

Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/plugins/cliconf/eos.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def get_device_operations(self):
return {
'supports_diff_replace': True,
'supports_commit': True if self.supports_sessions else False,
'supports_rollback': True if self.supports_sessions else False,
'supports_rollback': False,
'supports_defaults': False,
'supports_onbox_diff': True if self.supports_sessions else False,
'supports_commit_comment': False,
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/plugins/cliconf/iosxr.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def get_device_operations(self):
return {
'supports_diff_replace': False,
'supports_commit': True,
'supports_rollback': True,
'supports_rollback': False,
'supports_defaults': False,
'supports_onbox_diff': True,
'supports_commit_comment': True,
Expand Down
11 changes: 11 additions & 0 deletions lib/ansible/plugins/cliconf/junos.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ def compare_configuration(self, rollback_id=None):

return resp

@configure
def rollback(self, rollback_id, commit=True):
resp = {}
self.send_command('rollback %s' % int(rollback_id))
resp['diff'] = self.compare_configuration()
if commit:
self.commit()
else:
self.discard_changes()
return resp

def get_diff(self, rollback_id=None):
diff = {'config_diff': None}
response = self.compare_configuration(rollback_id=rollback_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
- debug: msg="START cli_config/cli_basic.yaml on connection={{ ansible_connection }}"

- name: setup
cli_config: &rm
cli_config: &rm1
config: delete interfaces ge-0/0/1
become: yes

- name: setup
cli_config: &rm2
config: delete interfaces ge-0/0/2
become: yes

- name: configure device with config
cli_config: &conf
cli_config: &conf1
config: set interfaces ge-0/0/1 description 'test-interface'
register: result

Expand All @@ -16,14 +21,32 @@
- "result.changed == true"

- name: Idempotence
cli_config: *conf
cli_config: *conf1
register: result

- assert:
that:
- "result.changed == false"

- name: configure device with config
cli_config: &conf2
config: set interfaces ge-0/0/2 description 'test-interface'
register: result

- name: test rollabck
cli_config:
rollback: 1
register: result

- assert:
that:
- "result.changed == true"
- "'ge-0/0/2' in result.diff.prepared"

- name: teardown
cli_config: *rm1

- name: teardown
cli_config: *rm
cli_config: *rm2

- debug: msg="END cli_config/cli_basic.yaml on connection={{ ansible_connection }}"