
    VhG-                         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mZ d dlmZ  G d	 d
e      Z	 	 ddZd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionaI
  
module: ipa_host
author: Thomas Krahn (@Nosmoht)
short_description: Manage FreeIPA host
description:
  - Add, modify and delete an IPA host using IPA API.
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  fqdn:
    description:
      - Full qualified domain name.
      - Can not be changed as it is the unique identifier.
    required: true
    aliases: ["name"]
    type: str
  description:
    description:
      - A description of this host.
    type: str
  force:
    description:
      - Force host name even if not in DNS.
    required: false
    type: bool
  ip_address:
    description:
      - Add the host to DNS with this IP address.
    type: str
  mac_address:
    description:
      - List of Hardware MAC address(es) off this host.
      - If option is omitted MAC addresses will not be checked or changed.
      - If an empty list is passed all assigned MAC addresses will be removed.
      - MAC addresses that are already assigned but not passed will be removed.
    aliases: ["macaddress"]
    type: list
    elements: str
  ns_host_location:
    description:
      - Host location (for example V(Lab 2)).
    aliases: ["nshostlocation"]
    type: str
  ns_hardware_platform:
    description:
      - Host hardware platform (for example V(Lenovo T61")).
    aliases: ["nshardwareplatform"]
    type: str
  ns_os_version:
    description:
      - Host operating system and version (for example V(Fedora 9)).
    aliases: ["nsosversion"]
    type: str
  user_certificate:
    description:
      - List of Base-64 encoded server certificates.
      - If option is omitted certificates will not be checked or changed.
      - If an empty list is passed all assigned certificates will be removed.
      - Certificates already assigned but not passed will be removed.
    aliases: ["usercertificate"]
    type: list
    elements: str
  state:
    description:
      - State to ensure.
    default: present
    choices: ["absent", "disabled", "enabled", "present"]
    type: str
  force_creation:
    description:
      - Create host if O(state=disabled) or O(state=enabled) but not present.
    default: true
    type: bool
    version_added: 9.5.0
  update_dns:
    description:
      - If set V(true) with O(state=absent), then removes DNS records of the host managed by FreeIPA DNS.
      - This option has no effect for states other than V(absent).
    type: bool
  random_password:
    description: Generate a random password to be used in bulk enrollment.
    type: bool
extends_documentation_fragment:
  - community.general.ipa.documentation
  - community.general.attributes
a  
- name: Ensure host is present
  community.general.ipa_host:
    name: host01.example.com
    description: Example host
    ip_address: 192.168.0.123
    ns_host_location: Lab
    ns_os_version: CentOS 7
    ns_hardware_platform: Lenovo T61
    mac_address:
      - "08:00:27:E3:B1:2D"
      - "52:54:00:BD:97:1E"
    state: present
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret

- name: Generate a random password for bulk enrolment
  community.general.ipa_host:
    name: host01.example.com
    description: Example host
    ip_address: 192.168.0.123
    state: present
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret
    random_password: true

- name: Ensure host is disabled
  community.general.ipa_host:
    name: host01.example.com
    state: disabled
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret

- name: Ensure that all user certificates are removed
  community.general.ipa_host:
    name: host01.example.com
    user_certificate: []
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret

- name: Ensure host is absent
  community.general.ipa_host:
    name: host01.example.com
    state: absent
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret

- name: Ensure host and its DNS record is absent
  community.general.ipa_host:
    name: host01.example.com
    state: absent
    ipa_host: ipa.example.com
    ipa_user: admin
    ipa_pass: topsecret
    update_dns: true
z
host:
  description: Host as returned by IPA API.
  returned: always
  type: dict
host_diff:
  description: List of options that differ and would be changed.
  returned: if check mode and a difference is found
  type: list
N)AnsibleModule)	IPAClientipa_argument_spec)	to_nativec                   B     e Zd Z fdZd Zd Zd Zd Zd Zd Z	 xZ
S )HostIPAClientc                 2    t         t        |   ||||       y )N)superr   __init__)selfmodulehostportprotocol	__class__s        n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/ipa_host.pyr   zHostIPAClient.__init__   s    mT+FD$I    c                 (    | j                  d|      S )N	host_showmethodname
_post_jsonr   r   s     r   r   zHostIPAClient.host_show   s    k==r   c                 0    | j                  dd d|d      S )N	host_findT)allfqdnr   r   itemr   r   s     r   r    zHostIPAClient.host_find   s    k4Y]C^__r   c                 *    | j                  d||      S )Nhost_addr#   r   r   r   r   s      r   r&   zHostIPAClient.host_add       jt$GGr   c                 *    | j                  d||      S )Nhost_modr#   r   r'   s      r   r*   zHostIPAClient.host_mod   r(   r   c                 .    | j                  d|d|i      S )Nhost_del	updatednsr#   r   )r   r   
update_dnss      r   r,   zHostIPAClient.host_del   s    jt;PZB[\\r   c                 (    | j                  d|      S )Nhost_disabler   r   r   s     r   r0   zHostIPAClient.host_disable   s    n4@@r   )__name__
__module____qualname__r   r   r    r&   r*   r,   r0   __classcell__)r   s   @r   r   r      s,    J>`HH]Ar   r   c	                     i }	| | |	d<   |||	d<   |||	d<   |||	d<   |||	d<   |||	d<   ||D 
cg c]  }
d|
i c}
|	d<   |||	d	<   |||	d
<   |	S c c}
w )Ndescriptionforce
ip_addressnshostlocationnshardwareplatformnsosversion
__base64__usercertificate
macaddressrandom )r6   r7   r8   ns_host_locationns_hardware_platformns_os_versionuser_certificatemac_addressrandom_passworddatar$   s              r   get_host_dictrH      s    D)]W'\#!1'%9!" +]#DT"UDL$#7"U(\"(XK #Vs   Ac                     ddg}|j                  d      s|j                  d       |D ]
  }||v s||=  | j                  ||      S )Nr7   r8   r?   )ipa_datamodule_data)getappendget_diff)clientipa_hostmodule_hostnon_updateable_keyskeys        r   get_host_diffrT      s[    "L1??8$""8," !+C ! ??H+?FFr   c                    | j                   d   }| j                   d   }| j                   d   }|j                  |      }t        | j                   d   | j                   d   | j                   d   | j                   d   | j                   d	   | j                   d
   | j                   d   | j                   d   | j                   d   	      }d}|dv r%|s+|s|dk(  r$d}| j                  sQ||j	                  ||      fS |dv r| j                  d|z   dz          t        |||      }t        |      dkD  rd}| j                  si }	|D ]  }
|j                  |
      |	|
<    d|	vr&|j                  dg       D cg c]  }|d   	 c}|	d<   |j                  |      }|j                  dd      r2|dk(  s| j                   j                  d      r|j                  |       ||j                  ||	      fS |dk(  r?|r=d}| j                   j                  dd      }| j                  s|j                  ||       ||j                  |      fS c c}w ) Nr"   stateforce_creation)r   r6   r7   r8   rA   rB   rC   rD   rE   rF   )	r6   r7   r8   rA   rB   rC   rD   rE   rF   F)presentenableddisabledrX   T)r   r   )rZ   rY   zNo host with name z found)msgr   r=   r<   
has_keytabrZ   absentr.   )r   r.   )paramsr    rH   
check_moder&   	fail_jsonrT   lenrL   r   r0   r*   r,   )r   rO   r   rV   rW   rP   rQ   changeddiffrG   rS   certipa_host_showr.   s                 r   ensurerf      sk   == DMM'"E]]#34NT*HFMM-,H&,mmG&<+1==+F17?Q1R5;]]CY5Z.4mmO.L17?Q1R,2MM-,H06>O0P	"K G22^u	/AG$$ T LLL//  %9H%Dx%O P ;?D4y1}((D# 9$/OOC$8S	9(4;C<<HY[];^337D.3./ %+$4$4$$4$?M$((t<%:BUY_YfYfYjYjk|Y}+++6"FOODO$III	(	G**<?J$$TjAF$$$$///3s   8Ic                  0   t               } | j                  t        d      t        dddg      t        d      t        d      t        ddg      t        dd	g      t        dd
g      t        ddgd      t        ddgd      t        d      t        ddg d      t        dd      t        dd             t        | d      }t	        ||j
                  d   |j
                  d   |j
                  d         }	 |j                  |j
                  d   |j
                  d          t        ||      \  }}|j                  ||       y # t        $ r8}|j                  t        |      t        j                                Y d }~y d }~ww xY w) Nstr)typeTr   )ri   requiredaliasesboolr9   )ri   rk   r:   r;   listr=   )ri   rk   elementsr>   rX   )rX   r]   rY   rZ   )ri   defaultchoicesF)ri   no_log)ri   ro   )r6   r"   r7   r8   rA   rB   rC   rD   rE   r.   rV   rF   rW   )argument_specsupports_check_moderP   ipa_portipa_prot)r   r   r   r   ipa_useripa_pass)usernamepassword)rb   r   )r[   	exception)r   updatedictr   r   r^   loginrf   	exit_json	Exceptionr`   r	   	traceback
format_exc)rr   r   rO   rb   r   es         r   mainr   #  s}   %'MTu%5"vhO#0$(e$4*.EDTCU*V.2H\G].^'+'P*.FEVDWbg*h%)v~X]%^$(f$5#	Kwx)-6%)H(,&$(G  J /35F & &j 9 &j 9$*MM*$=?F
MfmmJ7$mmJ7 	 	9vv.t4 MYq\Y5I5I5KLLMs   AE 	F.FF__main__)	NNNNNNNNN)
__future__r   r   r   ri   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   ansible.module_utils.basicr   >ansible_collections.community.general.plugins.module_utils.ipar   r   +ansible.module_utils.common.text.convertersr	   r   rH   rT   rf   r   r1   r@   r   r   <module>r      s}    A @Xt<|	
  4 g AAI A. nr_c0G20jMB zF r   