File: //usr/lib/python3.9/site-packages/ansible/plugins/inventory/__pycache__/script.cpython-39.pyc
a
�)g� � @ s� d dl mZmZmZ eZdZd dlZd dlZd dl m
Z
d dlmZm
Z
d dlmZ d dlmZmZ d dlmZ d d lmZ e� ZG d
d� de�ZdS )� )�absolute_import�division�print_functiona�
name: script
version_added: "2.4"
short_description: Executes an inventory script that returns JSON
options:
always_show_stderr:
description: Toggle display of stderr even when script was successful
version_added: "2.5.1"
default: True
type: boolean
ini:
- section: inventory_plugin_script
key: always_show_stderr
env:
- name: ANSIBLE_INVENTORY_PLUGIN_SCRIPT_STDERR
description:
- The source provided must be an executable that returns Ansible inventory JSON
- The source must accept C(--list) and C(--host <hostname>) as arguments.
C(--host) will only be used if no C(_meta) key is present.
This is a performance optimization as the script would be called per host otherwise.
notes:
- Enabled in configuration by default.
- The plugin does not cache results because external inventory scripts are responsible for their own caching.
N)�Mapping)�AnsibleError�AnsibleParserError)�json_dict_bytes_to_unicode)� to_native�to_text)�BaseInventoryPlugin)�Displayc sN e Zd ZdZdZ� fdd�Z� fdd�Zd� fdd � Zd
d� Zdd
� Z � Z
S )�InventoryModulezE Host inventory parser for ansible using external inventory scripts. Zscriptc s t t| ��� t� | _d S �N)�superr
�__init__�set�_hosts)�self�� __class__� �D/usr/lib/python3.9/site-packages/ansible/plugins/inventory/script.pyr 4 s zInventoryModule.__init__c s� t t| ��|�}|r�d}zFt|d��(}|�d�}|�d�r>d}W d � n1 sR0 Y W n typ Y n0 t�|tj �s�|s�d}|S )zP Verify if file is usable by this plugin, base does minimal accessibility check F�rb� s #!TN)
r r
�verify_file�open�read�
startswith� Exception�os�access�X_OK)r �pathZvalidZshebang_presentZinv_fileZ
initial_charsr r r r : s
&zInventoryModule.verify_fileNc s� t t| ��|||� | �� |dg}�zXztj|tjtjd�}W n> ty~ } z&tdd� |�t
|�f ��W Y d }~n
d }~0 0 |�� \}} t
|�}t
| p�d�}
|
r�|
�d�s�|
d7 }
|j
dkr�td||
f ��zt|d d
�}W n: t�y } z td�|t
|����W Y d }~n
d }~0 0 z| jj|dd
�}W n< t�yl } z"td�|t
|�|
���W Y d }~n
d }~0 0 | �r�| �d��r�| jjt|
�d� t|t��s�td�||
���d }
d }|�� D ]4\}
}|
dk�r�d|v �r�|d }n| �|
|� �q�| jD ]�}i }|d u �r| �||�}nPz|�|i �}W n> t�yh } z$td|t
|�f |d��W Y d }~n
d }~0 0 | �|g|� �q�W n2 t�y� } ztt
|���W Y d }~n
d }~0 0 d S )Nz--list��stdout�stderr�problem running %s (%s)� � �
r z1Inventory script (%s) had an execution error: %s �strict)�errorszKInventory {0} contained characters that cannot be interpreted as UTF-8: {1}T)Z json_onlyzEfailed to parse executable inventory script results from {0}: {1}
{2}Zalways_show_stderr)�msgzYfailed to parse executable inventory script results from {0}: needs to be a json dict
{1}�_metaZhostvarsz0Improperly formatted host information for %s: %s)Zorig_exc) r r
�parseZset_options�
subprocess�Popen�PIPE�OSErrorr �joinr �communicate�endswith�
returncoder r
r �format�loader�loadZ
get_option�display�error�
isinstancer �items�_parse_groupr �get_host_variables�get�AttributeErrorZ_populate_host_vars)r � inventoryr8 r"