관리-도구
편집 파일: generator.cpython-39.pyc
a �)g� � @ sl d dl mZmZmZ eZdZdZd dlZd dl m Z d dlmZ d dlmZ d dlmZ G d d � d e�ZdS )� )�absolute_import�division�print_functiona$ 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 sN e Zd ZdZdZ� fdd�Z� fdd�Zdd� Zd d � Zd� fdd � Z � Z S )�InventoryModulez> constructs groups and vars using Jinja2 template expressions � generatorc s t t| ��� d S �N)�superr �__init__)�self�� __class__� �G/usr/lib/python3.9/site-packages/ansible/plugins/inventory/generator.pyr X s zInventoryModule.__init__c s@ d}t t| ��|�r<tj�|�\}}|r8|dgtj v r<d}|S )NFz.configT)r r �verify_file�os�path�splitext�CZYAML_FILENAME_EXTENSIONS)r r Zvalid� file_name�extr r r r \ s zInventoryModule.verify_filec C s || j _| j �|�S r )ZtemplarZavailable_variablesZdo_template)r �patternZ variablesr r r �templateg s zInventoryModule.templatec C s� |D ]�}z| � |d |�}W n& ttfyB td|d ��Y n0 ||jvrX|�|� |j| }|�di ��� D ]\}} |�|| � | |�� qr|� ||� | � |||�dg �|� qd S )N�namez,Element %s has a parent with no name element�vars�parents)r �AttributeError� ValueErrorr �groups� add_group�get�itemsZset_variableZ add_child�add_parents) r � inventory�childr � template_vars�parentZ groupname�group�k�vr r r r% k s zInventoryModule.add_parentsFc s� t t| �j||||d� | �|�}t|d �� � }|D ]j}t� }t|d �� �D ]\} } || || <