
    VhV:                        d dl mZ dZdZd dlZd dlZd dlmZmZ d dl	m
Z
mZ d dlmZmZ d dlmZmZmZ d d	lmZ  e       Zd
 Z G d deee      Zy)    )annotationsa@  
name: iocage
short_description: iocage inventory source
version_added: 10.2.0
author:
  - Vladimir Botka (@vbotka)
requirements:
  - iocage >= 1.8
description:
  - Get inventory hosts from the iocage jail manager running on O(host).
  - By default, O(host) is V(localhost). If O(host) is not V(localhost) it
    is expected that the user running Ansible on the controller can
    connect to the O(host) account O(user) with SSH non-interactively and
    execute the command C(iocage list).
  - Uses a configuration file as an inventory source, it must end
    in C(.iocage.yml) or C(.iocage.yaml).
extends_documentation_fragment:
  - ansible.builtin.constructed
  - ansible.builtin.inventory_cache
options:
  plugin:
    description:
      - The name of this plugin, it should always be set to
        V(community.general.iocage) for this plugin to recognize
        it as its own.
    required: true
    choices: ['community.general.iocage']
    type: str
  host:
    description: The IP/hostname of the C(iocage) host.
    type: str
    default: localhost
  user:
    description:
      - C(iocage) user.
        It is expected that the O(user) is able to connect to the
        O(host) with SSH and execute the command C(iocage list).
        This option is not required if O(host) is V(localhost).
    type: str
  sudo:
    description:
      - Enable execution as root.
      - This requires passwordless sudo of the command C(iocage list*).
    type: bool
    default: false
    version_added: 10.3.0
  sudo_preserve_env:
    description:
      - Preserve environment if O(sudo) is enabled.
      - This requires C(SETENV) sudoers tag.
    type: bool
    default: false
    version_added: 10.3.0
  get_properties:
    description:
      - Get jails' properties.
        Creates dictionary C(iocage_properties) for each added host.
    type: bool
    default: false
  env:
    description:
      - O(user)'s environment on O(host).
      - Enable O(sudo_preserve_env) if O(sudo) is enabled.
    type: dict
    default: {}
  hooks_results:
    description:
      - List of paths to the files in a jail.
      - Content of the files is stored in the items of the list C(iocage_hooks).
      - If a file is not available the item keeps the dash character C(-).
      - The variable C(iocage_hooks) is not created if O(hooks_results) is empty.
    type: list
    elements: path
    version_added: 10.4.0
notes:
  - You might want to test the command C(ssh user@host iocage list -l) on
    the controller before using this inventory plugin with O(user) specified
    and with O(host) other than V(localhost).
  - If you run this inventory plugin on V(localhost) C(ssh) is not used.
    In this case, test the command C(iocage list -l).
  - This inventory plugin creates variables C(iocage_*) for each added host.
  - The values of these variables are collected from the output of the
    command C(iocage list -l).
  - The names of these variables correspond to the output columns.
  - The column C(NAME) is used to name the added host.
  - The option O(hooks_results) expects the C(poolname) of a jail is mounted to
    C(/poolname). For example, if you activate the pool C(iocage) this plugin
    expects to find the O(hooks_results) items in the path
    C(/iocage/iocage/jails/<name>/root). If you mount the C(poolname) to a
    different path the easiest remedy is to create a symlink.
a  
---
# file name must end with iocage.yaml or iocage.yml
plugin: community.general.iocage
host: 10.1.0.73
user: admin

---
# user is not required if iocage is running on localhost (default)
plugin: community.general.iocage

---
# run cryptography without legacy algorithms
plugin: community.general.iocage
host: 10.1.0.73
user: admin
env:
  CRYPTOGRAPHY_OPENSSL_NO_LEGACY: 1

---
# execute as root
# sudoers example 'admin ALL=(ALL) NOPASSWD:SETENV: /usr/local/bin/iocage list*'
plugin: community.general.iocage
host: 10.1.0.73
user: admin
sudo: true
sudo_preserve_env: true
env:
  CRYPTOGRAPHY_OPENSSL_NO_LEGACY: 1

