
    Vh                        d dl m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 d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ 	 d dlZd dlmZ eZ	 d dlZ G d de      Zy# e$ r Y w xY w# e$ r Y w xY w)    )annotationsa8  
name: reverse_lookup
author: Felix Fontein (@felixfontein)
short_description: Reverse-look up IP addresses
version_added: 3.1.0
requirements:
  - dnspython >= 1.15.0 (maybe older versions also work)
description:
  - Look up hostnames for IP addresses using DNS reverse lookup.
options:
  _terms:
    description:
      - IP address(es) to look up.
    type: list
    elements: str
    required: true
  query_retry:
    description:
      - Number of retries for DNS query timeouts.
    type: int
    default: 3
  query_timeout:
    description:
      - Timeout per DNS query in seconds.
    type: float
    default: 10
  server:
    description:
      - The DNS server(s) to use to look up the result. Must be a list of one or more IP addresses.
      - By default, the system's standard resolver is used.
    type: list
    elements: str
  servfail_retries:
    description:
      - How often to retry on SERVFAIL errors.
    type: int
    default: 0
notes:
  - Note that when using this lookup plugin with V(lookup(\)), and the result is a one-element list, Ansible simply returns
    the one element not as a list. Since this behavior is surprising and can cause problems, it is better to use V(query(\))
    instead of V(lookup(\)). See the examples and also R(Forcing lookups to return lists, query) in the Ansible documentation.
a  
- name: Look up hostname of IPv4 address
  ansible.builtin.debug:
    msg: "{{ query('community.dns.reverse_lookup', '192.168.1.1') }}"

- name: Look up hostname of IPv6 address
  ansible.builtin.debug:
    msg: "{{ query('community.dns.reverse_lookup', '1:2:3::4') }}"
a  
_result:
  description:
    - The hostname(s) returned for the queried IP addresses.
    - If multiple IP addresses are queried in O(_terms), the resulting lists have been concatenated.
  type: list
  elements: str
  sample:
    - example.com
    - example.org
N)AnsibleLookupError)to_text)
LookupBase)is_ip_address)SimpleResolver)assert_requirements_present)guarded_run)	RdataTypec                  j    e Zd Ze	 	 	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 dd       Z	 d	 	 	 	 	 	 	 ddZy)	LookupModulec                @     d fd}t        |t              S )Nc                 v    j                  ddd      } | sg S | D cg c]  }t        |       c}S c c}w )NTF)rdtypeserver_addressesnxdomain_is_emptytarget_can_be_relativesearch)resolver   )rrsetdatanamer   resolverr   s     o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/dns/plugins/lookup/reverse_lookup.pycallbackz'LookupModule._resolve.<locals>.callbackw   sL    $$!1"&', % E 	.34dGDM444s   6error_classserver)return	list[str])r
   r   )r   r   r   r   r   s   ```` r   _resolvezLookupModule._resolvep   s%    	5 	5 *
 	
    c                      fdS )Nc                 &     j                        S N)resolve_addressesr   r   s   r   <lambda>z,LookupModule._get_resolver.<locals>.<lambda>   s    x11&9 r"    r'   s   ``r   _get_resolverzLookupModule._get_resolver   s     :9r"   Nc           	     N   t        dd       t        dd       | j                  ||       t        | j	                  d      | j	                  d      | j	                  d            }d }| j	                  d      rhg }| j	                  d      }|D ]P  }t        |      r|j                  |        |j                  t        | j                  ||      t        |	             R g }|D ]V  }		 t        j                  t        |	            }
|
j                  }|j                  d
      s|d
z  }n	 |j                  |       X g }|D ]=  }|j                  | j#                  ||t$        j&                  j(                  |             ? |S # t         $ r}t        d|	d|       d }~ww xY w)Nzcommunity.dns.reverse_lookuplookup)var_optionsdirectquery_timeoutquery_retryservfail_retries)timeouttimeout_retriesr1   r   r   .zCannot parse IP address z: )%assert_requirements_present_dnspython%assert_requirements_present_ipaddressset_optionsr   
get_optionr   appendextendr
   r*   r   	ipaddress
ip_addressr   reverse_pointerendswith	Exceptionr!   dns	rdatatypePTR)selfterms	variableskwargsr   r   serversr   ip_adressesr<   ipaddrr   eresults                 r   runzLookupModule.run   s    	..LhW-.LhWYv>!OOO4 OOM:!__-?@
 .2??8$!!%!:G! 
 ($++F3 ''**8V<$6%	
  
	YJ	Y"--gj.AB--}}S)CKD""4(
	Y  	DMMhcmm.?.?AQR	   Y(+CJ>QSTUSV)WXXYs   *AF	F$FF$)
r   r   r   strr   r   r   zlist[str] | Noner   r    )r   r   r   rM   r   zt.Callable[[], list[str]]r%   )rD   zlist[t.Any]rE   zt.Any | NonerF   zt.Anyr   r    )__name__
__module____qualname__staticmethodr!   r*   rL   r)   r"   r   r   r   o   s    
 

 
 +	

 

 
2 : :*-:	": : =A0 0-90LQ0	0r"   r   )
__future__r   DOCUMENTATIONEXAMPLESRETURNtypingtansible.errorsr   +ansible.module_utils.common.text.convertersr   ansible.plugins.lookupr   :ansible_collections.community.dns.plugins.module_utils.ipsr   ?ansible_collections.community.dns.plugins.module_utils.resolverr   :ansible_collections.community.dns.plugins.plugin_utils.ipsr	   r6   ?ansible_collections.community.dns.plugins.plugin_utils.resolverr5   r
   dns.resolverr@   dns.rdatatyper   intImportErrorr;   r   r)   r"   r   <module>rc      s    #)V

  - ? - T
'
 I	Q: Q  		  		s$   
A A* A'&A'*A21A2