
    Vhx"                         d dl mZmZmZ eZdZdZd dlm	Z	 d dl
mZmZmZ d dlmZ d dlmZ d dlmZmZ d d	lmZmZmZ d d
lmZ  e       Z G d deee      Zy)    )absolute_importdivisionprint_functiona  
name: robot
author:
  - Oleksandr Stepanov (@alexandrst88)
short_description: Hetzner Robot inventory source
version_added: 1.1.0
description:
  - Reads servers from Hetzner Robot API.
  - Uses a YAML configuration file that ends with C(robot.yml) or C(robot.yaml).
  - The inventory plugin adds all values from U(https://robot.your-server.de/doc/webservice/en.html#get-server) prepended
    with C(hrobot_) to the server's inventory. For example, the variable C(hrobot_dc) contains the data center the server
    is located in.
extends_documentation_fragment:
  - ansible.builtin.constructed
  - ansible.builtin.inventory_cache
  - community.hrobot.robot
  - community.library_inventory_filtering_v1.inventory_filter
notes:
  - The O(hetzner_user) and O(hetzner_password) options can be templated.
options:
  plugin:
    description: Token that ensures this is a source file for the plugin.
    required: true
    choices: ["community.hrobot.robot"]
  hetzner_user:
    env:
      - name: HROBOT_API_USER
  hetzner_password:
    env:
      - name: HROBOT_API_PASSWORD
  simple_filters:
    description:
      - A dictionary of filter value pairs.
      - Available filters are listed here are keys of server like C(status) or C(server_ip).
      - See U(https://robot.your-server.de/doc/webservice/en.html#get-server) for all values that can be used.
      - This option used to be called O(filters) before community.hrobot 2.0.0. It has been renamed from O(filters) to O(simple_filters)
        in community.hrobotdns 1.9.0, and the old name was still available as an alias until community.hrobot 2.0.0. O(filters)
        is now used for something else.
    type: dict
    default: {}
  filters:
    version_added: 2.0.0
a  
---
# Fetch all hosts in Hetzner Robot
plugin: community.hrobot.robot
# Filters all servers in ready state
filters:
  status: ready

---
# Example showing encrypted credentials and using filters
# (This assumes that Mozilla sops was used to encrypt keys/hetzner.sops.yaml, which contains two values
# hetzner_username and hetzner_password. Needs the community.sops collection to decode that file.)
plugin: community.hrobot.robot
hetzner_user: '{{ (lookup("community.sops.sops", "keys/hetzner.sops.yaml") | from_yaml).hetzner_username }}'
hetzner_password: '{{ (lookup("community.sops.sops", "keys/hetzner.sops.yaml") | from_yaml).hetzner_password }}'
filters:
  # Accept all servers in FSN1-DC1 and FSN1-DC2
  - include: >-
      hrobot_dc in ["FSN1-DC1", "FSN1-DC2"]
  # Exclude all servers that did not match any of the above filters
  - exclude: true

---
# Example using constructed features to create groups
plugin: community.hrobot.robot
simple_filters:
  status: ready
  traffic: unlimited
# keyed_groups may be used to create custom groups
strict: false
keyed_groups:
  # Add e.g. groups for every data center
  - key: hrobot_dc
    separator: ""
# Use the IP address to connect to the host
compose:
  server_name_ip: hrobot_server_name ~ '-' ~ hrobot_server_ip
)AnsibleError)BaseInventoryPluginConstructable	Cacheable)Templar)Display)parse_filtersfilter_host)BASE_URLPluginExceptionplugin_open_url_json)make_unsafec                   @     e Zd ZdZ fdZd fd	Zd Zd Zd Z xZ	S )InventoryModulezcommunity.hrobot.robotc                     d}t         t        |   |      r*|j                  d      rd}|S t        j                  d       |S )zO return true/false if this is possibly a valid file for this plugin to consume F)z
robot.yamlz	robot.ymlTzBrobot inventory filename must end with 'robot.yml' or 'robot.yaml')superr   verify_fileendswithdisplaydebug)selfpathvalid	__class__s      l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/hrobot/plugins/inventory/robot.pyr   zInventoryModule.verify_filet   sD    $3D9}}89  bc    c                    t         t        |   |||       i }| j                  |       | j	                          | j                  |      }t        |      | _        | j                  d      }|xr |}|xr | }	|r	 | j                  |   }n|	s| j                         }n	 |	r| j                         }|| j                  |<   | j                  |       y # t        $ r d}	Y @w xY w)N)loadercacheT)r   r   parse_read_config_dataload_cache_pluginget_cache_keyr
   templar
