
    Vh#                        d dl mZ dZdZdZd dlZd dlmZmZ d dl	m
Z
 d dlmZmZ d d	lmZ d d
lmZ d dlmZ d ZddZ G d de      Zy)    )annotationsay	  
    name: first_found
    author: Seth Vidal (!UNKNOWN) <skvidal@fedoraproject.org>
    version_added: historical
    short_description: return first file found from list
    description:
      - This lookup checks a list of files and paths and returns the full path to the first combination found.
      - As all lookups, when fed relative paths it will try use the current task's location first and go up the chain
        to the containing locations of role / play / include and so on.
      - The list of files has precedence over the paths searched.
        For example, A task in a role has a 'file1' in the play's relative path, this will be used, 'file2' in role's relative path will not.
      - Either a list of files O(_terms) or a key O(files) with a list of files is required for this plugin to operate.
    notes:
      - This lookup can be used in 'dual mode', either passing a list of file names or a dictionary that has O(files) and O(paths).
    options:
      _terms:
        description: A list of file names.
      files:
        description: A list of file names.
        type: list
        elements: string
        default: []
      paths:
        description: A list of paths in which to look for the files.
        type: list
        elements: string
        default: []
      skip:
        type: boolean
        default: False
        description:
          - When V(True), return an empty list when no files are matched.
          - This is useful when used with C(with_first_found), as an empty list return to C(with_) calls
            causes the calling task to be skipped.
          - When used as a template via C(lookup) or C(query), setting O(skip=True) will *not* cause the task to skip.
            Tasks must handle the empty list return from the template.
          - When V(False) and C(lookup) or C(query) specifies O(ignore:errors='ignore') all errors (including no file found,
            but potentially others) return an empty string or an empty list respectively.
          - When V(True) and C(lookup) or C(query) specifies O(ignore:errors='ignore'), no file found will return an empty
            list and other potential errors return an empty string or empty list depending on the template call
            (in other words return values of C(lookup) vs C(query)).
    seealso:
      - ref: playbook_task_paths
        description: Search paths used for relative paths/files.
a	  
- name: Set _found_file to the first existing file, raising an error if a file is not found
  ansible.builtin.set_fact:
    _found_file: "{{ lookup('ansible.builtin.first_found', findme) }}"
  vars:
    findme:
      - /path/to/foo.txt
      - bar.txt  # will be looked in files/ dir relative to role and/or play
      - /path/to/biz.txt

- name: Set _found_file to the first existing file, or an empty list if no files found
  ansible.builtin.set_fact:
    _found_file: "{{ lookup('ansible.builtin.first_found', files, paths=['/extra/path'], skip=True) }}"
  vars:
    files:
      - /path/to/foo.txt
      - /path/to/bar.txt

- name: Include tasks only if one of the files exist, otherwise skip the task
  ansible.builtin.include_tasks:
    file: "{{ item }}"
  with_first_found:
    - files:
      - path/tasks.yaml
      - path/other_tasks.yaml
      skip: True

- name: Include tasks only if one of the files exists, otherwise skip
  ansible.builtin.include_tasks: '{{ tasks_file }}'
  when: tasks_file != ""
  vars:
    tasks_file: "{{ lookup('ansible.builtin.first_found', files=['tasks.yaml', 'other_tasks.yaml'], errors='ignore') }}"

- name: |
        copy first existing file found to /some/file,
        looking in relative directories from where the task is defined and
        including any play objects that contain it
  ansible.builtin.copy:
    src: "{{ lookup('ansible.builtin.first_found', findme) }}"
    dest: /some/file
  vars:
    findme:
      - foo
      - "{{ inventory_hostname }}"
      - bar

- name: same copy but specific paths
  ansible.builtin.copy:
    src: "{{ lookup('ansible.builtin.first_found', params) }}"
    dest: /some/file
  vars:
    params:
      files:
        - foo
        - "{{ inventory_hostname }}"
        - bar
      paths:
        - /tmp/production
        - /tmp/staging

