
    Vh)$                        d dl mZ dZdZdZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZmZ d dlmZ dd	Zdd
Zd Zedk(  r e        yy)    )annotationsa  
---
module: assemble
short_description: Assemble configuration files from fragments
description:
- Assembles a configuration file from fragments.
- Often a particular program will take a single configuration file and does not support a
  C(conf.d) style structure where it is easy to build up the configuration
  from multiple sources. M(ansible.builtin.assemble) will take a directory of files that can be
  local or have already been transferred to the system, and concatenate them
  together to produce a destination file.
- Files are assembled in string sorting order.
- Puppet calls this idea I(fragments).
version_added: '0.5'
options:
  src:
    description:
    - An already existing directory full of source files.
    type: path
    required: true
  dest:
    description:
    - A file to create using the concatenation of all of the source files.
    type: path
    required: true
  backup:
    description:
    - Create a backup file (if V(true)), including the timestamp information so
      you can get the original file back if you somehow clobbered it
      incorrectly.
    type: bool
    default: no
  delimiter:
    description:
    - A delimiter to separate the file contents.
    type: str
    version_added: '1.4'
  remote_src:
    description:
    - If V(false), it will search for src at originating/master machine.
    - If V(true), it will go to the remote/target machine for the src.
    type: bool
    default: yes
    version_added: '1.4'
  regexp:
    description:
    - Assemble files only if the given regular expression matches the filename.
    - If not set, all files are assembled.
    - Every V(\\) (backslash) must be escaped as V(\\\\) to comply to YAML syntax.
    - Uses L(Python regular expressions,https://docs.python.org/3/library/re.html).
    type: str
  ignore_hidden:
    description:
    - A boolean that controls if files that start with a C(.) will be included or not.
    type: bool
    default: no
    version_added: '2.0'
  validate:
    description:
    - The validation command to run before copying into place.
    - The path to the file to validate is passed in by C(%s) which must be present as in the sshd example below.
    - The command is passed securely so shell features like expansion and pipes won't work.
    type: str
    version_added: '2.0'
attributes:
    action:
      support: full
    async:
      support: none
    bypass_host_loop:
      support: none
    check_mode:
      support: none
    diff_mode:
      support: full
    platform:
      platforms: posix
    safe_file_operations:
      support: full
    vault:
      support: full
      version_added: '2.2'
seealso:
- module: ansible.builtin.copy
- module: ansible.builtin.template
- module: ansible.windows.win_copy
author:
- Stephen Fromm (@sfromm)
extends_documentation_fragment:
    - action_common_attributes
    - action_common_attributes.flow
    - action_common_attributes.files
    - decrypt
    - files
a(  
- name: Assemble from fragments from a directory
  ansible.builtin.assemble:
    src: /etc/someapp/fragments
    dest: /etc/someapp/someapp.conf

- name: Insert the provided delimiter between fragments
  ansible.builtin.assemble:
    src: /etc/someapp/fragments
    dest: /etc/someapp/someapp.conf
    delimiter: '### START FRAGMENT ###'

- name: Assemble a new "sshd_config" file into place, after passing validation with sshd
  ansible.builtin.assemble:
    src: /etc/ssh/conf.d/
    dest: /etc/ssh/sshd_config
    validate: /usr/sbin/sshd -t -f %s
#N)AnsibleModule)b
indexbytes)	to_nativec                   t        j                  |      \  }}t        j                  |d      }d}d}	t	        t        j
                  |             D ]Q  }
|r|j                  |
      st        j                  j                  | |
      }t        j                  j                  |      r0|r/t        j                  j                  |      j                  d      rt        |d      5 }|j                         }ddd       |	r|j                  t        d             |rT|rRt!        j"                  |      d   }|j                  |       t%        |d	      d
k7  r|j                  t        d             |j                         d}|j'                  t        d            rd}	Pd}	T |j)                          |S # 1 sw Y   xY w)z/ assemble a file from a directory of fragments )dirwbF.rbN
r   
   T)tempfilemkstemposfdopensortedlistdirsearchpathjoinisfilebasename
