def main():
    """main entry point for Ansible module"""
    argument_spec = dict(
        rpc=dict(required=True),
        args=dict(type="dict"),
        attrs=dict(type="dict"),
        output=dict(default="xml", choices=["xml", "json", "text"]),
    )

    argument_spec.update(junos_argument_spec)

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

    warnings = list()
    result = {"changed": False, "warnings": warnings}

    rpc = str(module.params["rpc"]).replace("_", "-")

    if all((module.check_mode, not rpc.startswith("get"))):
        module.fail_json(msg="invalid rpc for running in check_mode")

    args = module.params["args"] or {}
    attrs = module.params["attrs"] or {}

    xattrs = {"format": module.params["output"]}

    for key, value in iteritems(attrs):
        xattrs.update({key: value})

    element = Element(module.params["rpc"], xattrs)

    for key, value in iteritems(args):
        key = str(key).replace("_", "-")
        if isinstance(value, list):
            for item in value:
                child = SubElement(element, key)
                if item is not True:
                    child.text = item
        else:
            child = SubElement(element, key)
            if value is not True:
                child.text = value

    reply = exec_rpc(module, tostring(element), ignore_warning=False)

    result["xml"] = tostring(reply)

    if module.params["output"] == "text":
        data = reply.find(".//output")
        result["output"] = data.text.strip()
        result["output_lines"] = result["output"].split("\n")

    elif module.params["output"] == "json":
        result["output"] = module.from_json(reply.text.strip())

    else:
        result["output"] = tostring(reply).split("\n")

    module.exit_json(**result)
Exemple #2
0
    def populate(self):
        ele = Element('get-interface-information')
        SubElement(ele, 'detail')
        reply = exec_rpc(self.module, tostring(ele))

        interfaces = {}

        for item in reply[0]:
            name = self.get_text(item, 'name')
            obj = {
                'oper-status': self.get_text(item, 'oper-status'),
                'admin-status': self.get_text(item, 'admin-status'),
                'speed': self.get_text(item, 'speed'),
                'macaddress': self.get_text(item, 'hardware-physical-address'),
                'mtu': self.get_text(item, 'mtu'),
                'type': self.get_text(item, 'if-type'),
            }

            interfaces[name] = obj

        self.facts['interfaces'] = interfaces
Exemple #3
0
    def populate(self):
        ele = Element("get-interface-information")
        SubElement(ele, "detail")
        reply = exec_rpc(self.module, tostring(ele))

        interfaces = {}

        for item in reply[0]:
            name = self.get_text(item, "name")
            obj = {
                "oper-status": self.get_text(item, "oper-status"),
                "admin-status": self.get_text(item, "admin-status"),
                "speed": self.get_text(item, "speed"),
                "macaddress": self.get_text(item, "hardware-physical-address"),
                "mtu": self.get_text(item, "mtu"),
                "type": self.get_text(item, "if-type"),
            }

            interfaces[name] = obj

        self.facts["interfaces"] = interfaces
def zeroize(module):
    return exec_rpc(
        module,
        tostring(Element("request-system-zeroize")),
        ignore_warning=False,
    )
def main():
    """main entry point for Ansible module
    """
    argument_spec = dict(
        rpc=dict(required=True),
        args=dict(type='dict'),
        attrs=dict(type='dict'),
        output=dict(default='xml', choices=['xml', 'json', 'text']),
    )

    argument_spec.update(junos_argument_spec)

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

    warnings = list()
    result = {'changed': False, 'warnings': warnings}

    rpc = str(module.params['rpc']).replace('_', '-')

    if all((module.check_mode, not rpc.startswith('get'))):
        module.fail_json(msg='invalid rpc for running in check_mode')

    args = module.params['args'] or {}
    attrs = module.params['attrs'] or {}

    xattrs = {'format': module.params['output']}

    for key, value in iteritems(attrs):
        xattrs.update({key: value})

    element = Element(module.params['rpc'], xattrs)

    for key, value in iteritems(args):
        key = str(key).replace('_', '-')
        if isinstance(value, list):
            for item in value:
                child = SubElement(element, key)
                if item is not True:
                    child.text = item
        else:
            child = SubElement(element, key)
            if value is not True:
                child.text = value

    reply = exec_rpc(module, tostring(element), ignore_warning=False)

    result['xml'] = tostring(reply)

    if module.params['output'] == 'text':
        data = reply.find('.//output')
        result['output'] = data.text.strip()
        result['output_lines'] = result['output'].split('\n')

    elif module.params['output'] == 'json':
        result['output'] = module.from_json(reply.text.strip())

    else:
        result['output'] = tostring(reply).split('\n')

    module.exit_json(**result)