- name: INTERFACES | Create Ansible header for /etc/network/interfaces
  ansible.builtin.template:
    src: "{{ lookup('ansible.builtin.first_found', findme)}}"
    dest: "/etc/foo.conf"
  vars:
    findme:
      - "{{ ansible_virtualization_type }}_foo.conf"
      - "default_foo.conf"

- name: read vars from first file found, use 'vars/' relative subdir
  ansible.builtin.include_vars: "{{lookup('ansible.builtin.first_found', params)}}"
  vars:
    params:
      files:
        - '{{ ansible_distribution }}.yml'
        - '{{ ansible_os_family }}.yml'
        - default.yml
      paths:
        - 'vars'
zW
  _raw:
    description:
      - path to file found
    type: list
    elements: path
N)MappingSequence)UndefinedError)AnsibleLookupErrorAnsibleUndefinedVariable)string_types)
LookupBase)unfrackpathc              #  \   K   t        |      }d}| D ]  }||v r| d}||z  } | y w)N )set)valuecharsvcs       R/home/dcms/DCMS/lib/python3.12/site-packages/ansible/plugins/lookup/first_found.py	_splitterr      sG     JE
A :GA	Q Gs   *,c                    g }t        | t              rt        t        | |            }|S | D ]  }|j	                  t        ||              |S )N)
isinstancer	   listr   extend	_split_on)termsspliterstermlistts       r   r   r      sR    H%&	%23
 O  	4AOOIa23	4O    c                      e Zd Zd Zd Zy)LookupModulec                   g }d}|D ]A  }t        |t              r%| j                  ||       | j                  d      }nft        |t              r|g}nRt        |t
              r(| j                  |||      \  }}|j                  |       t        dt        |      d|      | j                  d      }	| j                  d      }t        |d      }
t        |	d	      }|rA|D ]:  }|
D ]3  }t        j                  j                  ||      }|j                  |       5 < |
r|j                  |
       1|j                  |       D ||fS )
NFvar_optionsdirectfileszNInvalid term supplied, can handle string, mapping or list of strings but got: z for pathsskipz,;z,:;)r   r   set_options
get_optionr	   r   _process_termsr   r   typer   ospathjoinappend)selfr   	variableskwargstotal_searchr'   termr%   partialr&   filelistpathlistr-   fnfs                  r   r*   zLookupModule._process_terms   sj      	*D$(  Yt D0D,/D(+ $ 3 3D)V L##G,(  GK  LP  GQ  SW  *X  Y  YOOG,E ??6*D !-H .H $ /D& /GGLLr2$++A.// ##H-##D)A 	*D T!!r   c                t   | j                  ||       |s| j                  d      }| j                  |||      \  }}t        | dd      }d }|D ]E  }	 | j                  j                  |      }| j                  |||d      }|7t        |d      gc S  |rg S t        d      # t        t        f$ r Y hw xY w)	Nr"   r%   _subdirT)ignore_missingF)followz)No file was found when using first_found.)r(   r)   r*   getattr_templartemplater   r   find_file_in_search_pathr   r   )	r0   r   r1   r2   r3   r'   subdirr-   r8   s	            r   runzLookupModule.run   s    Yv>OOG,E!00	6Jd y'2 	9B]]++B/ 00FBW[0\D #D788	9" I !LMM! -n=  s   B%%B76B7N)__name__
__module____qualname__r*   rC    r   r   r    r       s    ("T%Nr   r    ),)
__future__r   DOCUMENTATIONEXAMPLESRETURNr,   collections.abcr   r   jinja2.exceptionsr   ansible.errorsr   r   ansible.module_utils.sixr	   ansible.plugins.lookupr
   ansible.utils.pathr   r   r   r    rG   r   r   <module>rS      sS    #,\Ob
 
 - , G 1 - *	QN: QNr   