---
# enable cache
plugin: community.general.iocage
host: 10.1.0.73
user: admin
env:
  CRYPTOGRAPHY_OPENSSL_NO_LEGACY: 1
cache: true

---
# see inventory plugin ansible.builtin.constructed
plugin: community.general.iocage
host: 10.1.0.73
user: admin
env:
  CRYPTOGRAPHY_OPENSSL_NO_LEGACY: 1
cache: true
strict: false
compose:
  ansible_host: iocage_ip4
  release: iocage_release | split('-') | first
groups:
  test: inventory_hostname.startswith('test')
keyed_groups:
  - prefix: distro
    key: iocage_release
  - prefix: state
    key: iocage_state

---
# Read the file /var/db/dhclient-hook.address.epair0b in the jails and use it as ansible_host
plugin: community.general.iocage
host: 10.1.0.73
user: admin
hooks_results:
  - /var/db/dhclient-hook.address.epair0b
compose:
  ansible_host: iocage_hooks.0
groups:
  test: inventory_hostname.startswith('test')
N)PopenPIPE)AnsibleErrorAnsibleParserError)	to_nativeto_text)BaseInventoryPluginConstructable	Cacheable)Displayc                ^   i }g |d<   d|d<   | j                  d      }|D ]  }t        j                  d|      ret        j                   d|      }t        |      dk(  r"|d   j	                  |d   |d	   |d
   d       _|d   j	                  |d   |d	   dd       ~|dxx   |z  cc<    |S )z Return dictionary iocage_ip4_dict. default = {ip4: [], msg: ''}.
        If item matches ifc|IP or ifc|CIDR parse ifc, ip, and mask.
        Otherwise, append item to msg.
    ip4 msg,z ^\w+\|(?:\d{1,3}\.){3}\d{1,3}.*$z\||/   r         )ifcipmask-)splitrematchlenappend)r   iocage_ip4_dictitemsitemis        n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/inventory/iocage.py
_parse_ip4r$      s     OOEOEIIcNE +88;TB$'A1v{&--ad!A$PQRSPT.UV&--ad!A$PS.TUE"d*"+     c                  X     e Zd ZdZdZdZ fdZ fdZd fd	Zd Z	d Z
d	 Zd
 Z xZS )InventoryModulez; Host inventory parser for ansible using iocage as source. zcommunity.general.iocagez/usr/local/bin/iocagec                *    t         t        |           y )N)superr'   __init__)self	__class__s    r#   r*   zInventoryModule.__init__   s    ot-/r%   c                    d}t         t        |   |      r0|j                  d      rd}|S | j                  j                  d       |S )NF)ziocage.yamlz
iocage.ymlTzMSkipping due to inventory source not ending in "iocage.yaml" nor "iocage.yml")r)   r'   verify_fileendswithdisplayvvv)r+   pathvalidr,   s      r#   r.   zInventoryModule.verify_file   sJ    $3D9}}:;    !pqr%   c                d   t         t        |   |||       | j                  |       | j	                  |      }| j                  d      }|xr |}|xr | }|r	 | j                  |   }	|r|r| j                  |      }	|r	| j                  |<   | j                  	       y # t        $ r d}Y Ew xY w)NcacheT)
r)   r'   parse_read_config_dataget_cache_key
get_option_cacheKeyErrorget_inventorypopulate)r+   	inventoryloaderr2   r5   	cache_keyuser_cache_settingattempt_to_read_cachecache_needs_updateresultsr,   s             r#   r6   zInventoryModule.parse   s    ot*9fdCt$&&t,	!__W5 2 <u/=I *++i0 %(:((.G%,DKK	"g  *%)"*s   B! !B/.B/c                4   | j                  d      }| j                  d      }| j                  d      }| j                  d      }| j                  d      }| j                  d      }g }t        j                  j                         }	|dk(  rF|	j	                  |j                         D 
ci c]  \  }
}t        |
      t        |       c}}
       no| j                  d      }|j                  d	       |j                  | d
