
    Vh                      ~    d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZ d dlmZ  e       Zd	 Z G d
 de      Zy)    )absolute_importdivisionprint_functionaU  
author:
  - Roy Lenferink (@rlenferink)
  - Mark Ettema (@m-a-r-k-e)
  - Alexander Petrenz (@alpex8)
name: merge_variables
short_description: merge variables whose names match a given pattern
description:
  - This lookup returns the merged result of all variables in scope that match the given prefixes, suffixes, or regular expressions,
    optionally.
version_added: 6.5.0
options:
  _terms:
    description:
      - Depending on the value of O(pattern_type), this is a list of prefixes, suffixes, or regular expressions that will
        be used to match all variables that should be merged.
    required: true
    type: list
    elements: str
  pattern_type:
    description:
      - Change the way of searching for the specified pattern.
    type: str
    default: 'regex'
    choices:
      - prefix
      - suffix
      - regex
    env:
      - name: ANSIBLE_MERGE_VARIABLES_PATTERN_TYPE
    ini:
      - section: merge_variables_lookup
        key: pattern_type
  initial_value:
    description:
      - An initial value to start with.
    type: raw
  override:
    description:
      - Return an error, print a warning or ignore it when a key will be overwritten.
      - The default behavior V(error) makes the plugin fail when a key would be overwritten.
      - When V(warn) and V(ignore) are used, note that it is important to know that the variables are sorted by name before
        being merged. Keys for later variables in this order will overwrite keys of the same name for variables earlier in
        this order. To avoid potential confusion, better use O(override=error) whenever possible.
    type: str
    default: 'error'
    choices:
      - error
      - warn
      - ignore
    env:
      - name: ANSIBLE_MERGE_VARIABLES_OVERRIDE
    ini:
      - section: merge_variables_lookup
        key: override
  groups:
    description:
      - Search for variables across hosts that belong to the given groups. This allows to collect configuration pieces across
        different hosts (for example a service on a host with its database on another host).
    type: list
    elements: str
    version_added: 8.5.0
a  
# Some example variables, they can be defined anywhere as long as they are in scope
test_init_list:
  - "list init item 1"
  - "list init item 2"

testa__test_list:
  - "test a item 1"

testb__test_list:
  - "test b item 1"

testa__test_dict:
  ports:
    - 1

testb__test_dict:
  ports:
    - 3

# Merge variables that end with '__test_dict' and store the result in a variable 'example_a'
example_a: "{{ lookup('community.general.merge_variables', '__test_dict', pattern_type='suffix') }}"

# The variable example_a now contains:
# ports:
#   - 1
#   - 3

# Merge variables that match the '^.+__test_list$' regular expression, starting with an initial value and store the
# result in a variable 'example_b'
example_b: "{{ lookup('community.general.merge_variables', '^.+__test_list$', initial_value=test_init_list) }}"

# The variable example_b now contains:
#   - "list init item 1"
#   - "list init item 2"
#   - "test a item 1"
#   - "test b item 1"
z
_raw:
  description: In case the search matches list items, a list will be returned. In case the search matches dicts, a dict will
    be returned.
  type: raw
  elements: raw
N)AnsibleError)
LookupBase)Displayc                 \    t        | t              ryt        | t              ryt        d      )Nlistdictz>Not supported type detected, variable must be a list or a dict)
isinstancer
   r   r   )variables    t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/lookup/merge_variables.py_verify_and_get_typer      s(    (D!	Hd	#[\\    c                   ,    e Zd ZddZd Zd Zd Zd Zy)LookupModuleNc                 x   | j                  |       | j                  dd       }| j                  dd      | _        | j                  dd      | _        | j                  dd       | _        g }|D ]  }t        |t              st        dt        |       d	      | j                  s#|j                  | j                  |||             Z|}|d
   D ]U  }| j                  |d
   |   d         st        |d
   j                  |            }	|d
   |	d
<   | j                  |||	      }W |j                  |        |S )N)directinitial_valueoverrideerrorpattern_typeregexgroupszNon-string type 'z'' passed, only 'str' types are allowed!hostvarsgroup_names)set_options
get_option	_override_pattern_type_groupsr   strr   typeappend_merge_vars_is_host_in_allowed_groupsr   raw_get)
selfterms	variableskwargsr   rettermcross_host_merge_resulthosthost_variabless
             r   runzLookupModule.run   sD   '>W=!__^WEx6 	4DdC("%6tDzlBi#jkk<<

4++D-KL*7'%j1 rD66y7LT7RS`7ab)-i
.C.K.KD.Q)R5>z5Jz2262B2B4I`bp2q/	r
 

23	4 
r   c                 n    d| j                   v ry|D cg c]  }|| j                   v s| }}|ryyc c}w )NallTF)r!   )r(   host_groupshost_group_namegroup_intersections       r   r&   z'LookupModule._is_host_in_allowed_groups   sC    DLL EPt/TcgkgsgsTsott	 us   22c                     | j                   dk(  r|j                  |      S | j                   dk(  r|j                  |      S | j                   dk(  r&t        j                  |      }|j                  |      S y)Nprefixsuffixr   F)r    
startswithendswithrecompilesearch)r(   keysearch_patternmatchers       r   _var_matcheszLookupModule._var_matches   sj    )>>.118+<<//7*jj0G>>#&&r   c           	      X   t         j                  d| j                   d|        t        |j	                         D cg c]  }| j                  ||      s| c}      }t         j                  d|        d }d }|t        |      }|}|D ]  }| j                  j                  |      5  | j                  j                  ||         }	d d d        t        	      }
||
}n||
k7  rt        d      ||	}k|
dk(  r| j                  |	||g      }||	z  } |S c c}w # 1 sw Y   VxY w)NzMerge variables with z: z(The following variables will be merged: )available_variablesz7Unable to merge, not all variables are of the same typer   )displayvvvr    sortedkeysrB   r   _templarset_temporary_contexttemplater   _merge_dict)r(   r@   r   r*   r?   var_merge_namesprev_var_typeresultvar_name	var_valuevar_types              r   r%   zLookupModule._merge_vars   sF   +D,>,>+?r.AQRS 1A!l#TEVEVWZ\jEk#!lm>>OPQ$0?M"F' 	$H444S H MM229X3FG	H+I6H$ ((*"#\]]~"6!)))VhZH)##	$& 9 "mH Hs   DD,D  D)	c                    |j                         D ]  \  }}t        |t              r*|j                  |i       }| j	                  ||||gz          @t        |t
              r||v r||xx   |z  cc<   b||v rj||   |k7  rbd| d||    d| ddj                  |       d| d}| j                  dk(  rt        |      | j                  dk(  rt        j                  |       |||<    |S )	Nz	The key 'z' with value 'z"' will be overwritten with value 'z' from '.'r   warn)itemsr   r   
setdefaultrL   r
   joinr   r   rE   warning)r(   srcdestpathr?   valuenodemsgs           r   rL   zLookupModule._merge_dict   s   ))+ 	"JC%&sB/  dcUl;E4(SD[S	U"	4KT#Y%%7%cU.cCefkelltuxu}u}  C  vD  uE  EF  GJ  FK  KL  MC~~0*3//~~/,!S		"" r   )N)__name__
__module____qualname__r1   r&   rB   r%   rL    r   r   r   r      s    2	@r   r   )
__future__r   r   r   r#   __metaclass__DOCUMENTATIONEXAMPLESRETURNr<   ansible.errorsr   ansible.plugins.lookupr   ansible.utils.displayr   rE   r   r   rd   r   r   <module>rm      sS    C B>@%N
 
 ' - )
)]a: ar   