Exemple #6
0
def rpc(module, items):

    responses = list()
    for item in items:
        name = item["name"]
        xattrs = item["xattrs"]
        fetch_config = False

        args = item.get("args")
        text = item.get("text")

        name = str(name).replace("_", "-")

        if all((module.check_mode, not name.startswith("get"))):
            module.fail_json(msg="invalid rpc for running in check_mode")

        if (name == "command" and text.startswith("show configuration")
                or name == "get-configuration"):
            fetch_config = True

        element = Element(name, xattrs)

        if text:
            element.text = text

        elif args:
            for key, value in iteritems(args):
                key = str(key).replace("_", "-")
                if isinstance(value, list):
                    for item in value:
                        child = SubElement(element, key)
                        if item is not True:
                            child.text = item
                else:
                    child = SubElement(element, key)
                    if value is not True:
                        child.text = value

        if fetch_config:
            reply = get_configuration(module, format=xattrs["format"])
        else:
            reply = exec_rpc(module, tostring(element), ignore_warning=False)

        if xattrs["format"] == "text":
            if fetch_config:
                data = reply.find(".//configuration-text")
            else:
                data = reply.find(".//output")

            if data is None:
                module.fail_json(msg=tostring(reply))

            responses.append(data.text.strip())

        elif xattrs["format"] == "json":
            responses.append(module.from_json(reply.text.strip()))

        elif xattrs["format"] == "set":
            data = reply.find(".//configuration-set")
            if data is None:
                module.fail_json(
                    msg=
                    "Display format 'set' is not supported by remote device.")
            responses.append(data.text.strip())

        else:
            responses.append(tostring(reply))

    return responses
Exemple #7
0
 def rpc(self, rpc):
     return exec_rpc(self.module, tostring(Element(rpc)))
Exemple #8
0
def rpc(module, items):

    responses = list()
    for item in items:
        name = item['name']
        xattrs = item['xattrs']
        fetch_config = False

        args = item.get('args')
        text = item.get('text')

        name = str(name).replace('_', '-')

        if all((module.check_mode, not name.startswith('get'))):
            module.fail_json(msg='invalid rpc for running in check_mode')

        if name == 'command' and text.startswith(
                'show configuration') or name == 'get-configuration':
            fetch_config = True

        element = Element(name, xattrs)

        if text:
            element.text = text

        elif args:
            for key, value in iteritems(args):
                key = str(key).replace('_', '-')
                if isinstance(value, list):
                    for item in value:
                        child = SubElement(element, key)
                        if item is not True:
                            child.text = item
                else:
                    child = SubElement(element, key)
                    if value is not True:
                        child.text = value

        if fetch_config:
            reply = get_configuration(module, format=xattrs['format'])
        else:
            reply = exec_rpc(module, tostring(element), ignore_warning=False)

        if xattrs['format'] == 'text':
            if fetch_config:
                data = reply.find('.//configuration-text')
            else:
                data = reply.find('.//output')

            if data is None:
                module.fail_json(msg=tostring(reply))

            responses.append(data.text.strip())

        elif xattrs['format'] == 'json':
            responses.append(module.from_json(reply.text.strip()))

        elif xattrs['format'] == 'set':
            data = reply.find('.//configuration-set')
            if data is None:
                module.fail_json(
                    msg=
                    "Display format 'set' is not supported by remote device.")
            responses.append(data.text.strip())

        else:
            responses.append(tostring(reply))

    return responses