|        |j                  |j                         D 
cg c]  \  }
}|
 d|  c}}
       |j                         }|r$|j                  d       |r|j                  d       |j                  | j                         |j                  d       |j                  d       	 t        |t        t        |	      }|j                         \  }}|j                  dk7  r't        d| d|j                   dt        |             	 t!        |d      }	 ddi ii}| j)                  ||       |r |d   d   j                         D ]  \  }}|j                         }|j                  | j                         |j                  d       |j                  d       |j                  |        	 t        |t        t        |	      }|j                         \  }}|j                  dk7  r't        d| d|j                   dt        |             	 t!        |d      }	 | j+                  |||        |r|j                         }|j                  | j                         |j                  d       |j                  d       	 t        |t        t        |	      }|j                         \  }}|j                  dk7  r't        d| d|j                   dt        |             	 t!        |d      j-                         }	 |d   d   j                         D ]  \  }}g }|D ]  }d |z   d!z   |z   d"z   |z   }|j                         }|j                  d#       |j                  |       	 t        |t        t        |	      }|j                         \  }}|j                  dk7  r|j                  d$       	 t!        |d      j-                         }	 |j                  |        ||d   d   |   d%<    |S c c}}
w c c}}
w # t"        $ r}t        d|       |d }~ww xY w# t$        $ r }t'        dt        |       d|       |d }~ww xY w# t"        $ r}t        d|       |d }~ww xY w# t$        $ r}t        d|       |d }~ww xY w# t"        $ r}t        d|       |d }~ww xY w# t$        $ r}t        d|       |d }~ww xY w# t"        $ r}t        d|       |d }~ww xY w# t$        $ r |j                  d$       Y w xY w)&Nhostsudosudo_preserve_envenvget_propertieshooks_results	localhostuserssh@=z--preserve-envlistz--long)stdoutstderrrI   r   zFailed to run cmd=z, rc=z	, stderr=surrogate_or_strict)errorsz&Invalid (non unicode) input returned: zFailed to parse z: _metahostvarsgetz--allzFailed to get properties: z--poolzFailed to get pool: /z/iocage/jails/z/rootcatr   iocage_hooks)r9   osenvironcopyupdater    strr   extendIOCAGEr   r   communicate
returncoder   r   r	   UnicodeError	Exceptionr   	get_jailsrJ   strip)r+   r2   rF   rG   rH   rI   rJ   rK   cmdmy_envkvrM   cmd_listprR   rS   t_stdouterD   hostname	host_varscmd_get_propertiescmd_get_pooliocage_poolr[   hookcmd_cat_hookiocage_hooks                                r#   r<   zInventoryModule.get_inventory   s   v&v& OO,?@ooe$)9:8";MMciikBda3q63q6>BC??6*DJJuJJ$q'(JJsyy{;tq!1#Qqc
;<88:OOF#  01$!	WhtDfEA]]_NFF||q "%7zq||nT]^ghn^o]p#qrrX"62GH Z,-x)'.w'7
'C'I'I'K A#)%(XXZ""))$++6"))%0"))'2"))XJ8P0dPVWA%&]]_NFF||q(*01C0DE!,,W`ajkqar`stv v`#*6:O#P ##Hgx@+A. 88:L,&)F,tDfM!"<<1$&,\N%~YW`agWhVijl l\")&9N"O"U"U"WK (/w'7
'C'I'I'K V#)!) 9D,/??(JWTW[[D#&88:L ''. ''-9!,tDfU)*<<1,(//4$d*1&AV*W*]*]*_K %++K8)9, JV ,X6~F1V4 I C
 <$   X"%KA3#OPVWWX  	W$'7	$7H1#%NOUVV	W, ( `*-STUSV+WX^__` ! P&)CA3'GHaOP$ $ \&)OPQs'STZ[[\ F"%9!#=>AEF&  , d".1WXYWZ/["\bccd % 1$++C01s   *T
(T
5A T- T ;A U9UA V9 1V 4A
W9 W	T*T%%T**T- -	U6UU	U6"U11U66U99	VVV	V6"V11V66V9 9	WWW	W6"W11W66W99XXc                   |j                         }t        |      dk  ry t        |d         D cg c]  \  }}|dk(  s| }}}|dd d   D ]*  }t        |d d |dd        D cg c]  \  }}||dz   | j	                          }	}}|	d   }
t        |	d         }|d   r&d	j                  |d   D cg c]  }|d
   	 c}      }nd}i |d   d   |
