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__/terraform.cpython-310.pyc
o

�N�g��
@s�dZddlZddlZddlZddlZe�e�Z	dZ
ddddddddddddd�Zd	Zd
d�Z
dd
�Zdd�Zdd�Zddd�Zdd�Zdd�Zddd�ZdS)a�
Dynamic roster from terraform current state
===========================================

This roster module allows you dynamically generate the roster from the terraform
resources defined with the `Terraform Salt`_ provider.

It exposes all salt_host resources with the same attributes to the salt-ssh
roster, making it completely independent of the type of terraform resource, and
providing the integration using terraform constructs with interpolation.

Basic Example
-------------

Given a simple salt-ssh tree with a Saltfile:

.. code-block:: yaml

    salt-ssh:
      config_dir: etc/salt
      max_procs: 30
      wipe_ssh: True

and ``etc/salt/master``:

.. code-block:: yaml

    root_dir: .
      file_roots:
        base:
          - srv/salt
      pillar_roots:
        base:
          - srv/pillar
      roster: terraform

In the same folder as your ``Saltfile``, create terraform file with resources
like cloud instances, virtual machines, etc. For every single one of those that
you want to manage with Salt, create a ``salt_host`` resource:

.. code-block:: text

    resource "salt_host" "dbminion" {
      salt_id = "dbserver"
      host = "${libvirt_domain.vm-db.network_interface.0.addresses.0}"
      user = "root"
      passwd = "linux"
    }

You can use the count attribute to create multiple roster entries with a single
definition. Please refer to the `Terraform Salt`_ provider for more detailed
examples.

.. _Terraform Salt: https://github.com/dmacvicar/terraform-provider-salt
�Nzsalt.roster.�s�i�b�m)�host�user�passwd�portZsudoZ	sudo_userZtty�priv�timeoutZminion_optsZthin_dirZ	cmd_umaskZsalt_idcCsfi}|�di��di�}|�t�|t<t|����t���}|D]}t||�|�t�|��||<q |S)z�
    Handles salt_host resources.
    See https://github.com/dmacvicar/terraform-provider-salt

    Returns roster attributes for the resource or None
    Zprimary�
attributes)�get�	MINION_ID�set�keys�intersection�TF_ROSTER_ATTRS�_cast_output_to_type)�resource�ret�attrs�valid_attrs�attr�r�I/opt/saltstack/salt/lib/python3.10/site-packages/salt/roster/terraform.py�_handle_old_salt_host_resourceSs
�rcCs�g}|�dg�}|D]9}i}|�di�}|�t�|t<t|����t���}|D]}t||�|�t�|��||<q(t�|�|�	|�q
|S)z�
    Handles salt_host resources.
    See https://github.com/dmacvicar/terraform-provider-salt
    Returns roster attributes for the resource or None
    �	instancesr)
r
rrrrrr�log�info�append)rZretsr�instancerrrrrrr�_handle_new_salt_host_resourcees
�
r!c
Csxd}t�d�rtj�tj�d��rtj�d�}nt�dtj�tj�tddd���}|r8tj�|�r:||d<dSdSdS)	zM
    Setups the salt-ssh minion to be accessed with salt-ssh default key
    NZssh_use_home_keyz
~/.ssh/id_rsaZssh_privZpki_dirZsshzsalt-ssh.rsar
)�__opts__r
�os�path�isfile�
expanduser�abspath�join)rr
rrr�_add_ssh_key{s
���r)cCsH|dur|dkr
dS|S|dur|S|dkrt|�S|dkr"t|�S|S)z.cast the value depending on the terraform typeNrrrr)�bool�int)r�value�typrrrr�sr�terraform.tfstatecCsltjj�|d��}tjj�|�}Wd�n1swYd|vr't|�Sd|vr/t|�St�	d�iS)z_
    Parses the terraform state file passing different resource types to the right handler
    �rN�	resources�moduleszMalformed tfstate file.)
�salt�utils�filesZfopen�json�load�_do_parse_new_state_file�_do__parse_old_state_filer�error)Zstate_file_pathZfh_�tfstaterrr�_parse_state_file�s�
r;cCs�i}|�d�}|st�d�|S|D]8}|ddkrJt|�}|r&t|�dkr'q|D] }|s.q)|�t|�d��}|s:q)t|vrA|t=t|�|||<q)q|S)z}
    Parses the terraform state file passing different resource types to the right handler  terraform version >= v0.13.0
    r0z*Malformed tfstate file. No resources found�type�	salt_host��id)r
rr9r!�lenrr))r:rr0rZ
roster_entrys�roster_entry�	minion_idrrrr7�s,


�r7c	Cs�i}|�d�}|st�d�|S|D];}|�dg�}|��D].\}}d}|ddkr.t|�}|s1q|�t|�d��}|s=qt|vrD|t=t|�|||<qq|S)z|
    Parses the terraform state file passing different resource types to the right handler  terraform version < v0.13.0
    r1z(Malformed tfstate file. No modules foundr0Nr<r=r?)r
rr9�itemsrrr))	r:rr1�moduler0Z
resource_namerrArBrrrr8�s,


�r8�globcKs�tj�d�}t�d�rtj�td�}tj�|�s!t�d|�iSt�d|�|�	d�s3t�d�iSt
|�}t�dt|��td|||d	�S)
zw
    Returns the roster from the terraform state file, checks opts for location, but defaults to terraform.tfstate
    r.�roster_filez$Can't find terraform state file '%s'z%terraform roster: using %s state filez.tfstatez<Terraform roster can only be used with terraform state filesz %s hosts in terraform state filezroster_matcher.targetsZipv4)
r#r$r'r"r
r%rr9�debug�endswithr;r@Z	__utils__)ZtgtZtgt_type�kwargsrF�rawrrr�targets�s


rK)r.)rE)�__doc__�loggingZos.pathr#Zsalt.utils.filesr2Zsalt.utils.json�	getLogger�__name__rZTF_OUTPUT_PREFIXrrrr!r)rr;r7r8rKrrrr�<module>s:8
