HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/pillar/__pycache__/netbox.cpython-310.pyc
o

�N�gא�@s�dZddlZddlZddlZddlmZe�e�Z	dd�Z
dd�Zdd	�Zd
d�Z
dd
�Zdd�Zdd�Zdd�Zdd�Zdd�ZdS)aM
A module that adds data to the Pillar structure from a NetBox API.

.. versionadded:: 2019.2.0

Configuring the NetBox ext_pillar
---------------------------------

To use this pillar, you must first create a token in your NetBox instance at
http://netbox.example.com/user/api-tokens/ (substituting the hostname of your
NetBox instance)

The NetBox api_url and api_token must be set in the master
config.

For example ``/etc/salt/master.d/netbox.conf``:

.. code-block:: yaml

  ext_pillar:
    - netbox:
        api_url: http://netbox.example.com/api/
        api_token: 123abc


The following options are optional, and determine whether or not
the module will attempt to configure the ``proxy`` pillar data for
use with the napalm proxy-minion:

.. code-block:: yaml

  proxy_return: True
  proxy_username: admin

By default, this module will query the NetBox API for the platform
associated with the device, and use the 'NAPALM driver' field to
set the napalm proxy-minion driver. (Currently only 'napalm' is supported
for drivertype.)

This module currently only supports the napalm proxy minion and assumes
you will use SSH keys to authenticate to the network device.  If password
authentication is desired, it is recommended to create another ``proxy``
key in pillar_roots (or git_pillar) with just the ``passwd`` key and use
:py:func:`salt.renderers.gpg <salt.renderers.gpg>` to encrypt the value.

If you use more than one username for your devices, leave proxy_username unset,
and set the ``username`` key in your pillar as well. If any additional options
for the proxy setup are needed, they should also be configured in pillar_roots.

Other available configuration options:

site_details: ``True``
    Whether should retrieve details of the site the device belongs to.

site_prefixes: ``True``
    Whether should retrieve the prefixes of the site the device belongs to.

devices: ``True``
    .. versionadded:: 3004

    Whether should retrieve physical devices.

virtual_machines: ``False``
    .. versionadded:: 3004

    Whether should retrieve virtual machines.

interfaces: ``False``
    .. versionadded:: 3004

    Whether should retrieve the interfaces of the device.

interface_ips: ``False``
    .. versionadded:: 3004

    Whether should retrieve the IP addresses for interfaces of the device.
    (interfaces must be set to True as well)

api_query_result_limit: ``Use NetBox default``
    .. versionadded:: 3004

    An integer specifying how many results should be returned for each query
    to the NetBox API. Leaving this unset will use NetBox's default value.

connected_devices: ``False``
    .. versionadded:: 3006.0

    Whether connected_devices key should be populated with device objects.
    If set to True it will force `interfaces` to also be true as a dependency

Note that each option you enable can have a detrimental impact on pillar
performance, so use them with caution.

After configuring the pillar, you must restart the Salt master for the changes
to take effect.

For example:

.. code-block:: shell

  systemctl restart salt-master

To query perform a quick test of the pillar, you should refresh the pillar on
the minion with the following:

.. code-block:: shell

  salt minion1 saltutil.refresh_pillar

And then query the pillar:

.. code-block:: shell

  salt minion1 pillar.items 'netbox'

Example output:

