mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-25 11:51:26 -07:00
Fix rollback option in cli_config module (#44834)
* Fix rollback option in cli_config module * Update rollback flag in cliconf plugins * Add rollback api for junos cliconf plugin * Update doc * Update doc
This commit is contained in:
parent
a5b02d6c7c
commit
eb9c75caad
6 changed files with 67 additions and 13 deletions
|
@ -25,9 +25,9 @@ description:
|
||||||
options:
|
options:
|
||||||
config:
|
config:
|
||||||
description:
|
description:
|
||||||
- The config to be pushed to the network device. This is a
|
- The config to be pushed to the network device. This argument
|
||||||
required argument.
|
is mutually exclusive with C(rollback) and either one of the
|
||||||
required: true
|
option should be given as input.
|
||||||
type: 'str'
|
type: 'str'
|
||||||
commit:
|
commit:
|
||||||
description:
|
description:
|
||||||
|
@ -51,6 +51,7 @@ options:
|
||||||
argument. If the specified rollback identifier does not
|
argument. If the specified rollback identifier does not
|
||||||
exist on the remote device, the module will fail. To rollback
|
exist on the remote device, the module will fail. To rollback
|
||||||
to the most recent commit, set the C(rollback) argument to 0.
|
to the most recent commit, set the C(rollback) argument to 0.
|
||||||
|
This option is mutually exclusive with C(config).
|
||||||
commit_comment:
|
commit_comment:
|
||||||
description:
|
description:
|
||||||
- The C(commit_comment) argument specifies a text string to be used
|
- The C(commit_comment) argument specifies a text string to be used
|
||||||
|
@ -157,7 +158,7 @@ def validate_args(module, capabilities):
|
||||||
not capabilities['device_operations']['supports_replace']):
|
not capabilities['device_operations']['supports_replace']):
|
||||||
module.fail_json(msg='replace is not supported on this platform')
|
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']):
|
not capabilities['device_operations']['supports_rollback']):
|
||||||
module.fail_json(msg='rollback is not supported on this platform')
|
module.fail_json(msg='rollback is not supported on this platform')
|
||||||
|
|
||||||
|
@ -193,7 +194,7 @@ def run(module, capabilities, connection, candidate, running):
|
||||||
banner_diff = {}
|
banner_diff = {}
|
||||||
|
|
||||||
replace = module.params['replace']
|
replace = module.params['replace']
|
||||||
rollback = module.params['rollback']
|
rollback_id = module.params['rollback']
|
||||||
commit_comment = module.params['commit_comment']
|
commit_comment = module.params['commit_comment']
|
||||||
multiline_delimiter = module.params['multiline_delimiter']
|
multiline_delimiter = module.params['multiline_delimiter']
|
||||||
diff_replace = module.params['diff_replace']
|
diff_replace = module.params['diff_replace']
|
||||||
|
@ -207,7 +208,12 @@ def run(module, capabilities, connection, candidate, running):
|
||||||
elif replace in ('no', 'false', 'False'):
|
elif replace in ('no', 'false', 'False'):
|
||||||
replace = 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:
|
if diff_replace:
|
||||||
module.warn('diff_replace is ignored as the device supports onbox diff')
|
module.warn('diff_replace is ignored as the device supports onbox diff')
|
||||||
if diff_match:
|
if diff_match:
|
||||||
|
@ -280,7 +286,7 @@ def main():
|
||||||
"""main entry point for execution
|
"""main entry point for execution
|
||||||
"""
|
"""
|
||||||
argument_spec = dict(
|
argument_spec = dict(
|
||||||
config=dict(required=True, type='str'),
|
config=dict(type='str'),
|
||||||
commit=dict(type='bool'),
|
commit=dict(type='bool'),
|
||||||
replace=dict(type='str'),
|
replace=dict(type='str'),
|
||||||
rollback=dict(type='int'),
|
rollback=dict(type='int'),
|
||||||
|
@ -292,7 +298,12 @@ def main():
|
||||||
diff_ignore_lines=dict(type='list')
|
diff_ignore_lines=dict(type='list')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
mutually_exclusive = [('config', 'rollback')]
|
||||||
|
required_one_of = [['config', 'rollback']]
|
||||||
|
|
||||||
module = AnsibleModule(argument_spec=argument_spec,
|
module = AnsibleModule(argument_spec=argument_spec,
|
||||||
|
mutually_exclusive=mutually_exclusive,
|
||||||
|
required_one_of=required_one_of,
|
||||||
supports_check_mode=True)
|
supports_check_mode=True)
|
||||||
|
|
||||||
result = {'changed': False}
|
result = {'changed': False}
|
||||||
|
|
|
@ -307,6 +307,15 @@ class CliconfBase(AnsiblePlugin):
|
||||||
"""
|
"""
|
||||||
return self._connection.method_not_found("discard_changes is not supported by network_os %s" % self._play_context.network_os)
|
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):
|
def copy_file(self, source=None, destination=None, proto='scp', timeout=30):
|
||||||
"""Copies file over scp/sftp to remote device
|
"""Copies file over scp/sftp to remote device
|
||||||
|
|
||||||
|
|
|
@ -268,7 +268,7 @@ class Cliconf(CliconfBase):
|
||||||
return {
|
return {
|
||||||
'supports_diff_replace': True,
|
'supports_diff_replace': True,
|
||||||
'supports_commit': True if self.supports_sessions else False,
|
'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_defaults': False,
|
||||||
'supports_onbox_diff': True if self.supports_sessions else False,
|
'supports_onbox_diff': True if self.supports_sessions else False,
|
||||||
'supports_commit_comment': False,
|
'supports_commit_comment': False,
|
||||||
|
|
|
@ -188,7 +188,7 @@ class Cliconf(CliconfBase):
|
||||||
return {
|
return {
|
||||||
'supports_diff_replace': False,
|
'supports_diff_replace': False,
|
||||||
'supports_commit': True,
|
'supports_commit': True,
|
||||||
'supports_rollback': True,
|
'supports_rollback': False,
|
||||||
'supports_defaults': False,
|
'supports_defaults': False,
|
||||||
'supports_onbox_diff': True,
|
'supports_onbox_diff': True,
|
||||||
'supports_commit_comment': True,
|
'supports_commit_comment': True,
|
||||||
|
|
|
@ -174,6 +174,17 @@ class Cliconf(CliconfBase):
|
||||||
|
|
||||||
return resp
|
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):
|
def get_diff(self, rollback_id=None):
|
||||||
diff = {'config_diff': None}
|
diff = {'config_diff': None}
|
||||||
response = self.compare_configuration(rollback_id=rollback_id)
|
response = self.compare_configuration(rollback_id=rollback_id)
|
||||||
|
|
|
@ -2,12 +2,17 @@
|
||||||
- debug: msg="START cli_config/cli_basic.yaml on connection={{ ansible_connection }}"
|
- debug: msg="START cli_config/cli_basic.yaml on connection={{ ansible_connection }}"
|
||||||
|
|
||||||
- name: setup
|
- name: setup
|
||||||
cli_config: &rm
|
cli_config: &rm1
|
||||||
config: delete interfaces ge-0/0/1
|
config: delete interfaces ge-0/0/1
|
||||||
become: yes
|
become: yes
|
||||||
|
|
||||||
|
- name: setup
|
||||||
|
cli_config: &rm2
|
||||||
|
config: delete interfaces ge-0/0/2
|
||||||
|
become: yes
|
||||||
|
|
||||||
- name: configure device with config
|
- name: configure device with config
|
||||||
cli_config: &conf
|
cli_config: &conf1
|
||||||
config: set interfaces ge-0/0/1 description 'test-interface'
|
config: set interfaces ge-0/0/1 description 'test-interface'
|
||||||
register: result
|
register: result
|
||||||
|
|
||||||
|
@ -16,14 +21,32 @@
|
||||||
- "result.changed == true"
|
- "result.changed == true"
|
||||||
|
|
||||||
- name: Idempotence
|
- name: Idempotence
|
||||||
cli_config: *conf
|
cli_config: *conf1
|
||||||
register: result
|
register: result
|
||||||
|
|
||||||
- assert:
|
- assert:
|
||||||
that:
|
that:
|
||||||
- "result.changed == false"
|
- "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
|
- name: teardown
|
||||||
cli_config: *rm
|
cli_config: *rm1
|
||||||
|
|
||||||
|
- name: teardown
|
||||||
|
cli_config: *rm2
|
||||||
|
|
||||||
- debug: msg="END cli_config/cli_basic.yaml on connection={{ ansible_connection }}"
|
- debug: msg="END cli_config/cli_basic.yaml on connection={{ ansible_connection }}"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue