
    VhE                        d dl mZ dZdZd dlZd dlmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZ 	 d dlZd	Z G d dej*                        Z G d dee	      Zy# e$ r 	 d dlmZ d	Zn# e$ r d
ZY nw xY wY @w xY w)    )annotationsa  
author: Orion Poplawski (@opoplawski)
name: cobbler
short_description: Cobbler inventory source
version_added: 1.0.0
description:
  - Get inventory hosts from the cobbler service.
  - "Uses a configuration file as an inventory source, it must end in C(.cobbler.yml) or C(.cobbler.yaml) and have a C(plugin: cobbler) entry."
  - Adds the primary IP addresses to C(cobbler_ipv4_address) and C(cobbler_ipv6_address) host variables if defined in Cobbler.  The primary IP address is
    defined as the management interface if defined, or the interface who's DNS name matches the hostname of the system, or else the first interface found.
extends_documentation_fragment:
  - inventory_cache
options:
  plugin:
    description: The name of this plugin, it should always be set to V(community.general.cobbler) for this plugin to recognize it as its own.
    type: string
    required: true
    choices: ['cobbler', 'community.general.cobbler']
  url:
    description: URL to cobbler.
    type: string
    default: 'http://cobbler/cobbler_api'
    env:
      - name: COBBLER_SERVER
  user:
    description: Cobbler authentication user.
    type: string
    required: false
    env:
      - name: COBBLER_USER
  password:
    description: Cobbler authentication password.
    type: string
    required: false
    env:
      - name: COBBLER_PASSWORD
  cache_fallback:
    description: Fallback to cached results if connection to cobbler fails.
    type: boolean
    default: false
  connection_timeout:
    description: Timeout to connect to cobbler server.
    type: int
    required: false
    version_added: 10.7.0
  exclude_mgmt_classes:
    description: Management classes to exclude from inventory.
    type: list
    default: []
    elements: str
    version_added: 7.4.0
  exclude_profiles:
    description:
      - Profiles to exclude from inventory.
      - Ignored if O(include_profiles) is specified.
    type: list
    default: []
    elements: str
  include_mgmt_classes:
    description: Management classes to include from inventory.
    type: list
    default: []
    elements: str
    version_added: 7.4.0
  include_profiles:
    description:
      - Profiles to include from inventory.
      - If specified, all other profiles will be excluded.
      - O(exclude_profiles) is ignored if O(include_profiles) is specified.
    type: list
    default: []
    elements: str
    version_added: 4.4.0
  inventory_hostname:
    description:
      - What to use for the ansible inventory hostname.
      - By default the networking hostname is used if defined, otherwise the DNS name of the management or first non-static interface.
      - If set to V(system), the cobbler system name is used.
    type: str
    choices: ['hostname', 'system']
    default: hostname
    version_added: 7.1.0
  group_by:
    description: Keys to group hosts by.
    type: list
    elements: string
    default: ['mgmt_classes', 'owners', 'status']
  group:
    description: Group to place all hosts into.
    default: cobbler
  group_prefix:
    description: Prefix to apply to cobbler groups.
    default: cobbler_
  want_facts:
    description: Toggle, if V(true) the plugin will retrieve all host facts from the server.
    type: boolean
    default: true
  want_ip_addresses:
    description:
      - Toggle, if V(true) the plugin will add a C(cobbler_ipv4_addresses) and C(cobbler_ipv6_addresses) dictionary to the defined O(group) mapping
        interface DNS names to IP addresses.
    type: boolean
    default: true
    version_added: 7.1.0
  facts_level:
    description:
      - "Set to V(normal) to gather only system-level variables."
      - "Set to V(as_rendered) to gather all variables as rolled up by Cobbler."
    type: string
    choices: ['normal', 'as_rendered']
    default: normal
    version_added: 10.7.0