.. code-block:: text

  minion1:
      netbox:
          ----------
          id:
              511
          url:
              https://netbox.example.com/api/dcim/devices/511/
          name:
              minion1
          node_type:
              device
          display_name:
              minion1
          device_type:
              ----------
              id:
                  4
              url:
                  https://netbox.example.com/api/dcim/device-types/4/
              manufacturer:
                  ----------
                  id:
                      1
                  url:
                      https://netbox.example.com/api/dcim/manufacturers/1/
                  name:
                      Cisco
                  slug:
                      cisco
              model:
                  ISR2901
              slug:
                  isr2901
              display_name:
                  Cisco ISR2901
          device_role:
              ----------
              id:
                  45
              url:
                  https://netbox.example.com/api/dcim/device-roles/45/
              name:
                  Network
              slug:
                  network
          interfaces:
              |_
                ----------
                id:
                    8158
                ip_addresses:
                    |_
                      ----------
                      id:
                          1146
                      url:
                          https://netbox.example.com/api/ipam/ip-addresses/1146/
                      family:
                          ----------
                          value:
                              4
                          label:
                              IPv4
                      address:
                          192.0.2.1/24
                      vrf:
                          None
                      tenant:
                          None
                      status:
                          ----------
                          value:
                              active
                           label:
                              Active
                      role:
                          None
                      nat_inside:
                          None
                      nat_outside:
                          None
                      dns_name:
                      description:
                      tags:
                      custom_fields:
                      created:
                          2021-02-19
                      last_updated:
                          2021-02-19T06:12:04.153386Z
                url:
                    https://netbox.example.com/api/dcim/interfaces/8158/
                name:
                    GigabitEthernet0/0
                label:
                type:
                    ----------
                    value:
                        1000base-t
                    label:
                        1000BASE-T (1GE)
                enabled:
                    True
                lag:
                    None
                mtu:
                    None
                mac_address:
                    None
                mgmt_only:
                    False
                description:
                mode:
                    None
                untagged_vlan:
                    None
                tagged_vlans:
                cable:
                    None
                cable_peer:
                    None
                cable_peer_type:
                    None
                connected_endpoint:
                    None
                connected_endpoint_type:
                    None
                connected_endpoint_reachable:
                    None
                tags:
                count_ipaddresses:
                    1
              |_
                ----------
                id:
                    8159
                ip_addresses:
                    |_
                      ----------
                      id:
                          1147
                      url:
                          https://netbox.example.com/api/ipam/ip-addresses/1147/
                      family:
                          ----------
                          value:
                              4
                          label:
                              IPv4
                      address:
                          198.51.100.1/24
                      vrf:
                          None
                      tenant:
                          None
                      status:
                          ----------
                          value:
                              active
                          label:
                              Active
                      role:
                          None
                      nat_inside:
                          None
                      nat_outside:
                          None
                      dns_name:
                      description:
                      tags:
                      custom_fields:
                      created:
                          2021-02-19
                      last_updated:
                          2021-02-19T06:12:40.508154Z
                      url:
                          https://netbox.example.com/api/dcim/interfaces/8159/
                      name:
                          GigabitEthernet0/1
                      label:
                      type:
                          ----------
                          value:
                              1000base-t
                          label:
                              1000BASE-T (1GE)
                      enabled:
                          True
                      lag:
                          None
                      mtu:
                          None
                      mac_address:
                          None
                      mgmt_only:
                          False
                      description:
                      mode:
                          None
                      untagged_vlan:
                          None
                      tagged_vlans:
                      cable:
                          None
                      cable_peer:
                          None
                      cable_peer_type:
                          None
                      connected_endpoint:
                          None
                      connected_endpoint_type:
                          None
                      connected_endpoint_reachable:
                          None
                      tags:
                      count_ipaddresses:
                          1
          tenant:
              None
          platform:
              ----------
              id:
                  1
              url:
                  https://netbox.example.com/api/dcim/platforms/1/
              name:
                  Cisco IOS
              slug:
                  ios
          serial:
          asset_tag:
              None
          site:
              ----------
              id:
                  18
              url:
                  https://netbox.example.com/api/dcim/sites/18/
              name:
                  Site 1
              slug:
                  site1
              status:
                  ----------
                  value:
                      active
                  label:
                      Active
              region:
                  None
              tenant:
                  None
              facility:
              asn:
                  None
              time_zone:
                  None
              description:
              physical_address:
              shipping_address:
              latitude:
                  None
              longitude:
                  None
              contact_name:
              contact_phone:
              contact_email:
              comments:
              tags:
              custom_fields:
              created:
                  2021-02-25
              last_updated:
                  2021-02-25T14:21:07.898957Z
              circuit_count:
                  0
              device_count:
                  1
              prefix_count:
                  2
              rack_count:
                  0
              virtualmachine_count:
                  1
              vlan_count:
                  0
              prefixes:
                  |_
                    ----------
                    id:
                        284
                    url:
                        https://netbox.example.com/api/ipam/prefixes/284/
                    family:
                        ----------
                        value:
                            4
                        label:
                            IPv4
                    prefix:
                        192.0.2.0/24
                    vrf:
                        None
                    tenant:
                        None
                    vlan:
                        None
                          ----------
                        value:
                            active
                        label:
                            Active
                    role:
                        None
                    is_pool:
                        False
                    description:
                    tags:
                    custom_fields:
                    created:
                        2021-02-25
                    last_updated:
                        2021-02-25T15:08:27.136305Z
                  |_
                    ----------
                    id:
                        285
                    url:
                        https://netbox.example.com/api/ipam/prefixes/285/
                    family:
                        ----------
                        value:
                            4
                        label:
                            IPv4
                    prefix:
                        198.51.100.0/24
                    vrf:
                        None
                    tenant:
                        None
                    vlan:
                        None
                    status:
                        ----------
                        value:
                            active
                        label:
                            Active
                    role:
                        None
                    is_pool:
                        False
                    description:
                    tags:
                    custom_fields:
                    created:
                        2021-02-25
                    last_updated:
                        2021-02-25T15:08:59.880440Z
          rack:
              None
          position:
              None
          face:
              None
          parent_device:
              None
          status:
              ----------
              value:
                  active
              label:
                  Active
          primary_ip:
              ----------
              id:
                  1146
              url:
                  https://netbox.example.com/api/ipam/ip-addresses/1146/
              family:
                  4
              address:
                  192.0.2.1/24
          primary_ip4:
              ----------
              id:
                  1146
              url:
                  https://netbox.example.com/api/ipam/ip-addresses/1146/
              family:
                  4
              address:
                  192.0.2.1/24
          primary_ip6:
              None
          cluster:
              None
          virtual_chassis:
              None
          vc_position:
              None
          vc_priority:
              None
          comments:
          local_context_data:
              None
          tags:
          custom_fields:
          config_context:
          connected_devices:
        ----------
        512:
            ----------
            airflow:
                None
            asset_tag:
                001
            cluster:
                None
            comments:
            config_context:
            created:
                2022-03-10T00:00:00Z
            custom_fields:
            device_role:
                ----------
                display:
                    Network switch
                id:
                    512
                name:
                    Network switch
                slug:
                    network_switch
                url:
                    https://netbox.example.com/api/dcim/device-roles/5/
            device_type:
                ----------
                display:
                    Nexus 3048
                id:
                    40
                manufacturer:
                    ----------
                    display:
                        Cisco
                    id:
                        1
                    name:
                        Cisco
                    slug:
                        cisco
                    url:
                        https://netbox.example.com/api/dcim/manufacturers/1/
                model:
                    Nexus 3048
                slug:
                    n3k-c3048tp-1ge
                url:
                    https://netbox.example.com/api/dcim/device-types/40/
            display:
                another device (001)
            face:
                ----------
                label:
                    Front
                value:
                    front
            id:
                1533
            last_updated:
                2022-08-22T13:50:15.923868Z
            local_context_data:
                None
            location:
                ----------
                _depth:
                    2
                display:
                    Location Name
                id:
                    2
                name:
                    Location Name
                slug:
                    location-name
                url:
                    https://netbox.example.com/api/dcim/locations/2
            name:
                another device
            parent_device:
                None
            platform:
                None
            position:
                18.0
            primary_ip:
                ----------
                address:
                    192.168.1.1/24
                display:
                    192.168.1.1/24
                family:
                    4
                id:
                    1234
                url:
                    https://netbox.example.com/api/ipam/ip-addresses/1234/
            primary_ip4:
                ----------
                address:
                    192.168.1.1/24
                display:
                    192.168.1.1/24
                family:
                    4
                id:
                    1234
                url:
                    https://netbox.example.com/api/ipam/ip-addresses/1234/
            primary_ip6:
                None
            rack:
                ----------
                display:
                    RackName
                id:
                    139
                name:
                    RackName
                url:
                    https://netbox.example.com/api/dcim/racks/139/
            serial:
                ABCD12345
            site:
                ----------
                display:
                    SiteName
                id:
                    2
                name:
                    SiteName
                slug:
                    sitename
                url:
                    https://netbox.example.com/api/dcim/sites/2/
            status:
                ----------
                label:
                    Active
                value:
                    active
            tags:
            tenant:
                None
            url:
                https://netbox.example.com/api/dcim/devices/1533/
            vc_position:
                None
            vc_priority:
                None
            virtual_chassis:
                None
          created:
              2021-02-19
          last_updated:
              2021-02-19T06:12:04.171105Z
