def execute_module(self):
        """ Execute the module

        :rtype: A dictionary
        :returns: The result from module execution
        """
        result = {'changed': False}
        warnings = list()
        commands = list()

        existing_auto_save_cfg_facts = self.get_auto_save_cfg_facts()
        commands.extend(self.set_config(existing_auto_save_cfg_facts))
        if commands:
            if not self._module.check_mode:
                run_commands(self._module, commands)
            result['changed'] = True
        result['commands'] = commands

        changed_auto_save_cfg_facts = self.get_auto_save_cfg_facts()

        result['before'] = existing_auto_save_cfg_facts
        if result['changed']:
            result['after'] = changed_auto_save_cfg_facts

        result['warnings'] = warnings
        return result
Exemplo n.º 2
0
def get_startup_config_text(module):
    reply = run_commands(module, ['show switch | include "Config Selected"'])
    match = re.search(r': +(\S+)\.cfg', to_text(reply, errors='surrogate_or_strict').strip())
    if match:
        cfgname = match.group(1).strip()
        reply = run_commands(module, ['debug cfgmgr show configuration file ' + cfgname])
        data = reply[0]
    else:
        data = ''
    return data
Exemplo n.º 3
0
def save_config(module, result):
    result['changed'] = True
    if not module.check_mode:
        command = {"command": "save configuration",
                   "prompt": "Do you want to save configuration", "answer": "y"}
        run_commands(module, command)
    else:
        module.warn('Skipping command `save configuration` '
                    'due to check_mode.  Configuration not copied to '
                    'non-volatile storage')
Exemplo n.º 4
0
def get_startup_config(module, flags=None):
    reply = run_commands(module, {'command': 'show switch', 'output': 'text'})
    match = re.search(r'Config Selected: +(\S+)\.cfg',
                      to_text(reply, errors='surrogate_or_strict').strip(),
                      re.MULTILINE)
    if match:
        cfgname = match.group(1).strip()
        command = ' '.join(['debug cfgmgr show configuration file', cfgname])
        if flags:
            command += ' '.join(to_list(flags)).strip()
        reply = run_commands(module, {'command': command, 'output': 'text'})
        data = reply[0]
    else:
        data = ''
    return data
Exemplo n.º 5
0
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for auto_save_cfg
        :param connection: the device connection
        :param ansible_facts: Facts dictionary
        :param data: previously collected conf
        :rtype: dictionary
        :returns: facts
        """
        if not data:
            # typically data is populated from the current device configuration
            # data = connection.get('show running-config | section ^interface')
            # using mock data instead
            data = run_commands(self._module,
                                commands={
                                    "command":
                                    "debug cfgmgr show one cfgmgr.autoSaveCfg",
                                    "output": "json"
                                })

        obj = {}
        if data:
            cfg_obj = self.render_config(self.generated_spec,
                                         data[0]['data'][0])
            if cfg_obj:
                obj = cfg_obj

        ansible_facts['ansible_network_resources'].pop('auto_save_cfg', None)
        facts = {}

        params = utils.validate_config(self.argument_spec, {'config': obj})
        facts['auto_save_cfg'] = params['config']

        ansible_facts['ansible_network_resources'].update(facts)
        return ansible_facts
Exemplo n.º 6
0
def main():
    """main entry point for module execution
    """
    argument_spec = dict(
        commands=dict(type='list', required=True),

        wait_for=dict(type='list'),
        match=dict(default='all', choices=['all', 'any']),

        retries=dict(default=10, type='int'),
        interval=dict(default=1, type='int')
    )

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

    result = {'changed': False}

    warnings = list()
    commands = parse_commands(module, warnings)
    result['warnings'] = warnings

    wait_for = module.params['wait_for'] or list()
    conditionals = [Conditional(c) for c in wait_for]

    retries = module.params['retries']
    interval = module.params['interval']
    match = module.params['match']

    while retries > 0:
        responses = run_commands(module, commands)

        for item in list(conditionals):
            if item(responses):
                if match == 'any':
                    conditionals = list()
                    break
                conditionals.remove(item)

        if not conditionals:
            break

        time.sleep(interval)
        retries -= 1

    if conditionals:
        failed_conditions = [item.raw for item in conditionals]
        msg = 'One or more conditional statements have not be satisfied'
        module.fail_json(msg=msg, failed_conditions=failed_conditions)

    result.update({
        'changed': False,
        'stdout': responses,
        'stdout_lines': list(to_lines(responses))
    })

    module.exit_json(**result)
Exemplo n.º 7
0
 def run(self, cmd):
     return run_commands(self.module, cmd)
Exemplo n.º 8
0
 def populate(self):
     self.responses = run_commands(self.module, self.COMMANDS)