get_option_cacheKeyErrorget_serverspopulate)r   	inventoryr!   r   r"   servers	cache_keyuser_cache_settingattempt_to_read_cachecache_needs_updater   s             r   r#   zInventoryModule.parse   s    ot*9fdCt$ &&t,	f- "__W5 !3 <u/=I !*++i0 $&&(G &&(G &-DKK	"g  *%)"*s   =C C"!C"c                 $   | j                  d      }t        | j                  d            }| j                  d      }g }|D ]  }|d   }|j                  d      xs! |j                  d      xs t        |d         }| j	                  ||      }	|	sRi }
d|v rt        |d         |
d<   |j                         D ]#  \  }}t        |      |
d	j                  d
|      <   % t        | ||
|      s||v r%t        j                  dj                  |             | j                  j                  |       |j                  |       |
j                         D ]"  \  }}| j                  j                  |||       $ | j                  j                  |      j!                         }| j#                  | j                  d      |||       | j%                  | j                  d      |||       | j'                  | j                  d      |||        y )Nsimple_filtersfiltersstrictserverserver_name	server_ipserver_numberansible_hostz{0}_{1}hrobotzTwo of your Hetzner servers use the same server name ({0}). Please make sure that your server names are unique. Only the first server named {0} will be included in the inventory.compose)r6   groupskeyed_groups)r(   r   getstrfilterr   itemsformatr   r   warningr-   add_hostappendset_variableget_hostget_vars_set_composite_vars_add_host_to_composed_groups_add_host_to_keyed_groups)r   r.   r4   r5   r6   server_listsr7   sr8   matchedfactshostvarhostvalkeyvalueserver_varss                   r   r,   zInventoryModule.populate   s   )9:	 :;* #	pFx A%%._!%%2D_AoL^H_Kkk!^4GEa(3AkN(Cn%$%GGI R =H=Qi&&x9:R t[%Al* !eekeklwexz NN##K0,#kkm E
U++KeDE ..11+>GGIK$$T__Y%?kbh$i --dooh.GQ\ek-l **4??>+JFT_hn*oG#	pr   c                 l    d}|j                         D ]  \  }}|j                  |      |k7  sd} |S  |S )NTF)rC   r@   )r   r7   r4   rP   rT   rU   s         r   rB   zInventoryModule.filter   sG    (..0 	JCzz#%'		 r   c                     	 t        | dj                  t              | j                        d   S # t        $ r}t        |j                        d }~ww xY w)Nz
{0}/server)r'   r   )r   rD   r   r'   r   r   error_message)r   es     r   r+   zInventoryModule.get_servers   sM    	0'l.A.A(.KUYUaUabcdee 	0q//	0s   -0 	AAA)T)
__name__
__module____qualname__NAMEr   r#   r,   rB   r+   __classcell__)r   s   @r   r   r   p   s$    #D	%N(pT0r   r   N)
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESansible.errorsr   ansible.plugins.inventoryr   r   r	   ansible.templater
   ansible.utils.displayr   bansible_collections.community.library_inventory_filtering_v1.plugins.plugin_utils.inventory_filterr   r   ?ansible_collections.community.hrobot.plugins.module_utils.robotr   r   r   @ansible_collections.community.hrobot.plugins.plugin_utils.unsafer   r   r    r   r   <module>rm      sd    A @*X%N ( S S $ ) J 
 Y
)l0)=) l0r   