�N)�	ipaddressc
C��dj|ddd�}g}d|i}|r||d<tjjj|||dd�}	d	|vr2t�d
||d|d	�gS|�|dd
�|ddrPtjjj|dd|dd�}nnq d}|D]}	d||d<|d7}qV|S)N�{api_url}/{app}/{endpoint}�dcim�devices��api_url�app�endpoint�name�limitT��params�header_dict�decode�error�4API query failed for "%s", status code: %d, error %s�status�dict�results�next�rrr�device�	node_type���format�salt�utils�http�query�logr�extend)
r�	minion_id�headers�api_query_result_limit�
device_url�device_resultsr�
device_retZdevice_countr�r)�F/opt/saltstack/salt/lib/python3.10/site-packages/salt/pillar/netbox.py�_get_devices��>�����
r+c
Cr)Nr�virtualizationzvirtual-machinesrrrTr
rrrrrrrr�virtual-machinerrr)
rr#r$r%Zvm_urlZ
vm_resultsrZvm_retZvm_countZvmr)r)r*�_get_virtual_machines�r,r/c	Cst�d|�g}|dkrd}d}n|dkrd}d}dj||d	d
�}	||i}
|r+||
d<tjjj|	|
|dd
�}	d|vrKt�d||||d|d�gS|�|dd�|ddritjjj|dd|dd�}nnq7d}|D]}
|dkr{||d=n	|dkr�||d=|d7}qo|S)NzRetrieving interfaces for "%s"rr�	device_idr.r-�virtual_machine_idr�
interfacesrrTr
rzRUnable to retrieve interfaces for "%s" (Type %s, ID %d), status code: %d, error %srrrrrrZvirtual_machiner�	r!�debugrrrrr rr")rr#�node_idrr$r%Zinterfaces_results�app_name�
node_paramZinterfaces_urlrZinterfaces_ret�interface_count�	interfacer)r)r*�_get_interfaces
sb������

