Пример #1
0
    def get_diff(self,
                 candidate=None,
                 running=None,
                 diff_match='line',
                 diff_ignore_lines=None,
                 path=None,
                 diff_replace='line'):
        diff = {}

        # prepare candidate configuration
        candidate_obj = ImishConfig(indent=1)
        candidate_obj.load(candidate)

        if running and diff_match != 'none' and diff_replace != 'config':
            # running configuration
            running_obj = ImishConfig(indent=1,
                                      contents=running,
                                      ignore_lines=diff_ignore_lines)
            configdiffobjs = candidate_obj.difference(running_obj,
                                                      path=path,
                                                      match=diff_match,
                                                      replace=diff_replace)
        else:
            configdiffobjs = candidate_obj.items

        diff['config_diff'] = dumps(configdiffobjs,
                                    'commands') if configdiffobjs else ''
        return diff
Пример #2
0
    def get_candidate(self):
        candidate = ''
        if self.want.src:
            candidate = self.want.src

        elif self.want.lines:
            candidate_obj = ImishConfig(indent=1)
            parents = self.want.parents or list()
            if self.want.allow_duplicates:
                candidate_obj.add(self.want.lines,
                                  parents=parents,
                                  duplicates=True)
            else:
                candidate_obj.add(self.want.lines, parents=parents)
            candidate = dumps(candidate_obj, 'raw')
        return candidate
Пример #3
0
    def present(self):
        result = dict(changed=False)
        config = None
        contents = None

        if self.want.backup or (self.module._diff
                                and self.want.diff_against == 'running'):
            contents = self.read_current_from_device()
            config = ImishConfig(indent=1, contents=contents)
            if self.want.backup:
                # The backup file is created in the bigip_imish_config action plugin. Refer
                # to that if you have questions. The key below is removed by the action plugin.
                result['__backup__'] = contents

        if any((self.want.src, self.want.lines)):
            match = self.want.match
            replace = self.want.replace

            candidate = self.get_candidate()
            running = self.get_running_config(contents)

            response = self.get_diff(
                candidate=candidate,
                running=running,
                diff_match=match,
                diff_ignore_lines=self.want.diff_ignore_lines,
                path=self.want.parents,
                diff_replace=replace)

            config_diff = response['config_diff']

            if config_diff:
                commands = config_diff.split('\n')

                if self.want.before:
                    commands[:0] = self.want.before

                if self.want.after:
                    commands.extend(self.want.after)

                result['commands'] = commands
                result['updates'] = commands

                if not self.module.check_mode:
                    self.load_config(commands)

                result['changed'] = True

        running_config = self.want.running_config
        startup_config = None

        if self.want.save_when == 'always':
            self.save_config(result)
        elif self.want.save_when == 'modified':
            output = self.execute_show_commands(
                ['show running-config', 'show startup-config'])

            running_config = ImishConfig(
                indent=1,
                contents=output[0],
                ignore_lines=self.want.diff_ignore_lines)
            startup_config = ImishConfig(
                indent=1,
                contents=output[1],
                ignore_lines=self.want.diff_ignore_lines)

            if running_config.sha1 != startup_config.sha1:
                self.save_config(result)
        elif self.want.save_when == 'changed' and result['changed']:
            self.save_on_device()

        if self.module._diff:
            if not running_config:
                output = self.execute_show_commands('show running-config')
                contents = output[0]
            else:
                contents = running_config

            # recreate the object in order to process diff_ignore_lines
            running_config = ImishConfig(
                indent=1,
                contents=contents,
                ignore_lines=self.want.diff_ignore_lines)

            if self.want.diff_against == 'running':
                if self.module.check_mode:
                    self.module.warn(
                        "unable to perform diff against running-config due to check mode"
                    )
                    contents = None
                else:
                    contents = config.config_text

            elif self.want.diff_against == 'startup':
                if not startup_config:
                    output = self.execute_show_commands('show startup-config')
                    contents = output[0]
                else:
                    contents = startup_config.config_text

            elif self.want.diff_against == 'intended':
                contents = self.want.intended_config

            if contents is not None:
                base_config = ImishConfig(
                    indent=1,
                    contents=contents,
                    ignore_lines=self.want.diff_ignore_lines)

                if running_config.sha1 != base_config.sha1:
                    if self.want.diff_against == 'intended':
                        before = running_config
                        after = base_config
                    elif self.want.diff_against in ('startup', 'running'):
                        before = base_config
                        after = running_config

                    result.update({
                        'changed': True,
                        'diff': {
                            'before': str(before),
                            'after': str(after)
                        }
                    })
        self.changes.update(result)
        return result['changed']