startswithopenreadwriter   codecsescape_decoder   endswithclose)src_path	delimitercompiled_regexpignore_hiddentmpdirtmpfd	temp_pathtmp
delimit_meadd_newlineffragmentfragment_fhfragment_contents                 H/home/dcms/DCMS/lib/python3.12/site-packages/ansible/modules/assemble.pyassemble_from_fragmentsr3      sw   ''F3E9
))E4
 CJKBJJx() !?#9#9!#<77<<!,ww~~h'Mbgg>N>Nx>X>c>cdg>h(D! 	2[*//1	2 IIag "00;A>			)$ i,2IIag&		"#
$$QtW-KKC!F IIK=	2 	2s   &F99G	c                    t         j                  j                  |       r	 t        j                  |        y y # t        t
        f$ r&}|d| dt        |      g|d<   Y d }~y Y d }~y d }~ww xY w)NzUnable to remove temp file (z): warnings)r   r   existsremoveIOErrorOSErrorr   )r   resultes      r2   cleanupr<      se    	ww~~d	dIIdO  ! 	d!OSU^_`Ua&b%cz" "	ds   8 A-A((A-c                    t        t        t        dd      t        d      t        dd      t        dd      t        dd      t        d      t        dd      t        d      t        dd      		      d
      } d}d }d }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }d }	| j                  d   }
| j                  j                  dd       }t        ||      }t        j
                  j                  |      s| j                  d|z         t        j
                  j                  |      s| j                  d|z         |	 t        j                  |      }	|rd|vr| j                  d|z         t        |||	|
| j                        }| j                  |      }||d<   	 | j!                  |      }||d<   t        j
                  j                  |      r| j                  |      }||k7  r|rQ| j%                  ||z        \  }}}t        |||      |d<   |dk7  r#t'        |       | j                  d |d!|       |r|| j)                  |      |d"<   | j+                  ||| j                  d#   $       d}t'        ||       | j-                  | j                        }| j/                  ||      |d%<   d&|d'<    | j0                  d(i | y # t        j                  $ r-}| j                  dt        |      d|d       Y d }~d }~ww xY w# t"        $ r d }Y mw xY w))Nr   T)typerequiredstr)r>   boolF)r>   default)	srcr%   destbackup
remote_srcregexpr'   validatedecrypt)argument_specadd_file_common_argsrC   rD   rE   r%   rG   r'   rH   )rC   rD   zSource (%s) does not exist)msgzSource (%s) is not a directoryzInvalid Regexp (z) in ""z%szvalidate must contain %%s: %schecksummd5sum)rcstdoutstderr
validationr   zfailed to validate: rc:z error:backup_fileunsafe_writes)rU   changedOKrL    )r   dictparamsgetr   r   r6   	fail_jsonisdirrecompileerrorr   r3   r(   sha1md5
ValueErrorrun_commandr<   backup_localatomic_moveload_file_common_argumentsset_fs_attributes_if_different	exit_json)modulerV   	path_hash	dest_hashrC   rD   rE   r%   rG   r&   r'   rH   r:   r;   r   pathmd5rP   outerr	file_argss                       r2   mainrq      s2   &40&6D1VU36U#FE:u%
 fd3
 "#F( GII
--
C== D]]8$Fk*I]]8$FOMM/2M}}  T2Hc%F77>>#9C?@77===CD	[ jj0O D(<xGH"3	?MSYS`S`aDD!I"F:**T" F8	ww~~dKK%	I#//4@NRc#'2c##FF< Qw  RQT%U Vi+$*$7$7$=F=!4V]]?5STD& 11&--@I==iQF9 F5MFvS xx 	[IaLRX!YZZ	[  s*   6L M M"L>>MMM__main__)NNFN)N)
__future__r   DOCUMENTATIONEXAMPLESRETURNr    r   r^   r   ansible.module_utils.basicr   ansible.module_utils.sixr   r   +ansible.module_utils.common.text.convertersr   r3   r<   rq   __name__rX       r2   <module>r|      s]    #^@& 
  	 	  4 2 A+\dUp zF r{   