def main():
    """ main entry point for module execution
    """
    neighbors_spec = dict(host=dict(), port=dict())

    element_spec = dict(
        name=dict(),
        description=dict(),
        enabled=dict(default=True, type="bool"),
        speed=dict(type="str"),
        mtu=dict(type="int"),
        duplex=dict(choices=["full", "half", "auto"]),
        tx_rate=dict(),
        rx_rate=dict(),
        neighbors=dict(type="list", elements="dict", options=neighbors_spec),
        delay=dict(default=10, type="int"),
        state=dict(default="present",
                   choices=["present", "absent", "up", "down"]),
        active=dict(default=True, type="bool"),
    )

    aggregate_spec = deepcopy(element_spec)
    aggregate_spec["name"] = dict(required=True)

    # remove default in aggregate spec, to handle common arguments
    remove_default_spec(aggregate_spec)

    argument_spec = dict(
        aggregate=dict(type="list", elements="dict", options=aggregate_spec))

    argument_spec.update(element_spec)
    argument_spec.update(junos_argument_spec)

    required_one_of = [["name", "aggregate"]]
    mutually_exclusive = [["name", "aggregate"]]

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

    warnings = list()
    result = {"changed": False}

    if warnings:
        result["warnings"] = warnings

    top = "interfaces/interface"

    param_to_xpath_map = collections.OrderedDict()
    param_to_xpath_map.update([
        ("name", {
            "xpath": "name",
            "is_key": True
        }),
        ("description", "description"),
        ("speed", "speed"),
        ("mtu", "mtu"),
        ("duplex", "link-mode"),
        ("disable", {
            "xpath": "disable",
            "tag_only": True
        }),
    ])

    choice_to_value_map = {
        "link-mode": {
            "full": "full-duplex",
            "half": "half-duplex",
            "auto": "automatic",
        }
    }

    params = to_param_list(module)

    requests = list()
    for param in params:
        # if key doesn't exist in the item, get it from module.params
        for key in param:
            if param.get(key) is None:
                param[key] = module.params[key]

        item = param.copy()
        state = item.get("state")
        item["disable"] = True if not item.get("enabled") else False

        if state in ("present", "up", "down"):
            item["state"] = "present"

        validate_param_values(module, param_to_xpath_map, param=item)
        want = map_params_to_obj(module, param_to_xpath_map, param=item)
        requests.append(
            map_obj_to_ele(module,
                           want,
                           top,
                           value_map=choice_to_value_map,
                           param=item))

    diff = None
    with locked_config(module):
        for req in requests:
            diff = load_config(module, tostring(req), warnings, action="merge")

        # issue commit after last configuration change is done
        commit = not module.check_mode
        if diff:
            if commit:
                commit_configuration(module)
            else:
                discard_changes(module)
            result["changed"] = True

            if module._diff:
                result["diff"] = {"prepared": diff}

    failed_conditions = []
    neighbors = None
    for item in params:
        state = item.get("state")
        tx_rate = item.get("tx_rate")
        rx_rate = item.get("rx_rate")
        want_neighbors = item.get("neighbors")

        if (state not in ("up", "down") and tx_rate is None and rx_rate is None
                and want_neighbors is None):
            continue

        element = Element("get-interface-information")
        intf_name = SubElement(element, "interface-name")
        intf_name.text = item.get("name")

        if result["changed"]:
            sleep(item.get("delay"))

        reply = exec_rpc(module, tostring(element), ignore_warning=False)
        if state in ("up", "down"):
            admin_status = reply.xpath(
                "interface-information/physical-interface/admin-status")
            if not admin_status or not conditional(
                    state, admin_status[0].text.strip()):
                failed_conditions.append("state " + "eq(%s)" % state)

        if tx_rate:
            output_bps = reply.xpath(
                "interface-information/physical-interface/traffic-statistics/output-bps"
            )
            if not output_bps or not conditional(
                    tx_rate, output_bps[0].text.strip(), cast=int):
                failed_conditions.append("tx_rate " + tx_rate)

        if rx_rate:
            input_bps = reply.xpath(
                "interface-information/physical-interface/traffic-statistics/input-bps"
            )
            if not input_bps or not conditional(
                    rx_rate, input_bps[0].text.strip(), cast=int):
                failed_conditions.append("rx_rate " + rx_rate)

        if want_neighbors:
            if neighbors is None:
                element = Element("get-lldp-interface-neighbors")
                intf_name = SubElement(element, "interface-device")
                intf_name.text = item.get("name")

                reply = exec_rpc(module,
                                 tostring(element),
                                 ignore_warning=False)
                have_host = [
                    item.text for item in reply.xpath(
                        "lldp-neighbors-information/lldp-neighbor-information/lldp-remote-system-name"
                    )
                ]
                have_port = [
                    item.text for item in reply.xpath(
                        "lldp-neighbors-information/lldp-neighbor-information/lldp-remote-port-id"
                    )
                ]

            for neighbor in want_neighbors:
                host = neighbor.get("host")
                port = neighbor.get("port")
                if host and host not in have_host:
                    failed_conditions.append("host " + host)
                if port and port not in have_port:
                    failed_conditions.append("port " + port)
    if failed_conditions:
        msg = "One or more conditional statements have not been satisfied"
        module.fail_json(msg=msg, failed_conditions=failed_conditions)

    module.exit_json(**result)
