Ansible for IBM Z

Ansible for IBM Z

Ansible for IBM Z

Facilitate communication, user interaction and feedback for Red Hat Ansible Certified Content for IBM Z

 View Only
Expand all | Collapse all

Saving a result from command (zos_operator) to file (zos_copy) - Doesn´t accept registered variable

  • 1.  Saving a result from command (zos_operator) to file (zos_copy) - Doesn´t accept registered variable

    Posted Thu April 13, 2023 04:04 PM

    This could be a simple issue easy to be resolved but I'm just starting testing it on mainframe,  I'm not able to collect the result from a command and save it to a file , it gives me the known error  "UnboundLocalError: local variable 'local_content' referenced before assignment" - but the I've tried to define it in a different variable, (setfact) tried loading it from hostvars, using block, etc. the "reference before assignment" doesn´t make much sense as i've tested it and confirmed it can be loaded inside that play.  Would you know the best way to save any command's result to a file / dataset.

    ---
    - name: playbook - command - saving to file
      hosts: all
      gather_facts: false
      
      environment:
        ZOAU_HOME: "{{ ZOAU }}"
        PYTHONPATH: "{{ ZOAU }}/lib"
        LIBPATH: "{{ ZOAU }}/lib:{{ PYZ }}/lib:/lib:/usr/lib:."
        PATH: "{{ ZOAU }}/bin:{{ PYZ }}/bin:/bin:/usr/sbin:/var/bin"
        _BPXK_AUTOCVT: "ON"
        _CEE_RUNOPTS: "FILETAG(AUTOCVT,AUTOTAG) POSIX(ON)"
        _TAG_REDIR_ERR: "txt"
        _TAG_REDIR_IN: "txt"
        _TAG_REDIR_OUT: "txt"
        LANG: "C"
            
      collections:
        - ibm.ibm_zos_core
       
      vars:
        ansible_ssh_pipelining: no
            
      tasks:
       - name: testing command and saving result
         block:
    
         - name: issuing comand adn collecting results
           zos_operator:
             cmd: "d a,l"
           register: resultado
    
         - name: "Set variables - only to test"
           set_fact:
             display: "{{ resultado.content }}"
    
         - name: debuging
           debug:
             var: display         
    
         - name: saving to file
           zos_copy:
             content: "{{ display }}"
             dest: /u/LO1754A/Ansible/Files/display.txt
    

     

    The full traceback is:
    Traceback (most recent call last):
    File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 147, in run
    res = self._execute()
    File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 665, in _execute
    result = self._handler.run(task_vars=variables)
    File "/usr/share/ansible/collections/ansible_collections/ibm/ibm_zos_core/plugins/action/zos_copy.py", line 152, in run
    os.remove(local_content)
    UnboundLocalError: local variable 'local_content' referenced before assignment
    fatal: [nwrd]: FAILED! => {
    "msg": "Unexpected failure during module execution.",
    "stdout": ""
    }



     



    ------------------------------
    Fernando Alfredo Custodio
    ------------------------------


  • 2.  RE: Saving a result from command (zos_operator) to file (zos_copy) - Doesn´t accept registered variable

    Posted Fri May 05, 2023 01:25 AM

    Hello Fernando Alfredo Custodio

    I have not tried your scenario yet, but wondering if you tried any of these things that might help narrow down where the issue is:

    - Use a hard coded value instead of the fact
    - Use the var resultado.content instead of the fact
    - Use vars: and define your a var at the top 

    Below is an example snippet of one of mine but I defined the vars for it like so:

    ---
    - hosts: zvm
      collections:
        - ibm.ibm_zos_core
      gather_facts: false
      vars:
        PDSE: "ANSIBLE.COBOL.LOADLIB"
        PDS: "IMSTESTL.TNUC0"
        MEM_PGM: "HELLOPGM"
        MEM_TEST: "HELLODDD"
        USS_EXE: "/tmp/HIPGMUSS"
        VOLUME_PDSE: "SCR03"
        VOLUME_PDS: "USER01"
        USS_DZIP: "/tmp/temp_backup.dzp"
        USS_BINDER_JCL: "/tmp/binder.jcl"
        USS_SUB_EXE: "/tmp/pgmexe.jcl"
    
      environment: "{{ environment_vars }}"
    
      tasks:
        - name: Generate "{{ USS_SUB_EXE }}" on USS to execute library
          zos_copy:
            content: |
              //{{ MEM_TEST }}  JOB MSGCLASS=H,MSGLEVEL=(1,1),NOTIFY=&SYSUID,REGION=0M
              //STEP1    EXEC PGM={{ MEM_TEST }}
              //STEPLIB DD DSN={{ PDSE }},DISP=SHR
              //SYSPRINT DD SYSOUT=*
              //SYSOUT   DD SYSOUT=*
              //
            dest: "{{ USS_SUB_EXE }}"
            force: yes
          register: result
          tags: ocopy


    Looking at your error though, i don't think you should have seen a traceback , likely something got by the exceptions, but it does look like the fact is not available yet and not sure why without testing. Some specualtion is that the operator is executing on the managed node and zos_copy being made of two parts, a plugin and module, the plugin here is throwing the error, maybe the fact has not been made avaialbe to the plugin running on the controller. 

    Source in question:

     if content:
                    try:
                        local_content = _write_content_to_temp_file(content)
                        transfer_res = self._copy_to_remote(
                            local_content, ignore_stderr=ignore_sftp_stderr
                        )
                    finally:
                        os.remove(local_content)


    ------------------------------
    Demetri Dimatos
    IBM zOS Ansible Core Senior Technical Lead
    IBM
    San Jose CA
    ------------------------------