r:c	Cs�t�d|�g}|dkrd}d}n|dkrd}d}dj|d	d
d�}	||i}
|r+||
d<tjjj|	|
|d
d�}	d|vrKt�d||||d|d�gS|�|dd�|ddritjjj|dd|d
d�}n	|Sq7)Nz Retrieving IP addresses for "%s"rrr0r.r-r1r�ipamzip-addressesrrTr
rz^Unable to retrieve interface IP addresses for "%s" (Type %s, ID %d), status code: %d, error %srrrrrr3)rr#r5rr$r%Zinterface_ips_resultsr6r7Zinterface_ips_urlrZinterface_ips_retr)r)r*�_get_interface_ipsJsT������r<cCsxd}|D]5}t|�dkr5g||d<|D] }d|vr4|d|dkr4|d=|d=|d=||d�|�q|d7}q|S)NrZip_addressesZassigned_object_id�idZassigned_object_typeZassigned_objectr)�len�append)�interfaces_list�interface_ips_listr8r9�ipr)r)r*�_associate_ips_to_interfaces�s�
rCcCsdt�d|||�dj|dd|d�}tjjj||dd�}d|vr.t�d	|||d
|d�iS|dS)Nz2Retrieving site details for "%s" - site %s (ID %d)z%{api_url}/{app}/{endpoint}/{site_id}/rZsites)rr	r
�site_idTrrzIUnable to retrieve site details for %s (ID %d), status code: %d, error %srr)r!r4rrrrr r)rr#�	site_namerDr$Zsite_urlZsite_details_retr)r)r*�_get_site_details�s(���rFcCs�t�d|�i}g}|D]*}d|��vr6|dr6|dD]}d|��vr5|dd|vr5|�|dd�qqt�d|�|D]0}dj|dd|d	�}	g}
tjjj|	|d
d�}d|vrgt�	d
||d|d�q?t
|d�||<q?|S)Nz%Retrieving connected devices for "%s"Zconnected_endpointsrr=zconnected_devices_ids: %sz#{api_url}/{app}/{endpoint}/{dev_id}rr)rr	r
�dev_idTrrrrr)r!r4�keysr?rrrrr rr)rr#r2r$Zconnected_devices_resultZconnected_devices_idsZ	int_shortZdevice_shortrGr&r'r(r)r)r*�_get_connected_devices�s6���rIcCs�t�d|||�g}dj|ddd�}d|i}|r||d<tjjj|||dd	�}		d
|	vr;t�d|||	d|	d
�gS|�|	d
d�|	d
drYtjjj|	d
d|dd�}	nnq(d}
|D]}||
d=|
d7}
q_|S)Nz3Retrieving site prefixes for "%s" - site %s (ID %d)rr;�prefixesrrDrTr
rzJUnable to retrieve site prefixes for %s (ID %d), status code: %d, error %srrrrrr�siterr3)rr#rErDr$r%Zsite_prefixes_resultsZprefixes_urlrZsite_prefixes_retZprefix_count�prefixr)r)r*�_get_site_prefixes�sL������

