HEX
Server: LiteSpeed
System: Linux kapuas.iixcp.rumahweb.net 5.14.0-427.42.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Nov 1 14:58:02 EDT 2024 x86_64
User: mirz4654 (1666)
PHP: 8.1.33
Disabled: system,exec,escapeshellarg,escapeshellcmd,passthru,proc_close,proc_get_status,proc_nice,proc_open,proc_terminate,shell_exec,popen,pclose,dl,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setsid,posix_setuid,posix_setpgid,ini_alter,show_source,define_syslog_variables,symlink,syslog,openlog,openlog,closelog,ocinumcols,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dll,ftp,myshellexec,socket_bind,mail,posix_getwpuid
Upload Files
File: //usr/lib/python3.9/site-packages/ansible/modules/__pycache__/blockinfile.cpython-39.pyc
a

�)g+5�@s�ddlmZmZmZeZdZdZddlZddl	Z	ddl
Z
ddlmZddl
mZddlmZmZdd	�Zd
d�Zdd
�Zedkr�e�dS)�)�absolute_import�division�print_functiona�
---
module: blockinfile
short_description: Insert/update/remove a text block surrounded by marker lines
version_added: '2.0'
description:
- This module will insert/update/remove a block of multi-line text surrounded by customizable marker lines.
author:
- Yaegashi Takeshi (@yaegashi)
options:
  path:
    description:
    - The file to modify.
    - Before Ansible 2.3 this option was only usable as I(dest), I(destfile) and I(name).
    type: path
    required: yes
    aliases: [ dest, destfile, name ]
  state:
    description:
    - Whether the block should be there or not.
    type: str
    choices: [ absent, present ]
    default: present
  marker:
    description:
    - The marker line template.
    - C({mark}) will be replaced with the values in C(marker_begin) (default="BEGIN") and C(marker_end) (default="END").
    - Using a custom marker without the C({mark}) variable may result in the block being repeatedly inserted on subsequent playbook runs.
    - Multi-line markers are not supported and will result in the block being repeatedly inserted on subsequent playbook runs.
    - A newline is automatically appended by the module to C(marker_begin) and C(marker_end).
    type: str
    default: '# {mark} ANSIBLE MANAGED BLOCK'
  block:
    description:
    - The text to insert inside the marker lines.
    - If it is missing or an empty string, the block will be removed as if C(state) were specified to C(absent).
    type: str
    default: ''
    aliases: [ content ]
  insertafter:
    description:
    - If specified and no begin/ending C(marker) lines are found, the block will be inserted after the last match of specified regular expression.
    - A special value is available; C(EOF) for inserting the block at the end of the file.
    - If specified regular expression has no matches, C(EOF) will be used instead.
    - The presence of the multiline flag (?m) in the regular expression controls whether the match is done line by line or with multiple lines.
      This behaviour was added in ansible-core 2.14.
    type: str
    choices: [ EOF, '*regex*' ]
    default: EOF
  insertbefore:
    description:
    - If specified and no begin/ending C(marker) lines are found, the block will be inserted before the last match of specified regular expression.
    - A special value is available; C(BOF) for inserting the block at the beginning of the file.
    - If specified regular expression has no matches, the block will be inserted at the end of the file.
    - The presence of the multiline flag (?m) in the regular expression controls whether the match is done line by line or with multiple lines.
      This behaviour was added in ansible-core 2.14.
    type: str
    choices: [ BOF, '*regex*' ]
  create:
    description:
    - Create a new file if it does not exist.
    type: bool
    default: no
  backup:
    description:
    - Create a backup file including the timestamp information so you can
      get the original file back if you somehow clobbered it incorrectly.
    type: bool
    default: no
  marker_begin:
    description:
    - This will be inserted at C({mark}) in the opening ansible block marker.
    type: str
    default: BEGIN
    version_added: '2.5'
  marker_end:
    required: false
    description:
    - This will be inserted at C({mark}) in the closing ansible block marker.
    type: str
    default: END
    version_added: '2.5'
notes:
  - When using 'with_*' loops be aware that if you do not set a unique mark the block will be overwritten on each iteration.
  - As of Ansible 2.3, the I(dest) option has been changed to I(path) as default, but I(dest) still works as well.
  - Option I(follow) has been removed in Ansible 2.5, because this module modifies the contents of the file so I(follow=no) doesn't make sense.
  - When more then one block should be handled in one file you must change the I(marker) per task.
extends_documentation_fragment:
    - action_common_attributes
    - action_common_attributes.files
    - files
    - validate
attributes:
    check_mode:
        support: full
    diff_mode:
        support: full
    safe_file_operations:
      support: full
    platform:
      support: full
      platforms: posix
    vault:
      support: none
a�
# Before Ansible 2.3, option 'dest' or 'name' was used instead of 'path'
- name: Insert/Update "Match User" configuration block in /etc/ssh/sshd_config
  ansible.builtin.blockinfile:
    path: /etc/ssh/sshd_config
    block: |
      Match User ansible-agent
      PasswordAuthentication no

- name: Insert/Update eth0 configuration stanza in /etc/network/interfaces
        (it might be better to copy files into /etc/network/interfaces.d/)
  ansible.builtin.blockinfile:
    path: /etc/network/interfaces
    block: |
      iface eth0 inet static
          address 192.0.2.23
          netmask 255.255.255.0

- name: Insert/Update configuration using a local file and validate it
  ansible.builtin.blockinfile:
    block: "{{ lookup('ansible.builtin.file', './local/sshd_config') }}"
    path: /etc/ssh/sshd_config
    backup: yes
    validate: /usr/sbin/sshd -T -f %s

- name: Insert/Update HTML surrounded by custom markers after <body> line
  ansible.builtin.blockinfile:
    path: /var/www/html/index.html
    marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->"
    insertafter: "<body>"
    block: |
      <h1>Welcome to {{ ansible_hostname }}</h1>
      <p>Last updated on {{ ansible_date_time.iso8601 }}</p>

- name: Remove HTML as well as surrounding markers
  ansible.builtin.blockinfile:
    path: /var/www/html/index.html
    marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->"
    block: ""

- name: Add mappings to /etc/hosts
  ansible.builtin.blockinfile:
    path: /etc/hosts
    block: |
      {{ item.ip }} {{ item.name }}
    marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item.name }}"
  loop:
    - { name: host1, ip: 10.10.1.10 }
    - { name: host2, ip: 10.10.1.11 }
    - { name: host3, ip: 10.10.1.12 }

- name: Search with a multiline search flags regex and if found insert after
  blockinfile:
    path: listener.ora
    block: "{{ listener_line | indent(width=8, first=True) }}"
    insertafter: '(?m)SID_LIST_LISTENER_DG =\n.*\(SID_LIST ='
    marker: "    <!-- {mark} ANSIBLE MANAGED BLOCK -->"

