
    Vhw                    f    d dl mZ dZdZd dlZd dlmZ d dlmZ	 d dl
mZ d dlmZ  G d	 d
e      Zy)    )annotationsa$  
    name: generator
    version_added: "2.6"
    short_description: Uses Jinja2 to construct hosts and groups from patterns
    description:
        - Uses a YAML configuration file with a valid YAML or C(.config) extension to define var expressions and group conditionals
        - Create a template pattern that describes each host, and then use independent configuration layers
        - Every element of every layer is combined to create a host for every layer combination
        - Parent groups can be defined with reference to hosts and other groups using the same template variables
    options:
      plugin:
         description: token that ensures this is a source file for the 'generator' plugin.
         required: True
         choices: ['ansible.builtin.generator', 'generator']
      hosts:
        description:
          - The C(name) key is a template used to generate
            hostnames based on the C(layers) option. Each variable in the name is expanded to create a
            cartesian product of all possible layer combinations.
          - The C(parents) are a list of parent groups that the host belongs to. Each C(parent) item
            contains a C(name) key, again expanded from the template, and an optional C(parents) key
            that lists its parents.
          - Parents can also contain C(vars), which is a dictionary of vars that
            is then always set for that variable. This can provide easy access to the group name. E.g
            set an C(application) variable that is set to the value of the C(application) layer name.
      layers:
        description:
          - A dictionary of layers, with the key being the layer name, used as a variable name in the C(host)
            C(name) and C(parents) keys. Each layer value is a list of possible values for that layer.
a  
    # inventory.config file in YAML format
    # remember to enable this inventory plugin in the ansible.cfg before using
    # View the output using `ansible-inventory -i inventory.config --list`
    plugin: ansible.builtin.generator
    hosts:
        name: "{{ operation }}_{{ application }}_{{ environment }}_runner"
        parents:
          - name: "{{ operation }}_{{ application }}_{{ environment }}"
            parents:
              - name: "{{ operation }}_{{ application }}"
                parents:
                  - name: "{{ operation }}"
                  - name: "{{ application }}"
              - name: "{{ application }}_{{ environment }}"
                parents:
                  - name: "{{ application }}"
                    vars:
                      application: "{{ application }}"
                  - name: "{{ environment }}"
                    vars:
                      environment: "{{ environment }}"
          - name: runner
    layers:
        operation:
            - build
            - launch
        environment:
            - dev
            - test
            - prod
        application:
            - web
            - api
N)product)	constants)AnsibleParserError)BaseInventoryPluginc                  H     e Zd ZdZdZ fdZ fdZd Zd Zd fd	Z	 xZ
S )	InventoryModulez> constructs groups and vars using Jinja2 template expressions 	generatorc                *    t         t        |           y N)superr	   __init__)self	__class__s    S/home/dcms/DCMS/lib/python3.12/site-packages/ansible/plugins/inventory/generator.pyr   zInventoryModule.__init__W   s    ot-/    c                    d}t         t        |   |      r<t        j                  j                  |      \  }}|r|dgt        j                  z   v rd}|S )NFz.configT)r   r	   verify_fileospathsplitextCYAML_FILENAME_EXTENSIONS)r   r   valid	file_nameextr   s        r   r   zInventoryModule.verify_file[   sP    $3D9WW--d3NIs#)q/I/I!IIr   c                Z    || j                   _        | j                   j                  |      S r   )templaravailable_variablesdo_template)r   pattern	variabless      r   templatezInventoryModule.templatef   s#    +4(||''00r   c           	        |D ]  }	 | j                  |d   |      }||j                  vr|j                  |       |j                  |   }|j                  di       j                         D ]'  \  }}	|j                  || j                  |	|             ) |j                  ||       | j                  |||j                  dg       |        y # t        t        f$ r t        d|d   z        w xY w)Nnamez,Element %s has a parent with no name elementvarsparents)r#   AttributeError
ValueErrorr   groups	add_groupgetitemsset_variable	add_childadd_parents)
r   	inventorychildr'   template_varsparent	groupnamegroupkvs
             r   r0   zInventoryModule.add_parentsj   s     	]Fi MM&.-H	 	 0 00##I.$$Y/E **VR0668 GA""1dmmA}&EFG	51Y	6::i3Lm\	] #J/ i()WZ_`fZg)ghhis   C!C.c           	        t         t        |   ||||       | j                  |      }t	        |d   j                          }|D ]  }t               }t        |d   j                               D ]  \  }	}
||	   ||
<    | j                  |d   d   |      }|j                  |       | j                  |||d   j                  dg       |        y)z parses the inventory file )cachelayershostsr%   r'   N)r   r	   parse_read_config_datar   valuesdict	enumeratekeysr#   add_hostr0   r,   )r   r1   loaderr   r:   configtemplate_inputsitemr3   ikeyhostr   s               r   r=   zInventoryModule.parsex   s     	ot*9fd%*P''-!6(#3#:#:#<=# 	aD FM#F8$4$9$9$;< -3%)!Wc"-==!8-HDt$YfWo.A.A)R.PR_`	ar   )F)__name__
__module____qualname____doc__NAMEr   r   r#   r0   r=   __classcell__)r   s   @r   r	   r	   R   s,    HD0	1]a ar   r	   )
__future__r   DOCUMENTATIONEXAMPLESr   	itertoolsr   ansibler   r   ansible.errorsr   ansible.plugins.inventoryr   r	    r   r   <module>rY      s9    #>"H 
  " - 94a) 4ar   