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/roster/__pycache__/cache.cpython-310.pyc
o

�N�gx�@spdZddlZddlZddlZddlZddlZddlZddlm	Z	e�
e�Zd
dd�Z
dd�Zd	d
�Zdd�ZdS)a�

The ``cache`` roster provides a flexible interface to the Salt Masters' minion cache
to access regular minions over ``salt-ssh``.

.. versionadded:: 2017.7.0

    - grains, pillar, mine data matching
    - SDB URLs
    - IPv6 support
    - roster_order per config key
    - default order changed to industry-wide best practices
    - CIDR range selection


Targeting
---------

This roster supports all matching and targeting of the Salt Master.
The matching will be done using only the Salt Master's cache.


The Roster Order
----------------

The roster's composition can be configured using ``roster_order``.
In the ``roster_order`` you can define *any* roster key and fill it with a parameter
overriding the one in ``roster_defaults``:

.. code-block:: yaml

    roster_order:
        host: id          # use the minion id as hostname


You can define lists of parameters as well, the first result from the list will become the value.


Selecting a host
================

.. code-block:: yaml

    # default
    roster_order:
        host:
          - ipv6-private  # IPv6 addresses in private ranges
          - ipv6-global   # IPv6 addresses in global ranges
          - ipv4-private  # IPv4 addresses in private ranges
          - ipv4-public   # IPv4 addresses in public ranges
          - ipv4-local    # loopback addresses


This is the default ``roster_order``.
It prefers IPv6 over IPv4 addresses and private addresses over public ones.
The relevant data will be fetched from the cache in-order, and the first match will fill the ``host`` key.

Other address selection parameters are also possible:

.. code-block:: yaml

    roster_order:
      host:
        - global|public|private|local    # Both IPv6 and IPv4 addresses in that range
        - 2000::/3                       # CIDR networks, both IPv4 and IPv6 are supported


Using cached data
=================

Several cached libraries can be selected using the ``library: `` prefix, followed by the library key.
This can be referenced using the same ``:`` syntax as e.g. :py:func:`pillar.get <salt.modules.pillar.get>`.
Lists of references are also supported during the lookup, as are Salt SDB URLs.

This should be especially useful for the other roster keys:

.. code-block:: yaml

    roster_order:
      host:
        - grain: fqdn_ip4                # Lookup this grain
        - mine: network.ip_addrs         # Mine data lookup works the same

      password: sdb://vault/ssh_pass     # Salt SDB URLs are also supported

      user:
        - pillar: ssh:auth:user          # Lookup this pillar key
        - sdb://osenv/USER               # Lookup this env var through sdb

      priv:
        - pillar:                        # Lists are also supported
            - salt:ssh:private_key
            - ssh:auth:private_key

�N)�	ipaddress�globc	Kstjj�t�}|�||�}|d}i}|s|Stj�t�}t�dddi�}i}|D]Q}zt	||�}	Wn	t
y;Yq)wt�t�di��}
|�
�D]!\}}t|ttf�sW|g}|D]}
t||
|	�}|ri||
|<nqYqId|
vrt|
||<q)t�d|�q)t�d|�|S)z�
    Return the targets from the Salt Masters' minion cache.
    All targets and matchers are supported.

    The resulting roster can be configured using ``roster_order`` and ``roster_default``.
    �minions�roster_order�host)zipv6-privatezipv6-globalzipv4-privatezipv4-publicZroster_defaultsz2Could not determine host information for minion %szRoster lookup result: %s)�salt�utilsrZ	CkMinions�__opts__Z
check_minions�cacheZCache�get�_load_minion�LookupError�copy�deepcopy�items�
isinstance�list�tuple�_minion_lookup�log�warning�debug)ZtgtZtgt_type�kwargsrZ_res�retr
r�	minion_id�minionZ
minion_res�param�order�keyZkres�r�E/opt/saltstack/salt/lib/python3.10/site-packages/salt/roster/cache.py�targetslsD����
r!cCs�tjj�|t�\}}}||krt�d||�t�|s"t�d|�i}|s,t�d|�i}t	dd�|�
dg�D��t	dd�|�
dg�D��d	�}|�d
|��d�}||||fS)NzAsked for minion %s, got %szNo grain data for minion id %szNo pillar data for minion id %scs��|]}t�|�VqdS�N)r�IPv4Address��.0�addrrrr �	<genexpr>���z_load_minion.<locals>.<genexpr>Zipv4csr"r#)r�IPv6Addressr%rrr r(�r)Zipv6)��zminions/�mine)rrrZget_minion_datar	r�errorr
r�sortedrZfetch)rr
Zdata_minion�grains�pillar�addrsr-rrr r�s"
��rcCsBt|t�r|g}g}|D]}tjj�||d�}|r|�|�q|Sr#)r�strrr�dataZtraverse_dict_and_list�append)�ref�lookup�resZdata_keyr4rrr �_data_lookup�s

�r9c
	s�|\��}����fdd�}|dkr|St|t�r8|��D]\}}t||�|�D]
}|r2|Sq(dSdS|�d�rCtd|�St�d|�rwz	tj	|dd�}Wnt
ybt�d	|�YdSw||j
D]}	|	|vrtt|	�SqhdSd
d�dd�d
d�dd�d�}
ddg}|�d�r�t|d�g}|dd�}|D])}z||D]}	|
||	�r�t|	�WSq�Wq�ty�td|�d���wdS)Ncs(|dkr�S|dkr�S|dkr�SdS)Nr1r0r-r)�data_id�r0r-r1rr �_data_lookup_ref�s�z(_minion_lookup.<locals>._data_lookup_ref�idzsdb://zsdb.getz^[0-9a-fA-F:./]+$T)�strictz%s is an invalid CIDR networkcSs|jdkr|jS|jS)Nr,)�version�	is_global�
is_private�r'rrr �<lambda>�sz _minion_lookup.<locals>.<lambda>cSs|jSr#)rArBrrr rC�scSs|jo
|jo
|jSr#)rA�is_loopback�
is_link_localrBrrr rC�s�cSs|jSr#)rDrBrrr rC�s)�globalZpublicZprivate�localr+r,Zipv��zInvalid filter z specified in roster_order)r�dictrr9�
startswithr�re�matchr�
ip_network�
ValueErrorrr.r?r3�int�KeyError)
rrrr2r<r:r7�k�netr'�filtersZip_versZip_verrr;r r�s\
��
����
����r)r)�__doc__r�loggingrLZ
salt.cacherZsalt.utils.dataZsalt.utils.minionsZsalt._compatr�	getLogger�__name__rr!rr9rrrrr �<module>s_

2