N)�b)�
AnsibleModule)�to_bytes�	to_nativecCs�tj|jd�\}}t�|d�}|�|�|��|j�dd�}|}|r�d|vr`|j	d|d�|�
||�\}}	}
|dk}|dkr�|j	d||
fd�|r�|j|||jd	d
�dS)N)�dir�wb�validatez%szvalidate must contain %%s: %s��msgrz"failed to validate: rc:%s error:%s�
unsafe_writes)r)�tempfileZmkstempZtmpdir�os�fdopen�write�close�params�get�	fail_jsonZrun_commandZatomic_move)�module�contents�pathZtmpfdZtmpfile�frZvalid�rc�out�err�r�?/usr/lib/python3.9/site-packages/ansible/modules/blockinfile.py�
write_changes�s"
�r cCs<|�|j�}|j|d|d�r4|r(|d7}d}|d7}||fS)NF)�diffz and Tz,ownership, perms or SE linux context changed)Zload_file_common_argumentsrZ set_file_attributes_if_different)r�changed�messager!Z	file_argsrrr�check_file_attrs�sr$cCs�tttddgd�d�tddddgd�tdd	d
�tdddgd
�tdd�tdd�tddd
�tddd
�tdd�tddd
�tddd
�d�ddggddd�}|j}|d}tj�|�r�|jdd|d�tj�|�}|�sp|�|d�s�|jdd|d�tj�	|�}tj�|��sf|j
�sfzt�|�WnDt�yd}z*|jd||d|dfd �WYd}~n
d}~00d}g}n>t
|d!��}|��}Wd�n1�s�0Y|�d�}ddd"|d"|d#�}	|j�r�|�r�||	d$<|d}
|d}t|d%�}t|d&�}
|d'dk}|�s,|�s,|jdd(|d)�|
du�rD|du�rDd*}|d+v�rbt�t|d,d-��}n"|
d.v�r�t�t|
d,d-��}nd}t�td/�t|d0�|
�ttj�}t�td/�t|d1�|
�ttj�}|�r|�r|�ttj���s�|ttj�7}|g|�d�|g}ng}d}}t|�D]&\}}||k�r:|}||k�r$|}�q$d||fv�r(d}|du�r|jtj@�r�|�|�}|�r�|�r�t|��d2d|���}n|
�r�t|��d2d|� ��}n$t|�D]\}}|�|��r�|}�q�|du�r�t!|�}n|du�r&|d7}n|
du�rd}nt!|�}n0||k�rDg|||d�<ng|||d�<|}|dk�r�||d�ttj���s�||dttj�7<||||�<|�r�d3�"|�}nd3}|j�r�||	d4<||k�r�d}d}n,|du�r�d5}d}n|�sd6}d}nd7}d}d}|�rV|j
�sV|�|d8��r:|�r:|�#|�}tj�$|d�}t%|||�|j
�rt|�st|j|||	d9�i}t&||||�\}}d:||d;<d:||d<<|	|g}|du�r�|j|||d9�n|j||||d=�dS)>NrT)�destZdestfile�name)�type�required�aliases�str�presentZabsent)r'�default�choicesz# {mark} ANSIBLE MANAGED BLOCK)r'r,�Zcontent)r'r,r))r'�boolFZBEGINZEND)r�state�marker�block�insertafter�insertbefore�create�backupr�marker_begin�
marker_endr4r3)Z
argument_specZmutually_exclusiveZadd_file_common_argsZsupports_check_mode�zPath %s is a directory !)rr
r5izPath %s does not exist !z6Error creating %s Error code: %s Error description: %sr�r�rbz%s (content))�before�after�
before_header�after_headerr<r2r1r0zFile %s not present)r"r
�EOF)Nr@Zsurrogate_or_strict)�errors)NZBOFz{mark}r7r8�
�r=zFile createdz
Block removedzBlock insertedr6)r"r
r!z%s (file attributes)r>r?)r"r
r!�backup_file)'r�dictrrr�isdirr�existsZboolean�dirnameZ
check_mode�makedirs�	Exception�open�read�
splitlinesZ_diffrZ	exit_json�re�compile�subr�linesep�endswith�	enumerate�flags�	MULTILINE�searchr�count�end�start�len�joinZbackup_local�realpathr r$)rrrZpath_existsZdestpath�e�original�linesrr!r4r3r2r1r+ZinsertreZmarker0Zmarker1Z
blocklinesZn0Zn1�i�line�match�resultr
r"rDZ	real_pathZ	attr_diffZdifflistrrr�main�s




�
���4(
�

$$















rd�__main__)Z
__future__rrrr'Z
__metaclass__Z
DOCUMENTATIONZEXAMPLESrNrrZansible.module_utils.sixrZansible.module_utils.basicrZansible.module_utils._textrrr r$rd�__name__rrrr�<module>sj<
(