zz
# my.cobbler.yml
plugin: community.general.cobbler
url: http://cobbler/cobbler_api
user: ansible-tester
password: secure
N)AnsibleError)BaseInventoryPlugin	Cacheableto_safe_group_name)	text_type)make_unsafeTFc                  <     e Zd Zej                  f fd	Zd Z xZS )TimeoutTransportc                F    t         t        |           || _        d | _        y N)superr   __init___timeoutcontext)selftimeout	__class__s     o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/inventory/cobbler.pyr   zTimeoutTransport.__init__   s    .0    c                h    t         j                  j                  | |      }| j                  |_        |S r   )xmlrpc_clientSafeTransportmake_connectionr   r   )r   hostconns      r   r   z TimeoutTransport.make_connection   s*    **::4F}}r   )__name__
__module____qualname__socket_GLOBAL_DEFAULT_TIMEOUTr   r   __classcell__r   s   @r   r   r      s    %== 
r   r   c                  b     e Zd ZdZdZ fdZ fdZd Zd Zd Z	d Z
dd	Zd
 Zd fd	Z xZS )InventoryModulez< Host inventory parser for ansible using cobbler as source. zcommunity.general.cobblerc                Z    t         t        |           d | _        t        st        d      y )Nz&Could not import xmlrpc client library)r   r%   r   	cache_keyHAS_XMLRPC_CLIENTr   )r   r   s    r   r   zInventoryModule.__init__   s+    ot-/ GHH !r   c                    d}t         t        |   |      r0|j                  d      rd}|S | j                  j                  d       |S )NF)zcobbler.yamlzcobbler.ymlTzOSkipping due to inventory source not ending in "cobbler.yaml" nor "cobbler.yml")r   r%   verify_fileendswithdisplayvvv)r   pathvalidr   s      r   r*   zInventoryModule.verify_file   sJ    $3D9}}<=    !rsr   c                h    | j                   | j                  vri | j                  | j                   <   y y r   )r'   _cacher   s    r   _init_cachezInventoryModule._init_cache   s)    >>,*,DKK' -r   c                    | j                  d      ra| j                  j                  d       d| j                  d<   | j	                          | j
                  j                  | j                  i        y y )Ncache_fallbackz(Cannot connect to server, loading cache
r   cache_timeout)
get_optionr,   r-   _optionsload_cache_pluginr1   getr'   r2   s    r   _reload_cachezInventoryModule._reload_cache   sW    ??+,LLHI-.DMM/*""$KKOODNNB/	 -r   c                $   | j                   r(d| j                  j                  | j                  i       vry	 | j                  &| j
                  j                  | j                        }n| j
                  j                         }| j                          || j                  | j                     d<   | j                  | j                     d   S # t        j                  t        j                  t        j                  f$ r | j                          Y `w xY w)Nprofiles)	use_cacher1   r:   r'   tokencobblerget_profilesr3   r    gaierrorerrorr   ProtocolErrorr;   )r   datas     r   _get_profileszInventoryModule._get_profiles   s    ~~4;;??4>>SU3V!V	?::)<<44TZZ@D<<446D   ":>DNN+J7{{4>>*:66 OOV\\=3N3NO %""$%s   AC
 
ADDc                b   | j                   r)d| j                  j                  | j                  i       vr	 | j                  &| j
                  j                  | j                        }n| j
                  j                         }| j                  dk(  rt        |      D ]  \  }}| j                  j                  d|d    d       | j                  -| j
                  j                  |d   | j                        ||<   a| j
                  j                  |d         ||<    | j                          || j                  | j                     d<   | j                  | j                     d   S # t        j                  t        j                  t         j"                  f$ r | j%                          Y `w xY w)Nsystemsas_renderedzGathering all facts for name
)r>   r1   r:   r'   r?   r@   get_systemsfacts_level	enumerater,   vvvvget_system_as_renderedr3   r    rB   rC   r   rD   r;   )r   rE   ir   s       r   _get_systemszInventoryModule._get_systems   sa   ~~$++//$..RT2U!U>::)<<33DJJ?D<<335D ##}4#,T? X4)),DT&\NRT*UV::1&*ll&I&I$v,X\XbXb&cDG&*ll&I&I$v,&WDGX   "9=DNN+I6{{4>>*955 OOV\\=3N3NO %""$%s   C*E) )AF.-F.c           	         | j                   j                  t        | j                  d       |j	                         j                  dd                   }|| j                   j                  ||       |S )Ngroup_prefix  )	inventory	add_groupr   r7   lowerreplace	add_child)r   groupchild
group_names       r   _add_safe_group_namez$InventoryModule._add_safe_group_name   s    ^^--.@DOOTbDcCdejepeperezez{~  AC  fD  eE  BF  /G  H
NN$$Z7r   c                R    | j                   r|| j                   vS || j                  v S r   )include_profilesexclude_profiles)r   profiles     r   _exclude_profilez InventoryModule._exclude_profile   s-      $"7"777d3333r   c                   t         t        |   |||       | j                  |       | j	                  d      | _        | j                  j                  d| j
                   d       d| j                  v rAt        j                  | j
                  dt        | j	                  d                  | _        n&t        j                  | j
                  d      | _        d | _        | j	                  d	      Q| j                  j                  t        | j	                  d	            t        | j	                  d
                  | _        | j!                  |      | _        |xr | j	                  d      | _        | j	                  d      | _        | j	                  d      | _        | j	                  d      | _        | j	                  d      | _        | j	                  d      | _        | j	                  d      | _        | j	                  d      | _        | j5                         D ]  }|d   r| j                  j                  d|d    d|d    d       | j7                  |d         rF| j9                  |d         }| j                  j                  d| d       | j7                  |d         r| j9                  |d         }| j                  j                  d| d       | j:                  j=                  ||       | j                  j                  d|d    d       |d   j?                  d      }d}	|	tA        |      dz
  k  s)djC                  |d|	dz          }