<   |	d   |d   d   |
   d<   |	d   |d   d   |
   d<   |	d   |d   d   |
   d<   |	d   |d   d   |
   d<   |	d   |d   d   |
   d<   ||d   d   |
   d<   ||d   d   |
   d<   |	d   |d   d   |
   d<   |	d   |d   d   |
   d<   |	d   |d   d   |
   d<   - y c c}}w c c}}w c c}w )N   r   |r   r      r   r   r   r   rV   rW   r   
iocage_jidiocage_bootiocage_state   iocage_typeiocage_releaser   
iocage_ip4   
iocage_ip6   iocage_template	   iocage_basejail)
splitlinesr   	enumerateziprh   r$   join)r+   ro   rD   linesr"   valindiceslinejjailiocage_namer   dr   s                 r#   rg   zInventoryModule.get_jailsj  s'   ##%u:>#,U1X#6EC#*1EE!$Q$K 	SD69'#2,PQPR6TUdaDQqM'')UDUq'K(a1Ou% XX8N&O1qw&OP
 
8:GGZ(5FJ1gGGZ(5lCGKAwGGZ(5mDHLQGGZ(5nEGKAwGGZ(5mDJNq'GGZ(56FGKZGGZ(56GHFPGGZ(5lCFJ1gGGZ(5lCKOPQ7GGZ(56GHKOPQ7GGZ(56GH%	S FU 'Ps   E=E=' F4F	
c           	         t        |j                         D cg c]  }|j                  dd       c}      }||d   d   |   d<   y c c}w )N:r   rV   rW   iocage_properties)dictr   r   )r+   ro   rD   rq   x
propertiess         r#   rJ   zInventoryModule.get_properties  sJ    H4G4G4IJq1773?JK
FP$X./BC Ks   Ac                   | j                  d      }|d   d   j                         D ]  \  }}| j                  j                  |d       |j                         D ]"  \  }}| j                  j	                  |||       $ | j                  | j                  d      ||d       | j                  | j                  d	      |||       | j                  | j                  d
      |||        y )NstrictrV   rW   all)groupcomposeT)r   groupskeyed_groups)r9   r    r>   add_hostset_variable_set_composite_vars_add_host_to_composed_groups_add_host_to_keyed_groups)r+   rD   r   rq   rr   varvalues          r#   r=   zInventoryModule.populate  s    *#*7#3J#?#E#E#G 	pHiNN##HE#:'oo/ B
U++Hc5AB$$T__Y%?H]a$b--dooh.GT\ek-l**4??>+JIW_hn*o	pr%   )T)__name__
__module____qualname____doc__NAMErb   r*   r.   r6   r<   rg   rJ   r=   __classcell__)r,   s   @r#   r'   r'      s8    E%D$F0,obS2Q	pr%   r'   )
__future__r   DOCUMENTATIONEXAMPLESr   r\   
subprocessr   r   ansible.errorsr   r   +ansible.module_utils.common.text.convertersr   r	   ansible.plugins.inventoryr
   r   r   ansible.utils.displayr   r0   r$   r'    r%   r#   <module>r      sW    #ZxFP 
 	 " ; J S S )
)0p)=) pr%   