Ansibleを用いたz/OSカスタマイズ自動化 - (3) Ansible提供モジュールを試す

# はじめに
先の記事ではz/OS管理用に提供されているモジュールでどんなことができるのかをいくつか試してみました。
当記事では、Ansibleが標準で提供されているモジュールでz/OS管理に使えそうなものを試してみます。

## 関連記事
[Ansibleを用いたz/OSカスタマイズ自動化 - (1) 概要/基本構成](https://qiita.com/tomotagwork/items/44e6c02010c6e9025273)
[Ansibleを用いたz/OSカスタマイズ自動化 - (2) z/OS Core Collectionを試す](https://qiita.com/tomotagwork/items/2625d2d8a4a4aad6960b)
[Ansibleを用いたz/OSカスタマイズ自動化 - (3) Ansible提供モジュールを試す](https://qiita.com/tomotagwork/items/56ed181a209648b1e053)
[Ansibleを用いたz/OSカスタマイズ自動化 - (4) 雑多な考察](https://qiita.com/tomotagwork/items/ffd488a8e6e0f9e3b6d6)

# Ansible提供モジュールを試す

Ansibleでは汎用的に使用できるモジュールがいくつか提供されています。例えば以下のようなAnsible.Builtinと呼ばれるモジュール群です。
参考: [Ansible.Builtin](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/index.html)

Ansible Control ノードから、Managed Nodeであるz/OSを管理する場合、他のプラットフォームと同様SSH経由で管理されることになります。SSHで接続する先はz/OSのUSS(Unix System Service)環境ですので、Unixとして互換性のある部分についてはAnsible提供の汎用的なモジュールをそのまま利用できる場合があります。
debugやinclude_roleなどAnsible Control Node内で機能するものはもちろん利用できますが、それ以外にもManaged Node側を直接的に管理する際に使えそうなモジュールをここでは試してみます。

## commandモジュール
参考: [ansible.builtin.command module – Execute commands on targets](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/command_module.html#ansible-collections-ansible-builtin-command-module)

USS上のコマンドやスクリプトを実行するのに利用できます。
(より自由度の高いシェルのコマンドを実行したいのであれば[ansible.builtin.shell](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html#ansible-collections-ansible-builtin-shell-module)というのもあるのでそちらを利用するのがよさそうです。)

commandモジュール経由でいくつかUSS上のコマンドを実行するPlaybookを作成してみます。

```yaml:ansible_builtin_command.yml
- name: execute command
  hosts: ezdwazi04 
  environment: "{{ environment_vars }}"
  vars:
    new_dir: "/tmp/BBB"
  gather_facts: no
  
  tasks:
    - name: list env
      ansible.builtin.command:
        cmd: "env"
      register: tmp_result
    
    - name: debug
      debug:
        var: tmp_result

    - name: list files
      ansible.builtin.command:
        cmd: "ls -la"
      register: tmp_result
    
    - name: debug
      debug:
        var: tmp_result

    - name: create directory
      ansible.builtin.command:
        cmd: "mkdir -p {{ new_dir }}"
      register: tmp_result
    
    - name: debug
      debug:
        var: tmp_result

    - name: tsocmd1
      ansible.builtin.command:
        cmd: "tsocmd \"LISTUSER\""
      register: tmp_result
      
    - name: debug
      debug:
        var: tmp_result

    - name: tsocmd2
      ansible.builtin.command:
        cmd: "tsocmd \"LISTUSER BBB\""
      ignore_errors: yes
      register: tmp_result
      
    - name: debug
      debug:
        var: tmp_result
```

結果がどのようにハンドリングできそうかを確認するためにdebugによる結果出力をそれぞれ行っていますが、実質試しているのは以下のようなコマンドです。

- 「env」コマンド
- 「ls -la」コマンド
- 「mkdir -p xxx」コマンド
- 「tsocmd "LISTUSER"」コマンド
- 「tsocmd "LISTUSER BBB"」コマンド (エラーケース)

上のPlaybookを実行してみます。

<details><summary><font color="blue">実行例</font></summary><div>

```sh:実行例
user01@IBM-PF3ALW3Q:~/Ansible/VSCode_workspace/my_ansible_zos_sample_test$ ansible-playbook sample/ansible_builtin_command.yml 
Using /home/user01/Ansible/VSCode_workspace/my_ansible_zos_sample_test/ansible.cfg as config file

PLAY [execute command] *************************************************************************************************************************************************************

TASK [list env] ********************************************************************************************************************************************************************
changed: [ezdwazi04] => {
    "changed": true,
    "cmd": [
        "env"
    ],
    "delta": "0:00:00.374645",
    "end": "2024-04-29 04:39:10.673266",
    "rc": 0,
    "start": "2024-04-29 04:39:10.298621"
}

STDOUT:

MAIL=/usr/mail/IBMUSER
PATH=/usr/lpp/IBM/zoautil/bin:/usr/lpp/IBM/cyp/v3r11/pyz/bin:/bin:/var/bin
_TAG_REDIR_IN=txt
SSH_CLIENT=xx.xx.xx.xx 61338 22
_BPXK_AUTOCVT=ALL
SHELL=/bin/sh
SSH_TTY=/dev/ttyp0001
ZOAU_HOME=/usr/lpp/IBM/zoautil
_CEE_RUNOPTS=FILETAG(AUTOCVT,AUTOTAG) POSIX(ON)
_=/usr/lpp/IBM/cyp/v3r11/pyz/bin/python3
LOGNAME=IBMUSER
LANG=C
LIBPATH=/usr/lpp/IBM/cyp/v3r11/pyz/bin:/usr/lpp/IBM/cyp/v3r11/pyz/lib:/usr/lpp/IBM/zoautil/lib:/usr/lpp/IBM/cyp/v3r11/pyz/lib:/lib:/usr/lib:.:/lib
_TAG_REDIR_OUT=txt
USER=IBMUSER
TERM=xterm-256color
_BPX_SHAREAS=YES
HOME=/u/ibmuser
PYTHONPATH=/usr/lpp/IBM/cyp/v3r11/pyz/lib
SSH_CONNECTION=xx.xx.xx.xx 61338 10.1.1.2 22
_BPX_BATCH_SPAWN=YES
_TAG_REDIR_ERR=txt
_EDC_SIG_DFLT=1
_EDC_SUSV3=1
SETUPTOOLS_USE_DISTUTILS=stdlib

TASK [debug] ***********************************************************************************************************************************************************************
ok: [ezdwazi04] => {
    "tmp_result": {
        "changed": true,
        "cmd": [
            "env"
        ],
        "delta": "0:00:00.374645",
        "end": "2024-04-29 04:39:10.673266",
        "failed": false,
        "msg": "",
        "rc": 0,
        "start": "2024-04-29 04:39:10.298621",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "MAIL=/usr/mail/IBMUSER\nPATH=/usr/lpp/IBM/zoautil/bin:/usr/lpp/IBM/cyp/v3r11/pyz/bin:/bin:/var/bin\n_TAG_REDIR_IN=txt\nSSH_CLIENT=xx.xx.xx.xx 61338 22\n_BPXK_AUTOCVT=ALL\nSHELL=/bin/sh\nSSH_TTY=/dev/ttyp0001\nZOAU_HOME=/usr/lpp/IBM/zoautil\n_CEE_RUNOPTS=FILETAG(AUTOCVT,AUTOTAG) POSIX(ON)\n_=/usr/lpp/IBM/cyp/v3r11/pyz/bin/python3\nLOGNAME=IBMUSER\nLANG=C\nLIBPATH=/usr/lpp/IBM/cyp/v3r11/pyz/bin:/usr/lpp/IBM/cyp/v3r11/pyz/lib:/usr/lpp/IBM/zoautil/lib:/usr/lpp/IBM/cyp/v3r11/pyz/lib:/lib:/usr/lib:.:/lib\n_TAG_REDIR_OUT=txt\nUSER=IBMUSER\nTERM=xterm-256color\n_BPX_SHAREAS=YES\nHOME=/u/ibmuser\nPYTHONPATH=/usr/lpp/IBM/cyp/v3r11/pyz/lib\nSSH_CONNECTION=xx.xx.xx.xx 61338 10.1.1.2 22\n_BPX_BATCH_SPAWN=YES\n_TAG_REDIR_ERR=txt\n_EDC_SIG_DFLT=1\n_EDC_SUSV3=1\nSETUPTOOLS_USE_DISTUTILS=stdlib",
        "stdout_lines": [
            "MAIL=/usr/mail/IBMUSER",
            "PATH=/usr/lpp/IBM/zoautil/bin:/usr/lpp/IBM/cyp/v3r11/pyz/bin:/bin:/var/bin",
            "_TAG_REDIR_IN=txt",
            "SSH_CLIENT=xx.xx.xx.xx 61338 22",
            "_BPXK_AUTOCVT=ALL",
            "SHELL=/bin/sh",
            "SSH_TTY=/dev/ttyp0001",
            "ZOAU_HOME=/usr/lpp/IBM/zoautil",
            "_CEE_RUNOPTS=FILETAG(AUTOCVT,AUTOTAG) POSIX(ON)",
            "_=/usr/lpp/IBM/cyp/v3r11/pyz/bin/python3",
            "LOGNAME=IBMUSER",
            "LANG=C",
            "LIBPATH=/usr/lpp/IBM/cyp/v3r11/pyz/bin:/usr/lpp/IBM/cyp/v3r11/pyz/lib:/usr/lpp/IBM/zoautil/lib:/usr/lpp/IBM/cyp/v3r11/pyz/lib:/lib:/usr/lib:.:/lib",
            "_TAG_REDIR_OUT=txt",
            "USER=IBMUSER",
            "TERM=xterm-256color",
            "_BPX_SHAREAS=YES",
            "HOME=/u/ibmuser",
            "PYTHONPATH=/usr/lpp/IBM/cyp/v3r11/pyz/lib",
            "SSH_CONNECTION=xx.xx.xx.xx 61338 10.1.1.2 22",
            "_BPX_BATCH_SPAWN=YES",
            "_TAG_REDIR_ERR=txt",
            "_EDC_SIG_DFLT=1",
            "_EDC_SUSV3=1",
            "SETUPTOOLS_USE_DISTUTILS=stdlib"
        ]
    }
}

TASK [list files] ******************************************************************************************************************************************************************
changed: [ezdwazi04] => {
    "changed": true,
    "cmd": [
        "ls",
        "-la"
    ],
    "delta": "0:00:00.377732",
    "end": "2024-04-29 04:39:12.760955",
    "rc": 0,
    "start": "2024-04-29 04:39:12.383223"
}

STDOUT:

total 380
drwxr-xr-x   7 BPXROOT  SYS1        8192 Apr 28 03:09 .
dr-xr-xr-x  16 BPXROOT  TTY            0 Apr 28 04:31 ..
drwx------   3 BPXROOT  SYS1        8192 Mar  6 16:15 .ansible
-rw-r--r--   1 BPXROOT  SYS1         158 Mar  6 16:19 .bashrc
drwxr-xr-x   3 BPXROOT  SYS1        8192 Mar  6 16:41 .cache
drwx------   2 BPXROOT  SYS1        8192 Mar  6 17:27 .dbb
-rw-r--r--   1 BPXROOT  SYS1          36 Mar  6 17:27 .gitconfig
-rwxr-xr-x   1 BPXROOT  SYS1         916 Mar  6 16:49 .profile_for_ibmdb
drwxr-xr-x   3 BPXROOT  SYS1        8192 Mar  6 17:07 .rseapi
-rw-------   1 BPXROOT  SYS1        1768 Apr 29 04:38 .sh_history
drwx------   2 BPXROOT  SYS1        8192 Mar  6 16:55 .ssh
-rw-------   1 BPXROOT  SYS1        1395 Apr 28 00:24 common_cacert
-rwxr-xr-x   1 BPXROOT  SYS1         606 Mar  6 16:22 db2clp.sh
-rw-r--r--   1 BPXROOT  SYS1        1203 Mar  6 16:39 dbbtest
-rw-------   1 BPXROOT  SYS1          16 Mar 12 18:54 env-overrides
-rwxr-xr-x   1 BPXROOT  SYS1         457 Mar  6 16:49 ibm_db_test.py
-rwxr-xr-x   1 BPXROOT  SYS1         197 Mar  6 16:22 jdbc.properties
-rwxr-xr-x   1 BPXROOT  SYS1         346 Mar  6 16:49 odbcini
-rwxr-xr-x   1 BPXROOT  SYS1        1093 Mar  6 16:50 pyzoau
-rw-r--r--   1 BPXROOT  SYS1        1093 Mar  6 16:41 pyzoau.py
-rwxr-xr-x   1 BPXROOT  SYS1        1203 Mar  6 16:50 rundbb
-rwxr-xr-x   1 BPXROOT  SYS1         195 Mar  6 16:49 runibmdb
-rwxr-xr-x   1 BPXROOT  SYS1         122 Mar  6 16:22 sql.txt
-rw-r--r--   1 BPXROOT  SYS1       10240 Apr 28 03:09 test.tar
-rwxr-xr-x   1 BPXROOT  SYS1         151 Mar  6 16:50 wazidtst.sh

TASK [debug] ***********************************************************************************************************************************************************************
ok: [ezdwazi04] => {
    "tmp_result": {
        "changed": true,
        "cmd": [
            "ls",
            "-la"
        ],
        "delta": "0:00:00.377732",
        "end": "2024-04-29 04:39:12.760955",
        "failed": false,
        "msg": "",
        "rc": 0,
        "start": "2024-04-29 04:39:12.383223",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "total 380\ndrwxr-xr-x   7 BPXROOT  SYS1        8192 Apr 28 03:09 .\ndr-xr-xr-x  16 BPXROOT  TTY            0 Apr 28 04:31 ..\ndrwx------   3 BPXROOT  SYS1        8192 Mar  6 16:15 .ansible\n-rw-r--r--   1 BPXROOT  SYS1         158 Mar  6 16:19 .bashrc\ndrwxr-xr-x   3 BPXROOT  SYS1        8192 Mar  6 16:41 .cache\ndrwx------   2 BPXROOT  SYS1        8192 Mar  6 17:27 .dbb\n-rw-r--r--   1 BPXROOT  SYS1          36 Mar  6 17:27 .gitconfig\n-rwxr-xr-x   1 BPXROOT  SYS1         916 Mar  6 16:49 .profile_for_ibmdb\ndrwxr-xr-x   3 BPXROOT  SYS1        8192 Mar  6 17:07 .rseapi\n-rw-------   1 BPXROOT  SYS1        1768 Apr 29 04:38 .sh_history\ndrwx------   2 BPXROOT  SYS1        8192 Mar  6 16:55 .ssh\n-rw-------   1 BPXROOT  SYS1        1395 Apr 28 00:24 common_cacert\n-rwxr-xr-x   1 BPXROOT  SYS1         606 Mar  6 16:22 db2clp.sh\n-rw-r--r--   1 BPXROOT  SYS1        1203 Mar  6 16:39 dbbtest\n-rw-------   1 BPXROOT  SYS1          16 Mar 12 18:54 env-overrides\n-rwxr-xr-x   1 BPXROOT  SYS1         457 Mar  6 16:49 ibm_db_test.py\n-rwxr-xr-x   1 BPXROOT  SYS1         197 Mar  6 16:22 jdbc.properties\n-rwxr-xr-x   1 BPXROOT  SYS1         346 Mar  6 16:49 odbcini\n-rwxr-xr-x   1 BPXROOT  SYS1        1093 Mar  6 16:50 pyzoau\n-rw-r--r--   1 BPXROOT  SYS1        1093 Mar  6 16:41 pyzoau.py\n-rwxr-xr-x   1 BPXROOT  SYS1        1203 Mar  6 16:50 rundbb\n-rwxr-xr-x   1 BPXROOT  SYS1         195 Mar  6 16:49 runibmdb\n-rwxr-xr-x   1 BPXROOT  SYS1         122 Mar  6 16:22 sql.txt\n-rw-r--r--   1 BPXROOT  SYS1       10240 Apr 28 03:09 test.tar\n-rwxr-xr-x   1 BPXROOT  SYS1         151 Mar  6 16:50 wazidtst.sh",
        "stdout_lines": [
            "total 380",
            "drwxr-xr-x   7 BPXROOT  SYS1        8192 Apr 28 03:09 .",
            "dr-xr-xr-x  16 BPXROOT  TTY            0 Apr 28 04:31 ..",
            "drwx------   3 BPXROOT  SYS1        8192 Mar  6 16:15 .ansible",
            "-rw-r--r--   1 BPXROOT  SYS1         158 Mar  6 16:19 .bashrc",
            "drwxr-xr-x   3 BPXROOT  SYS1        8192 Mar  6 16:41 .cache",
            "drwx------   2 BPXROOT  SYS1        8192 Mar  6 17:27 .dbb",
            "-rw-r--r--   1 BPXROOT  SYS1          36 Mar  6 17:27 .gitconfig",
            "-rwxr-xr-x   1 BPXROOT  SYS1         916 Mar  6 16:49 .profile_for_ibmdb",
            "drwxr-xr-x   3 BPXROOT  SYS1        8192 Mar  6 17:07 .rseapi",
            "-rw-------   1 BPXROOT  SYS1        1768 Apr 29 04:38 .sh_history",
            "drwx------   2 BPXROOT  SYS1        8192 Mar  6 16:55 .ssh",
            "-rw-------   1 BPXROOT  SYS1        1395 Apr 28 00:24 common_cacert",
            "-rwxr-xr-x   1 BPXROOT  SYS1         606 Mar  6 16:22 db2clp.sh",
            "-rw-r--r--   1 BPXROOT  SYS1        1203 Mar  6 16:39 dbbtest",
            "-rw-------   1 BPXROOT  SYS1          16 Mar 12 18:54 env-overrides",
            "-rwxr-xr-x   1 BPXROOT  SYS1         457 Mar  6 16:49 ibm_db_test.py",
            "-rwxr-xr-x   1 BPXROOT  SYS1         197 Mar  6 16:22 jdbc.properties",
            "-rwxr-xr-x   1 BPXROOT  SYS1         346 Mar  6 16:49 odbcini",
            "-rwxr-xr-x   1 BPXROOT  SYS1        1093 Mar  6 16:50 pyzoau",
            "-rw-r--r--   1 BPXROOT  SYS1        1093 Mar  6 16:41 pyzoau.py",
            "-rwxr-xr-x   1 BPXROOT  SYS1        1203 Mar  6 16:50 rundbb",
            "-rwxr-xr-x   1 BPXROOT  SYS1         195 Mar  6 16:49 runibmdb",
            "-rwxr-xr-x   1 BPXROOT  SYS1         122 Mar  6 16:22 sql.txt",
            "-rw-r--r--   1 BPXROOT  SYS1       10240 Apr 28 03:09 test.tar",
            "-rwxr-xr-x   1 BPXROOT  SYS1         151 Mar  6 16:50 wazidtst.sh"
        ]
    }
}

TASK [create directory] ************************************************************************************************************************************************************
changed: [ezdwazi04] => {
    "changed": true,
    "cmd": [
        "mkdir",
        "-p",
        "/tmp/BBB"
    ],
    "delta": "0:00:00.369362",
    "end": "2024-04-29 04:39:14.804439",
    "rc": 0,
    "start": "2024-04-29 04:39:14.435077"
}

TASK [debug] ***********************************************************************************************************************************************************************
ok: [ezdwazi04] => {
    "tmp_result": {
        "changed": true,
        "cmd": [
            "mkdir",
            "-p",
            "/tmp/BBB"
        ],
        "delta": "0:00:00.369362",
        "end": "2024-04-29 04:39:14.804439",
        "failed": false,
        "msg": "",
        "rc": 0,
        "start": "2024-04-29 04:39:14.435077",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "",
        "stdout_lines": []
    }
}

TASK [tsocmd1] *********************************************************************************************************************************************************************
[DEPRECATION WARNING]: Module "ansible.builtin.command" returned non UTF-8 data in the JSON response. This will become an error in the future. This feature will be removed in 
version 2.18. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
[DEPRECATION WARNING]: Non UTF-8 encoded data replaced with "?" while displaying text to stdout/stderr, this is temporary and will become an error. This feature will be removed in
 version 2.18. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [ezdwazi04] => {
    "changed": true,
    "cmd": [
        "tsocmd",
        "LISTUSER"
    ],
    "delta": "0:00:00.418244",
    "end": "2024-04-29 04:39:16.897239",
    "rc": 0,
    "start": "2024-04-29 04:39:16.478995"
}

STDOUT:

USER=IBMUSER  NAME=?  OWNER=IBMUSER   CREATED=21.243  
 DEFAULT-GROUP=SYS1     PASSDATE=24.118 PASS-INTERVAL= 30 PHRASEDATE=24.072 
 ATTRIBUTES=SPECIAL OPERATIONS  
 ATTRIBUTES=PASSPHRASE  
 REVOKE DATE=NONE   RESUME DATE=NONE 
 LAST-ACCESS=24.120/00:39:16   
 CLASS AUTHORIZATIONS=NONE  
 NO-INSTALLATION-DATA
 NO-MODEL-NAME
 LOGON ALLOWED   (DAYS)          (TIME)
 ---------------------------------------------
 ANYDAY                          ANYTIME
  GROUP=SYS1      AUTH=JOIN     CONNECT-OWNER=IBMUSER   CONNECT-DATE=21.243
    CONNECTS=15,380  UACC=READ     LAST-CONNECT=24.120/00:39:16
    CONNECT ATTRIBUTES=NONE  
    REVOKE DATE=NONE   RESUME DATE=NONE    
  GROUP=IZUADMIN  AUTH=USE      CONNECT-OWNER=IBMUSER   CONNECT-DATE=21.245
    CONNECTS=    00  UACC=NONE     LAST-CONNECT=UNKNOWN        
    CONNECT ATTRIBUTES=NONE  
    REVOKE DATE=NONE   RESUME DATE=NONE    
  GROUP=IZUSECAD  AUTH=USE      CONNECT-OWNER=IBMUSER   CONNECT-DATE=21.245
    CONNECTS=    00  UACC=NONE     LAST-CONNECT=UNKNOWN        
    CONNECT ATTRIBUTES=NONE  
    REVOKE DATE=NONE   RESUME DATE=NONE    
  GROUP=IYU       AUTH=USE      CONNECT-OWNER=IBMUSER   CONNECT-DATE=21.245
    CONNECTS=    00  UACC=NONE     LAST-CONNECT=UNKNOWN        
    CONNECT ATTRIBUTES=NONE  
    REVOKE DATE=NONE   RESUME DATE=NONE    
  GROUP=CFZADMGP  AUTH=USE      CONNECT-OWNER=IBMUSER   CONNECT-DATE=22.132
    CONNECTS=    00  UACC=NONE     LAST-CONNECT=UNKNOWN        
    CONNECT ATTRIBUTES=NONE  
    REVOKE DATE=NONE   RESUME DATE=NONE    
SECURITY-LEVEL=NONE SPECIFIED
CATEGORY-AUTHORIZATION
 NONE SPECIFIED
SECURITY-LABEL=NONE SPECIFIED


STDERR:

LISTUSER

TASK [debug] ***********************************************************************************************************************************************************************
ok: [ezdwazi04] => {
    "tmp_result": {
        "changed": true,
        "cmd": [
            "tsocmd",
            "LISTUSER"
        ],
        "delta": "0:00:00.418244",
        "end": "2024-04-29 04:39:16.897239",
        "failed": false,
        "msg": "",
        "rc": 0,
        "start": "2024-04-29 04:39:16.478995",
        "stderr": "LISTUSER",
        "stderr_lines": [
            "LISTUSER"
        ],
        "stdout": "USER=IBMUSER  NAME=?  OWNER=IBMUSER   CREATED=21.243  \n DEFAULT-GROUP=SYS1     PASSDATE=24.118 PASS-INTERVAL= 30 PHRASEDATE=24.072 \n ATTRIBUTES=SPECIAL OPERATIONS  \n ATTRIBUTES=PASSPHRASE  \n REVOKE DATE=NONE   RESUME DATE=NONE \n LAST-ACCESS=24.120/00:39:16   \n CLASS AUTHORIZATIONS=NONE  \n NO-INSTALLATION-DATA\n NO-MODEL-NAME\n LOGON ALLOWED   (DAYS)          (TIME)\n ---------------------------------------------\n ANYDAY                          ANYTIME\n  GROUP=SYS1      AUTH=JOIN     CONNECT-OWNER=IBMUSER   CONNECT-DATE=21.243\n    CONNECTS=15,380  UACC=READ     LAST-CONNECT=24.120/00:39:16\n    CONNECT ATTRIBUTES=NONE  \n    REVOKE DATE=NONE   RESUME DATE=NONE    \n  GROUP=IZUADMIN  AUTH=USE      CONNECT-OWNER=IBMUSER   CONNECT-DATE=21.245\n    CONNECTS=    00  UACC=NONE     LAST-CONNECT=UNKNOWN        \n    CONNECT ATTRIBUTES=NONE  \n    REVOKE DATE=NONE   RESUME DATE=NONE    \n  GROUP=IZUSECAD  AUTH=USE      CONNECT-OWNER=IBMUSER   CONNECT-DATE=21.245\n    CONNECTS=    00  UACC=NONE     LAST-CONNECT=UNKNOWN        \n    CONNECT ATTRIBUTES=NONE  \n    REVOKE DATE=NONE   RESUME DATE=NONE    \n  GROUP=IYU       AUTH=USE      CONNECT-OWNER=IBMUSER   CONNECT-DATE=21.245\n    CONNECTS=    00  UACC=NONE     LAST-CONNECT=UNKNOWN        \n    CONNECT ATTRIBUTES=NONE  \n    REVOKE DATE=NONE   RESUME DATE=NONE    \n  GROUP=CFZADMGP  AUTH=USE      CONNECT-OWNER=IBMUSER   CONNECT-DATE=22.132\n    CONNECTS=    00  UACC=NONE     LAST-CONNECT=UNKNOWN        \n    CONNECT ATTRIBUTES=NONE  \n    REVOKE DATE=NONE   RESUME DATE=NONE    \nSECURITY-LEVEL=NONE SPECIFIED\nCATEGORY-AUTHORIZATION\n NONE SPECIFIED\nSECURITY-LABEL=NONE SPECIFIED",
        "stdout_lines": [
            "USER=IBMUSER  NAME=?  OWNER=IBMUSER   CREATED=21.243  ",
            " DEFAULT-GROUP=SYS1     PASSDATE=24.118 PASS-INTERVAL= 30 PHRASEDATE=24.072 ",
            " ATTRIBUTES=SPECIAL OPERATIONS  ",
            " ATTRIBUTES=PASSPHRASE  ",
            " REVOKE DATE=NONE   RESUME DATE=NONE ",
            " LAST-ACCESS=24.120/00:39:16   ",
            " CLASS AUTHORIZATIONS=NONE  ",
            " NO-INSTALLATION-DATA",
            " NO-MODEL-NAME",
            " LOGON ALLOWED   (DAYS)          (TIME)",
            " ---------------------------------------------",
            " ANYDAY                          ANYTIME",
            "  GROUP=SYS1      AUTH=JOIN     CONNECT-OWNER=IBMUSER   CONNECT-DATE=21.243",
            "    CONNECTS=15,380  UACC=READ     LAST-CONNECT=24.120/00:39:16",
            "    CONNECT ATTRIBUTES=NONE  ",
            "    REVOKE DATE=NONE   RESUME DATE=NONE    ",
            "  GROUP=IZUADMIN  AUTH=USE      CONNECT-OWNER=IBMUSER   CONNECT-DATE=21.245",
            "    CONNECTS=    00  UACC=NONE     LAST-CONNECT=UNKNOWN        ",
            "    CONNECT ATTRIBUTES=NONE  ",
            "    REVOKE DATE=NONE   RESUME DATE=NONE    ",
            "  GROUP=IZUSECAD  AUTH=USE      CONNECT-OWNER=IBMUSER   CONNECT-DATE=21.245",
            "    CONNECTS=    00  UACC=NONE     LAST-CONNECT=UNKNOWN        ",
            "    CONNECT ATTRIBUTES=NONE  ",
            "    REVOKE DATE=NONE   RESUME DATE=NONE    ",
            "  GROUP=IYU       AUTH=USE      CONNECT-OWNER=IBMUSER   CONNECT-DATE=21.245",
            "    CONNECTS=    00  UACC=NONE     LAST-CONNECT=UNKNOWN        ",
            "    CONNECT ATTRIBUTES=NONE  ",
            "    REVOKE DATE=NONE   RESUME DATE=NONE    ",
            "  GROUP=CFZADMGP  AUTH=USE      CONNECT-OWNER=IBMUSER   CONNECT-DATE=22.132",
            "    CONNECTS=    00  UACC=NONE     LAST-CONNECT=UNKNOWN        ",
            "    CONNECT ATTRIBUTES=NONE  ",
            "    REVOKE DATE=NONE   RESUME DATE=NONE    ",
            "SECURITY-LEVEL=NONE SPECIFIED",
            "CATEGORY-AUTHORIZATION",
            " NONE SPECIFIED",
            "SECURITY-LABEL=NONE SPECIFIED"
        ]
    }
}

TASK [tsocmd2] *********************************************************************************************************************************************************************
fatal: [ezdwazi04]: FAILED! => {
    "changed": true,
    "cmd": [
        "tsocmd",
        "LISTUSER BBB"
    ],
    "delta": "0:00:00.411048",
    "end": "2024-04-29 04:39:18.957882",
    "rc": 4,
    "start": "2024-04-29 04:39:18.546834"
}

STDOUT:

ICH30001I UNABLE TO LOCATE USER    ENTRY BBB              


STDERR:

LISTUSER BBB


MSG:

non-zero return code
...ignoring

TASK [debug] ***********************************************************************************************************************************************************************
ok: [ezdwazi04] => {
    "tmp_result": {
        "changed": true,
        "cmd": [
            "tsocmd",
            "LISTUSER BBB"
        ],
        "delta": "0:00:00.411048",
        "end": "2024-04-29 04:39:18.957882",
        "failed": true,
        "msg": "non-zero return code",
        "rc": 4,
        "start": "2024-04-29 04:39:18.546834",
        "stderr": "LISTUSER BBB",
        "stderr_lines": [
            "LISTUSER BBB"
        ],
        "stdout": "ICH30001I UNABLE TO LOCATE USER    ENTRY BBB              ",
        "stdout_lines": [
            "ICH30001I UNABLE TO LOCATE USER    ENTRY BBB              "
        ]
    }
}

PLAY RECAP *************************************************************************************************************************************************************************
ezdwazi04                  : ok=10   changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1 
```

</div></details>

普通にUSS上でコマンド実行を行えることが確認できました。USS上で単純なコマンド実行やスクリプト実行を行うケースはこのモジュールが使えそうです。
(※tsocmdは興味本位で試してみたものです。TSO Commandを実行したいのであればz/OS Core Collectionで専用モジュールが提供されているのでそちらを利用するのがよいでしょう。)

## fileモジュール
参考: [ansible.builtin.file module – Manage files and file properties](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html#ansible-collections-ansible-builtin-file-module)

USS上のディレクトリやファイルの属性管理等を行うためのモジュールです。
まず、ディレクトリ/ファイルの作成、属性変更を行うPlaybookを作成してみます。

```yaml:ansible_builtin_file.yml
- name: directory and file management
  hosts: ezdwazi04 
  environment: "{{ environment_vars }}"
  vars:
    new_dir: "/tmp/CCC"
    new_file: "{{ new_dir }}/ccc.txt"
    owner: "IZUSVR"
    group: "IZUADMIN"
  gather_facts: no
  
  tasks:

    - name: create directory
      ansible.builtin.file:
        path: "{{ new_dir }}"
        state: directory
        mode: "0775"
      register: tmp_result

    - name: create file
      ansible.builtin.file:
        path: "{{ new_file }}"
        state: touch
        mode: "0664"
      register: tmp_result

    - name: change owner/group
      ansible.builtin.file:
        path: "{{ new_dir }}"
        state: directory
        recurse: yes
        owner: "{{ owner }}"
        group: "{{ group }}"
     
    - name: list files
      ansible.builtin.command:
        chdir: "{{ new_dir }}"
        cmd: "ls -la"
```

以下の操作を試しています。

- ディレクトリ作成
- ファイル作成
- ディレクトリ配下のowner, group変更

最後のタスクでファイルのステータスを確認するために"ls -la"コマンドを実行してしています。

上のPlaybookを実行してみます。

<details><summary><font color="blue">実行例</font></summary><div>

```sh:実行例
user01@IBM-PF3ALW3Q:~/Ansible/VSCode_workspace/my_ansible_zos_sample_test$ ansible-playbook sample/ansible_builtin_file.yml 
Using /home/user01/Ansible/VSCode_workspace/my_ansible_zos_sample_test/ansible.cfg as config file

PLAY [directory and file management] *************************************************************************************************************************************************************

TASK [create directory] ************************************************************************************************************************************************************
changed: [ezdwazi04] => {
    "changed": true,
    "gid": 0,
    "group": "SYS1",
    "mode": "0775",
    "owner": "BPXROOT",
    "path": "/tmp/CCC",
    "size": 4096,
    "state": "directory",
    "uid": 0
}

TASK [create file] *****************************************************************************************************************************************************************
changed: [ezdwazi04] => {
    "changed": true,
    "dest": "/tmp/CCC/ccc.txt",
    "gid": 0,
    "group": "SYS1",
    "mode": "0664",
    "owner": "BPXROOT",
    "size": 0,
    "state": "file",
    "uid": 0
}

TASK [change owner/group] **********************************************************************************************************************************************************
changed: [ezdwazi04] => {
    "changed": true,
    "gid": 9003,
    "group": "IZUADMIN",
    "mode": "0775",
    "owner": "IZUSVR",
    "path": "/tmp/CCC",
    "size": 4096,
    "state": "directory",
    "uid": 9010
}

TASK [list files] ******************************************************************************************************************************************************************
changed: [ezdwazi04] => {
    "changed": true,
    "cmd": [
        "ls",
        "-la"
    ],
    "delta": "0:00:00.375619",
    "end": "2024-04-29 04:50:09.958722",
    "rc": 0,
    "start": "2024-04-29 04:50:09.583103"
}

STDOUT:

total 24
drwxrwxr-x   2 IZUSVR   IZUADMIN    4096 Apr 29 04:50 .
drwxrwxrwt   8 BPXROOT  SYS1        8192 Apr 29 04:50 ..
-rw-rw-r--   1 IZUSVR   IZUADMIN       0 Apr 29 04:50 ccc.txt

PLAY RECAP *************************************************************************************************************************************************************************
ezdwazi04                  : ok=4    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
```

</div></details>

次に、上で作成したディレクトリを削除するPlaybookを作成してみます。

```yaml:ansible_builtin_file_remove.yml
- name: remove direcotory
  hosts: ezdwazi04 
  environment: "{{ environment_vars }}"
  vars:
    new_dir: "/tmp/CCC"
  gather_facts: no
  
  tasks:

    - name: remove directory
      ansible.builtin.file:
        path: "{{ new_dir }}"
        state: absent
      register: tmp_result
```

上のPlaybookを実行してみます。

<details><summary><font color="blue">実行例</font></summary><div>

```sh:実行例
user01@IBM-PF3ALW3Q:~/Ansible/VSCode_workspace/my_ansible_zos_sample_test$ ansible-playbook sample/ansible_builtin_file_remove.yml 
Using /home/user01/Ansible/VSCode_workspace/my_ansible_zos_sample_test/ansible.cfg as config file

PLAY [remove direcotory] ***********************************************************************************************************************************************************

TASK [remove directory] ************************************************************************************************************************************************************
changed: [ezdwazi04] => {
    "changed": true,
    "path": "/tmp/CCC",
    "state": "absent"
}

PLAY RECAP *************************************************************************************************************************************************************************
ezdwazi04                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
```

</div></details>

指定したディレクトリ配下が削除されました。

USS上のディレクトリやファイル操作の基本的なことはこのモジュールが使えそうです。


## findモジュール
[ansible.builtin.find module – Return a list of files based on specific criteria](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/find_module.html#ansible-collections-ansible-builtin-find-module)

特定の条件に合致するファイルを抽出するためのモジュールです。このモジュールを使用したPlaybookを作成してみます。

```yaml:ansible_builtin_find.yml
- name: find files
  hosts: ezdwazi04 
  environment: "{{ environment_vars }}"
  gather_facts: no
  
  tasks:
    - name: Recursively find /tmp files older than 1 days
      ansible.builtin.find:
        paths: /tmp
        age: 1d
        recurse: yes

    - name: Find /var/zosconnect log files equal or greater than 30 kilobytes ending with .log via regex
      ansible.builtin.find:
        paths: /var/zosconnect
        patterns: "^.*\\.log$"
        size: 30k
        use_regex: yes
        recurse: yes
```


上のPlaybookを実行してみます。

<details><summary><font color="blue">実行例</font></summary><div>

```sh:実行例
user01@IBM-PF3ALW3Q:~/Ansible/VSCode_workspace/my_ansible_zos_sample_test$ ansible-playbook sample/ansible_builtin_find.yml 
Using /home/user01/Ansible/VSCode_workspace/my_ansible_zos_sample_test/ansible.cfg as config file

PLAY [find files] ******************************************************************************************************************************************************************

TASK [Recursively find /tmp files older than 1 days] *******************************************************************************************************************************
ok: [ezdwazi04] => {
    "changed": false,
    "examined": 21,
    "files": [
        {
            "atime": 1714263873.0,
            "ctime": 1714263873.0,
            "dev": 7,
            "gid": 0,
            "gr_name": "SYS1",
            "inode": 6167,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0700",
            "mtime": 1714263873.0,
            "nlink": 1,
            "path": "/tmp/sshd.stderr",
            "pw_name": "BPXROOT",
            "rgrp": false,
            "roth": false,
            "rusr": true,
            "size": 0,
            "uid": 0,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": true
        },
        {
            "atime": 1714263873.0,
            "ctime": 1714263873.0,
            "dev": 7,
            "gid": 0,
            "gr_name": "SYS1",
            "inode": 6171,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0644",
            "mtime": 1714263873.0,
            "nlink": 1,
            "path": "/tmp/pagent.pid",
            "pw_name": "BPXROOT",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 3,
            "uid": 0,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        },
        {
            "atime": 1714278525.0,
            "ctime": 1714278523.0,
            "dev": 7,
            "gid": 0,
            "gr_name": "SYS1",
            "inode": 6304,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0644",
            "mtime": 1714278523.0,
            "nlink": 1,
            "path": "/tmp/ibmuser_ssh.tar",
            "pw_name": "BPXROOT",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 10240,
            "uid": 0,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        },
        {
            "atime": 1714278668.0,
            "ctime": 1714278666.0,
            "dev": 7,
            "gid": 0,
            "gr_name": "SYS1",
            "inode": 6280,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0644",
            "mtime": 1714278666.0,
            "nlink": 1,
            "path": "/tmp/TEST01.tar",
            "pw_name": "BPXROOT",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 20480,
            "uid": 0,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        },
        {
            "atime": 1714263926.0,
            "ctime": 1714263930.0,
            "dev": 7,
            "gid": 0,
            "gr_name": "SYS1",
            "inode": 6209,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0700",
            "mtime": 1714263930.0,
            "nlink": 1,
            "path": "/tmp/JVM.out",
            "pw_name": "BPXROOT",
            "rgrp": false,
            "roth": false,
            "rusr": true,
            "size": 0,
            "uid": 0,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": true
        },
        {
            "atime": 1714263926.0,
            "ctime": 1714263930.0,
            "dev": 7,
            "gid": 0,
            "gr_name": "SYS1",
            "inode": 6212,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0700",
            "mtime": 1714263930.0,
            "nlink": 1,
            "path": "/tmp/JVM.err",
            "pw_name": "BPXROOT",
            "rgrp": false,
            "roth": false,
            "rusr": true,
            "size": 0,
            "uid": 0,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": true
        },
        {
            "atime": 1714263926.0,
            "ctime": 1714263950.0,
            "dev": 7,
            "gid": 0,
            "gr_name": "SYS1",
            "inode": 6199,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0666",
            "mtime": 1714263926.0,
            "nlink": 1,
            "path": "/tmp/.com_ibm_tools_attach/_controller",
            "pw_name": "ZOSCSRV",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 0,
            "uid": 99996,
            "wgrp": true,
            "woth": true,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        },
        {
            "atime": 1714263926.0,
            "ctime": 1714263926.0,
            "dev": 7,
            "gid": 0,
            "gr_name": "SYS1",
            "inode": 6200,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0666",
            "mtime": 1714263926.0,
            "nlink": 1,
            "path": "/tmp/.com_ibm_tools_attach/_notifier",
            "pw_name": "ZOSCSRV",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 0,
            "uid": 99996,
            "wgrp": true,
            "woth": true,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        },
        {
            "atime": 1714263966.0,
            "ctime": 1714263966.0,
            "dev": 7,
            "gid": 9003,
            "gr_name": "IZUADMIN",
            "inode": 6231,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0666",
            "mtime": 1714263966.0,
            "nlink": 1,
            "path": "/tmp/.com_ibm_tools_attach/67108991/attachNotificationSync",
            "pw_name": "IZUSVR",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 0,
            "uid": 9010,
            "wgrp": true,
            "woth": true,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        },
        {
            "atime": 1714263966.0,
            "ctime": 1714263966.0,
            "dev": 7,
            "gid": 9003,
            "gr_name": "IZUADMIN",
            "inode": 6239,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0600",
            "mtime": 1714263966.0,
            "nlink": 1,
            "path": "/tmp/.com_ibm_tools_attach/67108991/attachInfo",
            "pw_name": "IZUSVR",
            "rgrp": false,
            "roth": false,
            "rusr": true,
            "size": 271,
            "uid": 9010,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        }
    ],
    "matched": 10,
    "skipped_paths": {}
}

MSG:

All paths examined

TASK [Find /var/zosconnect log files equal or greater than 30 kilobytes ending with .log via regex] ********************************************************************************
ok: [ezdwazi04] => {
    "changed": false,
    "examined": 182,
    "files": [
        {
            "atime": 1714263966.0,
            "ctime": 1714350725.0,
            "dev": 29,
            "gid": 0,
            "gr_name": "SYS1",
            "inode": 165,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0666",
            "mtime": 1714350725.0,
            "nlink": 1,
            "path": "/var/zosconnect/v3r0/servers/defaultServer/logs/messages.log",
            "pw_name": "ZOSCSRV",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 33312,
            "uid": 99996,
            "wgrp": true,
            "woth": true,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        }
    ],
    "matched": 1,
    "skipped_paths": {}
}

MSG:

All paths examined

PLAY RECAP *************************************************************************************************************************************************************************
ezdwazi04                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
```

</div></details>

ログファイルのメンテナンス等で使えそうです。