diff --git a/changelogs/fragments/59786-ce_netstream_global-to-fix-bugs.yml b/changelogs/fragments/59786-ce_netstream_global-to-fix-bugs.yml new file mode 100644 index 00000000000..491f7c7c5d9 --- /dev/null +++ b/changelogs/fragments/59786-ce_netstream_global-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/59689) diff --git a/lib/ansible/modules/network/cloudengine/ce_netstream_global.py b/lib/ansible/modules/network/cloudengine/ce_netstream_global.py index f8ad7e57494..a11257fd685 100644 --- a/lib/ansible/modules/network/cloudengine/ce_netstream_global.py +++ b/lib/ansible/modules/network/cloudengine/ce_netstream_global.py @@ -213,7 +213,8 @@ changed: ''' 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 @@ -247,6 +248,38 @@ def get_interface_type(interface): return iftype.lower() +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 NetStreamGlobal(object): """ Manages netstream global parameters. @@ -342,8 +375,8 @@ class NetStreamGlobal(object): self.existing["sampler"].append(sampler_tmp) if self.interface != "all": flags = list() - exp = " | ignore-case section include ^interface %s$" \ - " | include netstream sampler random-packets" % self.interface + exp = r" | ignore-case section include ^#\s+interface %s" \ + r" | include netstream sampler random-packets" % self.interface flags.append(exp) config = get_config(self.module, flags) if not config: @@ -376,8 +409,8 @@ class NetStreamGlobal(object): statistic_tmp1["statistics_record"] = list() statistic_tmp1["interface"] = self.interface flags = list() - exp = " | ignore-case section include ^interface %s$" \ - " | include netstream record"\ + exp = r" | ignore-case section include ^#\s+interface %s" \ + r" | include netstream record"\ % (self.interface) flags.append(exp) config = get_config(self.module, flags) @@ -414,8 +447,8 @@ class NetStreamGlobal(object): statistic_tmp1 = dict() statistic_tmp1["statistics_direction"] = list() flags = list() - exp = " | ignore-case section include ^interface %s$" \ - " | include netstream inbound|outbound"\ + exp = r" | ignore-case section include ^#\s+interface %s" \ + r" | include netstream inbound|outbound"\ % self.interface flags.append(exp) config = get_config(self.module, flags) @@ -501,8 +534,8 @@ class NetStreamGlobal(object): self.end_state["sampler"].append(sampler_tmp) if self.interface != "all": flags = list() - exp = " | ignore-case section include ^interface %s$" \ - " | include netstream sampler random-packets" % self.interface + exp = r" | ignore-case section include ^#\s+interface %s" \ + r" | include netstream sampler random-packets" % self.interface flags.append(exp) config = get_config(self.module, flags) if not config: @@ -535,8 +568,8 @@ class NetStreamGlobal(object): statistic_tmp1["statistics_record"] = list() statistic_tmp1["interface"] = self.interface flags = list() - exp = " | ignore-case section include ^interface %s$" \ - " | include netstream record"\ + exp = r" | ignore-case section include ^#\s+interface %s" \ + r" | include netstream record"\ % (self.interface) flags.append(exp) config = get_config(self.module, flags) @@ -573,8 +606,8 @@ class NetStreamGlobal(object): statistic_tmp1 = dict() statistic_tmp1["statistics_direction"] = list() flags = list() - exp = " | ignore-case section include ^interface %s$" \ - " | include netstream inbound|outbound"\ + exp = r" | ignore-case section include ^#\s+interface %s" \ + r" | include netstream inbound|outbound"\ % self.interface flags.append(exp) config = get_config(self.module, flags)