djC                  |d|	dz          }| j7                  |
      r!| j                  j                  d|
 d       | j9                  |
      }| j                  j                  d| d       | j9                  |      }| j                  j                  d| d | d       | j:                  j=                  ||       |	dz   }	|	tA        |      dz
  k  r! tE        | j	                  d!            | _#        | jF                  ]| jF                  d"k7  rN| j:                  jI                  | jF                         | j                  j                  d#| jF                   d       i }i }| jK                         D ]B  }| j0                  d$k(  rtM        |d         }ntM        |d%         }|d&   }tO        |d'         tO        | j(                        z  r)| j                  j                  d(|d    d)|d'    d       n| j7                  |d*         r)| j                  j                  d+|d    d,|d*    d       tO        |d'         tO        | j&                        z  r*| j                  j                  d+|d    d)|d'    d       |d"k(  rk|jQ                         D ]X  \  }}|d-   s|d.   r|jS                  d/d       }|&|d"k7  s,tM        |      }| j                  j                  d0| d1| d       Z |d"k(  r$| j                  j                  d2|d    d3       | j:                  jU                  |       | j                  j                  d4|d    d5| d       |d*   d"k7  r9| j9                  |d*   |6      }| j                  j                  d4| d7| d       n| j                  jW                  d8| d9       | j.                  D ]r  }||   d:k(  s||   d"k(  rg }ntY        ||   tZ              r||   gn||   }|D ]:  }| j9                  ||6      }| j                  j                  d4| d;| d<| d       < t | jF                  &| j:                  j=                  | jF                  |       d }d }d }d }|jQ                         D ]  \  }}|d=   d"k7  r!||d=   }|d-   r|d=   }n|d/   |k(  r||d=   }|d>   d"k7  r!||d>   }|d-   r|d>   }n|d/   |k(  r||d>   }| j	                  d?      sj|d/   d"k7  ss|d=   d"k7  r|d=   ||d/   <   |d>   d"k7  s|d>   ||d/   <    |||}|&| j:                  j]                  |d@tM        |             |||}|&| j:                  j]                  |dAtM        |             | j	                  dB      s	 | j:                  j]                  |dCtM        |             E | j	                  d?      ra| j:                  j]                  | jF                  dFtM        |             | j:                  j]                  | jF                  dGtM        |             y y # t^        $ r,}| j                  jW                  dD| dE|        Y d }~d }~ww xY w)HNurlzConnecting to rK   connection_timeoutT)r   )
allow_none	transport)rh   userpasswordcacheexclude_mgmt_classesinclude_mgmt_classesrb   ra   group_byinventory_hostnamerM   parentzProcessing profile rJ   z with parent zAdded profile parent group zAdded profile group z without parent
-r         zExcluding profile zAdded profile child group z to r\   rV   zAdded site group systemhostname
interfacesmgmt_classeszIncluding host z in mgmt_classes rc   zExcluding host z in profile 
managementstaticdns_namezSet hostname to z from z#Cannot determine hostname for host z, skipping
zAdded host z
 hostname )r]   z to profile group zHost z has an empty profile
