From 777395ec1873b8dcb5412b3df08d24c3d4f399b5 Mon Sep 17 00:00:00 2001 From: Xu Yuandong <786018072@qq.com> Date: Thu, 8 Aug 2019 03:16:32 +0800 Subject: [PATCH] Backport/2.8/59690 ce_netstream_template: update to fix a bug. (#59690) (#60033) * ce_netstream_template: update to fix a bug. (#59690) * update to fix a bug. * Update ce_netstream_template.py * Update ce_netstream_template.py * Update ce_netstream_template.py (cherry picked from commit adfbd04b3ab611a9807a36c7d4ab648d53a811be) * add a changelog fragment. --- ...0033-ce_netstream_template-to-fix-bugs.yml | 2 + .../cloudengine/ce_netstream_template.py | 40 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/60033-ce_netstream_template-to-fix-bugs.yml diff --git a/changelogs/fragments/60033-ce_netstream_template-to-fix-bugs.yml b/changelogs/fragments/60033-ce_netstream_template-to-fix-bugs.yml new file mode 100644 index 00000000000..906c76d6fa5 --- /dev/null +++ b/changelogs/fragments/60033-ce_netstream_template-to-fix-bugs.yml @@ -0,0 +1,2 @@ +bugfixes: +- ce_netstream_global - The 'get_config', which is from 'ansible.module_utils.network.cloudengine.ce', try to return the result from cache,however the configure has changed. (https://github.com/ansible/ansible/pull/59690) diff --git a/lib/ansible/modules/network/cloudengine/ce_netstream_template.py b/lib/ansible/modules/network/cloudengine/ce_netstream_template.py index c5928115497..2254b25d1f1 100644 --- a/lib/ansible/modules/network/cloudengine/ce_netstream_template.py +++ b/lib/ansible/modules/network/cloudengine/ce_netstream_template.py @@ -138,10 +138,43 @@ updates: import re from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.network.cloudengine.ce import get_config, load_config +from ansible.module_utils.network.cloudengine.ce import load_config +from ansible.module_utils.network.cloudengine.ce import get_connection, rm_config_prefix from ansible.module_utils.network.cloudengine.ce import ce_argument_spec +def get_config(module, flags): + + """Retrieves the current config from the device or cache + """ + flags = [] if flags is None else flags + if isinstance(flags, str): + flags = [flags] + elif not isinstance(flags, list): + flags = [] + + cmd = 'display current-configuration ' + cmd += ' '.join(flags) + cmd = cmd.strip() + conn = get_connection(module) + rc, out, err = conn.exec_command(cmd) + if rc != 0: + module.fail_json(msg=err) + cfg = str(out).strip() + # remove default configuration prefix '~' + for flag in flags: + if "include-default" in flag: + cfg = rm_config_prefix(cfg) + break + if cfg.startswith('display'): + lines = cfg.split('\n') + if len(lines) > 1: + return '\n'.join(lines[1:]) + else: + return '' + return cfg + + class NetstreamTemplate(object): """ Manages netstream template configuration """ @@ -291,6 +324,9 @@ class NetstreamTemplate(object): tmp_value = re.findall(r'collect interface (.*)', self.netstream_cfg) if tmp_value: self.end_state["collect_interface"] = tmp_value + if self.end_state == self.existing: + self.changed = False + self.updates_cmd = list() def present_netstream(self): """ Present netstream configuration """ @@ -309,7 +345,7 @@ class NetstreamTemplate(object): need_create_record = True if self.description: - cmd = "description %s" % self.description + cmd = "description %s" % self.description.strip() if not self.netstream_cfg or cmd not in self.netstream_cfg: cmds.append(cmd) self.updates_cmd.append(cmd)