ansible-collections / ibm_zos_core

Red Hat Ansible Certified Content for IBM Z
77 stars 44 forks source link

I want to remove a block between # BEGIN and # END from a PDS member with the help of blockinfile module. #1668

Open rohan-zos-ansible opened 2 months ago

rohan-zos-ansible commented 2 months ago

I want to remove a block between # BEGIN and # END from a PDS member with the help of blockinfile module.

SCRIPT +                                              
# BEGIN ANSIBLE MANAGED BLOCK                         
       UTIDSN(ZZZDBU.V51QA.SMPE.DBUSCRL +             
       ZZZBRI.QAV51.BRISCRL  +                        
       ZZZCDBU.V51QA.SMPE.DBUSCRS  +                   
       ZZZCBRI.QAV51.BRISCRS) +                        
       SYSOUT(*) TRACE(Y) TRACEOUT(*)                 
# END ANSIBLE MANAGED BLOCK            

Code:

        - name: Remove some libraries from Scripts in INI file
          ibm.ibm_zos_core.zos_blockinfile:
            src: "PDS.ANSIBLE.WORK.CNTL(TRZSINI)"
            marker_begin: "# BEGIN ANSIBLE MANAGED BLOCK                                           "
            marker_end: "# END ANSIBLE MANAGED BLOCK                                             "
            state: absent 

Not sure what mistake i am doing. If anyone has any knowledge on this, please let me know.

Thank you, Rohan

_Originally posted by @rohan-zos-ansible in https://github.com/ansible-collections/ibm_zos_core/discussions/1667_

rohan-zos-ansible commented 2 months ago

I am using ZOS CORE version 1.8.0

AndreMarcel99 commented 2 months ago

Hi @rohan-zos-ansible I work with your case and if you're using the default markers as I can see for your playbooks using

    - name: Remove some libraries from Scripts in INI file
      zos_blockinfile:
        src: "{{ tmp_ds }}"
        state: absent
      ignore_errors: True 

will work on you.

I try the playbook:

  tasks:
    - name: Create a sequential data set if it does not exist
      zos_data_set:
        name: "{{ tmp_ds }}"
        type: seq
        state: present

    - name: Ensure
      zos_lineinfile:
        src: "{{ tmp_ds }}"
        insertbefore: BOF
        line: "script"

    - name: Write on it
      zos_blockinfile:
        src: "{{ tmp_ds }}"
        insertafter: EOF
        block: |
          UTIDSN(ZZZDBU.V51QA.SMPE.DBUSCRL +
          ZZZBRI.QAV51.BRISCRL  +
          ZZZCDBU.V51QA.SMPE.DBUSCRS  +
          ZZZCBRI.QAV51.BRISCRS) +
          SYSOUT(*) TRACE(Y) TRACEOUT(*)

    - name: Remove some libraries from Scripts in INI file
      zos_blockinfile:
        src: "{{ tmp_ds }}"
        state: absent
      ignore_errors: True

    - name: "dcat {{ tmp_ds }}"
      ansible.builtin.shell:
        cmd: "dcat {{ tmp_ds }}"
      register: dcat

    - name: "Result of dcat"
      debug:
        msg:
          - "{{ dcat }}"

And got the next result:

Captura de pantalla 2024-08-28 a la(s) 11 17 29 a m
rohan-zos-ansible commented 2 months ago

Thank you so much @AndreMarcel99. It worked. How if i want to remove a block between BEGIN and END. I may not be able to use # but i could use * since # may not be allowed in my file as comment or marker. But, I may be able to use #.

SCRIPT +                                              
* BEGIN ANSIBLE MANAGED BLOCK                 
       UTIDSN(ZZZDBU.V51QA.SMPE.DBUSCRL +             
       ZZZBRI.QAV51.BRISCRL  +                        
       ZZZCDBU.V51QA.SMPE.DBUSCRS  +                   
       ZZZCBRI.QAV51.BRISCRS) +                        
       SYSOUT(*) TRACE(Y) TRACEOUT(*)                 
* END ANSIBLE MANAGED BLOCK 
AndreMarcel99 commented 2 months ago

@rohan-zos-ansible Still work, in these case I recommend

    - name: Write on it
      zos_blockinfile:
        src: "{{ tmp_ds }}"
        marker: "* {mark} ANSIBLE MANAGED BLOCK"
        insertafter: EOF
        block: |
          UTIDSN(ZZZDBU.V51QA.SMPE.DBUSCRL +
          ZZZBRI.QAV51.BRISCRL  +
          ZZZCDBU.V51QA.SMPE.DBUSCRS  +
          ZZZCBRI.QAV51.BRISCRS) +
          SYSOUT(*) TRACE(Y) TRACEOUT(*)

       - name: Remove some libraries from Scripts in INI file
      zos_blockinfile:
        marker: "* {mark} ANSIBLE MANAGED BLOCK"
        src: "{{ tmp_ds }}"
        state: absent
      ignore_errors: True

Same playbook:

Captura de pantalla 2024-08-29 a la(s) 10 19 39 a m

I check and try some cases and for the version of core you're using and I don't recommend to change the values of marker_begin or marker_end (Defaults are BEGIN and END respectively) when doing an insert.