rMc	Cs�t�d|�dj|dd|d�}tjjj||dd�}d|vr+t�d	||d
|d�dS|d�d�}|rBt	t
�|�j�|d
d�}|SdS)Nz!Retrieving proxy details for "%s"z {api_url}/{app}/{endpoint}/{id}/rZ	platforms)rr	r
r=Trrz9Unable to proxy details for %s, status code: %d, error %srr�
napalm_driverZnapalm)�hostZdriverZ	proxytype)
r!r4rrrrr r�get�strr�ip_interfacerB)	rr#�
primary_ip�platform_idr$Zplatform_urlZplatform_retrN�proxyr)r)r*�_get_proxy_detailss.���	��rVcOsT|dkrt�d�iS|d�d�}|�d�}|�dd�}|�dd	�}|�d
d	�}|�dd	�}	|�dd�}
|�d
d�}|�dd�}|�dd�}
|�dd	�}|rX|sXd}t�d�|�d�}i}tjj�|ddg�sqt�	d|�|S|s||s|t�	d�|S|	r�|s�t�	d�|S|r�t
|�dkr�t�	d�|Si}|r�dd|��i}nt�	d�|Sg}|r�|�t||||��|r�|�t
||||��t|�dkr�|d|d<nt|�dkr�t�	d |�|St�	d!|�|S|dd"}|dd#}|�rt||||||�}t|�dk�r|	�rt||||||�}t||�|dd
<|dd$d"}|dd$d%}|
�r9t|||||�|dd$<|�rKt||||||�|dd$d&<|�r]t|||dd
|�|dd<|
�r�|dd'�rp|dd'd"}nt�	d(|�dS|dd)�r�|dd)d*}nt�	d+|�dSt|||||�}|�r�||d,<|�r�||d,d-<|S).z*
    Query NetBox API for minion data
    �*z5There's no data to collect from NetBox for the Masterr�/�	api_tokenrT�virtual_machinesFr2�
interface_ips�site_details�
site_prefixes�proxy_usernameN�proxy_return�connected_deviceszEnetbox pillar interfaces set to 'True' as connected_devices is 'True'r%r�httpszFProvided URL for api_url "%s" is malformed or is not an http/https URLz8At least one of devices or virtual_machines must be Truez>The value for interfaces must be True if interface_ips is TruerzEThe value for api_query_result_limit must be a postive integer if setZ
AuthorizationzToken z"The value for api_token is not setrZnetboxz!More than one node found for "%s"z#Unable to pull NetBox data for "%s"r=rrKrrJ�platformzZYou have set "proxy_return" to "True" but you have not set the platform in NetBox for "%s"rS�addressznYou have set "proxy_return" to "True" but you have not set the primary IPv4 or IPv6 address in NetBox for "%s"rU�username)r!�info�rstriprPr4rr�url�validater�intr"r+r/r>r:r<rCrFrMrIrV)r#Zpillar�args�kwargsrrYrrZr2r[r\r]r^r_r`r%�retr$Znodesr5rr@rArDrErTrSrUr)r)r*�
ext_pillars�

�
�

�
����
�����rm)�__doc__�loggingZsalt.utils.httprZsalt.utils.urlZsalt._compatr�	getLogger�__name__r!r+r/r:r<rCrFrIrMrVrmr)r)r)r*�<module>s*5
))=7"3