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')
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
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
    def execute_module(self):
        """ Execute the module

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

        existing_interfaces_facts = self.get_interfaces_facts()
        req, cmd = self.set_config(existing_interfaces_facts)
        requests.extend(req)
        commands.extend(cmd)

        if requests:
            if not self._module.check_mode:
                send_requests(self._module, requests=requests)
            result['changed'] = True
        result['requests'] = requests

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

        changed_interfaces_facts = self.get_interfaces_facts()

        result['before'] = existing_interfaces_facts
        if result['changed']:
            result['after'] = changed_interfaces_facts

        result['warnings'] = warnings
        return result
Ejemplo n.º 5
0
    def render_config(self, spec, conf):
        """
        Render config as dictionary structure and delete keys
          from spec for null values

        :param spec: The facts tree, generated from the argspec
        :param conf: The configuration
        :rtype: dictionary
        :returns: The generated config
        """
        config = deepcopy(spec)
        if conf["config"]["type"] == "ethernetCsmacd" and conf["state"][
                "oper-status"] == "DOWN":
            config["name"] = conf["name"]
            config["description"] = conf["config"]["description"]
            config["enabled"] = conf["config"]["enabled"]
            dic = [{
                "command":
                'debug cfgmgr show next vlan.show_ports_info_detail portList='
                + conf["name"],
                "output":
                "json"
            }]
            conf_json = run_commands(self._module, commands=dic)
            config["jumbo_frames"]["enabled"] = True if conf_json[0]["data"][
                0]["isJumboEnabled"] == "1" else False
            if conf_json[0]["data"][0]["duplexSpeedCfg"] == "1":
                config["duplex"] = "FULL"
            elif conf_json[0]["data"][0]["duplexSpeedCfg"] == "2":
                config["duplex"] = "AUTO"
            else:
                config["duplex"] = "HALF"
            if conf_json[0]["data"][0]["portSpeedCfg"] in self.PORT_SPEED.keys(
            ):
                config["speed"] = self.PORT_SPEED[conf_json[0]["data"][0]
                                                  ["portSpeedCfg"]]
            else:
                config["speed"] = "SPEED_UNKNOWN"
        return utils.remove_empties(config)
 def run(self, cmd):
     return run_commands(self.module, cmd)
 def populate(self):
     self.responses = run_commands(self.module, self.COMMANDS)