z<<inherit>>z to group_by z group 
ip_addressipv6_addresswant_ip_addressescobbler_ipv4_addresscobbler_ipv6_address
want_factsr@   zCould not set host info for z: cobbler_ipv4_addressescobbler_ipv6_addresses)0r   r%   parse_read_config_datar7   cobbler_urlr,   rO   r8   r   Serverr   r@   r?   loginr   get_cache_keyr'   r>   rm   rn   rb   ra   ro   rp   rM   rF   rd   r_   rW   r[   splitlenjoinr   r\   rX   rR   r	   setitemsr:   add_hostwarning
isinstancestrset_variable
ValueError)r   rW   loaderr.   rl   rc   parent_group_namer^   profile_elementsrQ   profile_groupprofile_group_childchild_group_nameip_addressesipv6_addressesr   rv   rw   inameivaluethis_dns_namero   groupsr\   r|   ip_address_firstr}   ipv6_address_firster   s                                r   r   zInventoryModule.parse   s	   ot*9fdC 	t$  ??51N4+;+;*<B?@4==0(//0@0@T:JSWSbSbcwSx:y{DL )//0@0@TRDL
??6".++Idoof6M,NPYZ^ZiZijtZuPvwDJ++D1;4??7#;$(OO4J$K!$(OO4J$K! $0B C $0B C
3"&//2F"G??=9))+ 	Gx !!$77HV]^fVgUhhj"kl,,WX->?(,(A(A'(BS(T%LL%%(CDUCVVX&YZ00A%)%>%>wv%O
)),@B*OP001BJO!!$77HHY"Z[#*6?#8#8#= #./!33$'HH-=aA-F$GM*-((3CAa!e3L*M',,];)),>}oR*PQ!%!:!:=!IJLL%%(<ZL&KL'+'@'@AT'U$LL%%(BCSBTTXYcXddf&ghNN,,Z9IJAA #./!33	: ((@A
::!djjB&6NN$$TZZ0LL 1$**R@A%%' d	YD&&(2&tF|4&tJ'78l+J4'(3t/H/H+II!!ODL>ARSWXfSgRhhj"kl((i9LL%%V~\RVW`RaQbbd&eftN+,s43L3L/MMLL%%V~EVW[\jWkVlln&op 2~%/%5%5%7 \ME6l+6(3C(.

:t(D(4"9L'2='AH LL--0@
&QVPWWY.Z[\ 2~!!$GV~Ua"bcNN##H-LLDL>H:RPQ I"$!66tIh6W
!!Kz9KJ<WY"Z[$$uXJ6M%NO !MM l>]2d8n6JF1;DNC1Pd8n-VZ[cVdF# lE!%!:!:5!:!QJLL%%H:]8*T[\f[ggi&jkll zz%((X> J#L!%!+!1!1!3 Vv,'2-'/+1,+?(l+%+L%9

+x7J<N%+L%9
.)R/)1-3N-C*l+'-n'=
+x7L<P'-n'= ??#67j)R/!,/25?El?SL
);<!.1R7?En?UL
);</V4 !&6&B-
%++H6LkZdNef#(:(F1'++H6LkZfNgh|,YNN//)[QUEVWEd	YL ??./NN''

4LkZfNghNN''

4LkZhNij 0 " YLL((+GzQSTUSV)WXXYs   :&f	g!ggr   )T)r   r   r   __doc__NAMEr   r*   r3   r;   rF   rR   r_   rd   r   r"   r#   s   @r   r%   r%      sB    F&DI-07604kk kkr   r%   )
__future__r   DOCUMENTATIONEXAMPLESr    ansible.errorsr   ansible.plugins.inventoryr   r   r   ansible.module_utils.sixr   Aansible_collections.community.general.plugins.plugin_utils.unsafer	   	xmlrpclibr   r(   ImportErrorxmlrpc.clientclientr   r   r%    r   r   <module>r      s    #pd  ' X X . Y"%	33 	~k)9 ~k)  ""-  "!"	"s5   A A3A#"A3#A-*A3,A--A32A3