def main():
    """ main entry point for module execution
    """
    neighbors_spec = dict(host=dict(), port=dict())

    element_spec = dict(name=dict(),
                        description=dict(),
                        enabled=dict(default=True, type='bool'),
                        speed=dict(),
                        mtu=dict(type='int'),
                        duplex=dict(choices=['full', 'half', 'auto']),
                        tx_rate=dict(),
                        rx_rate=dict(),
                        neighbors=dict(type='list',
                                       elements='dict',
                                       options=neighbors_spec),
                        delay=dict(default=10, type='int'),
                        state=dict(default='present',
                                   choices=['present', 'absent', 'up',
                                            'down']),
                        active=dict(default=True, type='bool'))

    aggregate_spec = deepcopy(element_spec)
    aggregate_spec['name'] = dict(required=True)

    # remove default in aggregate spec, to handle common arguments
    remove_default_spec(aggregate_spec)

    argument_spec = dict(aggregate=dict(type='list',
                                        elements='dict',
                                        options=aggregate_spec), )

    argument_spec.update(element_spec)
    argument_spec.update(junos_argument_spec)

    required_one_of = [['name', 'aggregate']]
    mutually_exclusive = [['name', 'aggregate']]

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

    warnings = list()
    result = {'changed': False}

    if warnings:
        result['warnings'] = warnings

    top = 'interfaces/interface'

    param_to_xpath_map = collections.OrderedDict()
    param_to_xpath_map.update([('name', {
        'xpath': 'name',
        'is_key': True
    }), ('description', 'description'), ('speed', 'speed'), ('mtu', 'mtu'),
                               ('duplex', 'link-mode'),
                               ('disable', {
                                   'xpath': 'disable',
                                   'tag_only': True
                               })])

    choice_to_value_map = {
        'link-mode': {
            'full': 'full-duplex',
            'half': 'half-duplex',
            'auto': 'automatic'
        }
    }

    params = to_param_list(module)

    requests = list()
    for param in params:
        # if key doesn't exist in the item, get it from module.params
        for key in param:
            if param.get(key) is None:
                param[key] = module.params[key]

        item = param.copy()
        state = item.get('state')
        item['disable'] = True if not item.get('enabled') else False

        if state in ('present', 'up', 'down'):
            item['state'] = 'present'

        validate_param_values(module, param_to_xpath_map, param=item)
        want = map_params_to_obj(module, param_to_xpath_map, param=item)
        requests.append(
            map_obj_to_ele(module,
                           want,
                           top,
                           value_map=choice_to_value_map,
                           param=item))

    diff = None
    with locked_config(module):
        for req in requests:
            diff = load_config(module, tostring(req), warnings, action='merge')

        # issue commit after last configuration change is done
        commit = not module.check_mode
        if diff:
            if commit:
                commit_configuration(module)
            else:
                discard_changes(module)
            result['changed'] = True

            if module._diff:
                result['diff'] = {'prepared': diff}

    failed_conditions = []
    neighbors = None
    for item in params:
        state = item.get('state')
        tx_rate = item.get('tx_rate')
        rx_rate = item.get('rx_rate')
        want_neighbors = item.get('neighbors')

        if state not in (
                'up', 'down'
        ) and tx_rate is None and rx_rate is None and want_neighbors is None:
            continue

        element = Element('get-interface-information')
        intf_name = SubElement(element, 'interface-name')
        intf_name.text = item.get('name')

        if result['changed']:
            sleep(item.get('delay'))

        reply = exec_rpc(module, tostring(element), ignore_warning=False)
        if state in ('up', 'down'):
            admin_status = reply.xpath(
                'interface-information/physical-interface/admin-status')
            if not admin_status or not conditional(
                    state, admin_status[0].text.strip()):
                failed_conditions.append('state ' + 'eq(%s)' % state)

        if tx_rate:
            output_bps = reply.xpath(
                'interface-information/physical-interface/traffic-statistics/output-bps'
            )
            if not output_bps or not conditional(
                    tx_rate, output_bps[0].text.strip(), cast=int):
                failed_conditions.append('tx_rate ' + tx_rate)

        if rx_rate:
            input_bps = reply.xpath(
                'interface-information/physical-interface/traffic-statistics/input-bps'
            )
            if not input_bps or not conditional(
                    rx_rate, input_bps[0].text.strip(), cast=int):
                failed_conditions.append('rx_rate ' + rx_rate)

        if want_neighbors:
            if neighbors is None:
                element = Element('get-lldp-interface-neighbors')
                intf_name = SubElement(element, 'interface-device')
                intf_name.text = item.get('name')

                reply = exec_rpc(module,
                                 tostring(element),
                                 ignore_warning=False)
                have_host = [
                    item.text for item in reply.xpath(
                        'lldp-neighbors-information/lldp-neighbor-information/lldp-remote-system-name'
                    )
                ]
                have_port = [
                    item.text for item in reply.xpath(
                        'lldp-neighbors-information/lldp-neighbor-information/lldp-remote-port-id'
                    )
                ]

            for neighbor in want_neighbors:
                host = neighbor.get('host')
                port = neighbor.get('port')
                if host and host not in have_host:
                    failed_conditions.append('host ' + host)
                if port and port not in have_port:
                    failed_conditions.append('port ' + port)
    if failed_conditions:
        msg = 'One or more conditional statements have not been satisfied'
        module.fail_json(msg=msg, failed_conditions=failed_conditions)

    module.exit_json(**result)