def _update_patch_request(self, want, have):

        facts, _warnings = Facts(self._module).get_facts(
            self.gather_subset, [
                'vlans',
            ])
        vlans_facts = facts['ansible_network_resources'].get('vlans')

        vlan_id = []

        for vlan in vlans_facts:
            vlan_id.append(vlan['vlan_id'])

        if want.get("access"):
            if want["access"]["vlan"] in vlan_id:
                l2_request = deepcopy(self.L2_INTERFACE_ACCESS)
                l2_request["data"]["openconfig-vlan:config"][
                    "access-vlan"] = want["access"]["vlan"]
                l2_request["path"] = self.L2_PATH + str(
                    want["name"]
                ) + "/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config"
            else:
                self._module.fail_json(msg="VLAN %s does not exist" %
                                       (want["access"]["vlan"]))

        elif want.get("trunk"):
            if want["trunk"]["native_vlan"]:
                if want["trunk"]["native_vlan"] in vlan_id:
                    l2_request = deepcopy(self.L2_INTERFACE_NATIVE)
                    l2_request["data"]["openconfig-vlan:config"][
                        "native-vlan"] = want["trunk"]["native_vlan"]
                    l2_request["path"] = self.L2_PATH + str(
                        want["name"]
                    ) + "/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config"
                    for vlan in want["trunk"]["trunk_allowed_vlans"]:
                        if int(vlan) in vlan_id:
                            l2_request["data"]["openconfig-vlan:config"][
                                "trunk-vlans"].append(int(vlan))
                        else:
                            self._module.fail_json(
                                msg="VLAN %s does not exist" % (vlan))
                else:
                    self._module.fail_json(msg="VLAN %s does not exist" %
                                           (want["trunk"]["native_vlan"]))
            else:
                l2_request = deepcopy(self.L2_INTERFACE_TRUNK)
                l2_request["path"] = self.L2_PATH + str(
                    want["name"]
                ) + "/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config"
                for vlan in want["trunk"]["trunk_allowed_vlans"]:
                    if int(vlan) in vlan_id:
                        l2_request["data"]["openconfig-vlan:config"][
                            "trunk-vlans"].append(int(vlan))
                    else:
                        self._module.fail_json(msg="VLAN %s does not exist" %
                                               (vlan))
        return l2_request
Example #2
0
    def get_interfaces_facts(self):
        """ Get the 'facts' (the current configuration)

        :rtype: A dictionary
        :returns: The current configuration as a dictionary
        """
        facts, _warnings = Facts(self._module).get_facts(
            self.gather_subset, self.gather_network_resources)
        interfaces_facts = facts['ansible_network_resources'].get('interfaces')
        if not interfaces_facts:
            return []
        return interfaces_facts
Example #3
0
    def get_lldp_global_facts(self):
        """ Get the 'facts' (the current configuration)

        :rtype: A dictionary
        :returns: The current configuration as a dictionary
        """
        facts, _warnings = Facts(self._module).get_facts(
            self.gather_subset, self.gather_network_resources)
        lldp_global_facts = facts['ansible_network_resources'].get(
            'lldp_global')
        if not lldp_global_facts:
            return {}
        return lldp_global_facts
Example #4
0
def main():
    """Main entry point for AnsibleModule
    """
    argument_spec = FactsArgs.argument_spec

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

    warnings = [
        'default value for `gather_subset` '
        'will be changed to `min` from `!config` v2.11 onwards'
    ]

    result = Facts(module).get_facts()

    ansible_facts, additional_warnings = result
    warnings.extend(additional_warnings)

    module.exit_json(ansible_facts=ansible_facts, warnings=warnings)