Ansibleを使用して必要なログファイルを取得する

# 目次
[経緯](#経緯)
[内容](#内容)
[応用](#応用)
[あとがき](#あとがき)

# 経緯

現場業務にて、
・Ansible管理サーバーから各サーバーへシェルスクリプトを配布し実行
・生成されたデータ（ホスト名＋YYYYMMDDHHMMSS.dataのようなファイル名）をAnsible管理サーバーへ転送し、回収する。
というような作業が生じた。
しかも
・ログの生成先は数GB単位で様々なデータが入っているため、今回のデータのみ取得したい
という条件付き。（今回のファイルも数百MBのサイズ）

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/519662/7cd6aa35-c9ff-d4ec-de81-40bb344ee3f1.png)



時間がない中でその場しのぎのPlaybookを作成したので、サンプルを記載します。

# 内容

今回、再現するために、[ホスト名＋日付＋時刻].logを作るshファイルと、
playbookを作成した。

スクリプトはtouchでファイルを作成するだけの簡単な内容。
``` testscript.sh
#!/bin/bash
HOSTNAME=`uname -n`
DATE=`date "+%Y%m%d%H%M%S"`
touch /var/log/scriptlog/${HOSTNAME}_${DATE}.log
exit 0
```

playbookの記述で躓く点として、
ファイル名に`date "+%Y%m%d%H%M%S"`が含まれていること。
シェルの実行時間によって、ファイル名が変わってしまうので、
名前を指定して回収することができない。
ディレクトリの内容丸ごと持ってくるのであれば、syncronizeモジュールや、with_fileglobを用いればいいのだが、今回は1サーバーにつきファイルを1つだけもってくる必要がある。
今回は直前に`ls -1t | head -1`を打ち、タイムスタンプ順にソートした1行目の標準出力を回収するよう作成した。


``` GetLog.yml
---
- hosts: TEST01
  gather_facts: false
  tasks:
    - name: Copy Script
      copy:
        src: "testscript.sh"
        dest: "/tmp/"
        owner: "root"
        group: "root"
        mode: "777"

    - name: Run script
      shell: "/tmp/testscript.sh"

    - name: Check Log
      shell: "ls -1t /var/log/scriptlog/ | head -1"
      register: result

    - name: Get Log
      fetch:
        src: "/var/log/scriptlog/{{ result.stdout }}"
        dest: "/var/log/GetData/"
        flat: yes

    - name: DEL script
      file:
        state: absent
        path: /tmp/testscript.sh
```



playbookの実行結果

``` result.log
[root@ans-mngr ansible]# ansible-playbook test/GetLog.yml -i inventory/hosts.ini -v
Using /etc/ansible/ansible.cfg as config file

PLAY [TEST01] *********************************************************************************************************

TASK [Copy Script] ****************************************************************************************************
changed: [192.168.11.71] => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": true, "checksum": "d4ce3905fc0629dbf55d7705af5328fc3c4d2458", "dest": "/tmp/testscript.sh", "gid": 0, "group": "root", "md5sum": "2220b3118a148207c10eb32a7c93c017", "mode": "0777", "owner": "root", "size": 117, "src": "/root/.ansible/tmp/ansible-tmp-1706150751.9667144-3376-110390147566219/source", "state": "file", "uid": 0}

TASK [Run script] *****************************************************************************************************
changed: [192.168.11.71] => {"changed": true, "cmd": "/tmp/testscript.sh", "delta": "0:00:00.016879", "end": "2024-01-24 21:45:54.988094", "msg": "", "rc": 0, "start": "2024-01-24 21:45:54.971215", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

TASK [Check Log] ******************************************************************************************************
changed: [192.168.11.71] => {"changed": true, "cmd": "ls -1t /var/log/scriptlog/ | head -1", "delta": "0:00:00.013493", "end": "2024-01-24 21:45:55.700531", "msg": "", "rc": 0, "start": "2024-01-24 21:45:55.687038", "stderr": "", "stderr_lines": [], "stdout": "alma84-01_20240124214554.log", "stdout_lines": ["alma84-01_20240124214554.log"]}

TASK [Get Log] ********************************************************************************************************
changed: [192.168.11.71] => {"changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/var/log/GetData/alma84-01_20240124214554.log", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "remote_md5sum": null}

TASK [DEL script] *****************************************************************************************************
changed: [192.168.11.71] => {"changed": true, "path": "/tmp/testscript.sh", "state": "absent"}

PLAY RECAP ************************************************************************************************************
192.168.11.71              : ok=5    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[root@ans-mngr ansible]#
```

実行先のサーバーのログ置き場
```
[root@alma84-01 ~]# ls -lt /var/log/scriptlog/
合計 0
-rw-r--r-- 1 root root 0  1月 24 21:45 alma84-01_20240124214554.log
-rw-r--r-- 1 root root 0  1月 24 21:27 alma84-01_20240124212711.log
-rw-r--r-- 1 root root 0  1月 24 21:25 alma84-01_20240124212538.log
-rw-r--r-- 1 root root 0  1月 24 21:24 alma84-01_20240124212434.log
-rw-r--r-- 1 root root 0  1月 24 21:23 alma84-01_20240124212313.log
-rw-r--r-- 1 root root 0  1月 24 21:20 alma84-01_20240124212046.log
-rw-r--r-- 1 root root 0  1月 24 21:20 alma84-01_20240124212035.log
-rw-r--r-- 1 root root 0  1月 24 21:19 alma84-01_20240124211945.log
```

ansible管理サーバーのログ収集先
```
[root@ans-mngr GetData]# ls -l /var/log/GetData/
合計 0
-rw-r--r-- 1 root root 0  1月 24 21:45 alma84-01_20240124214554.log
```

最新のファイルを無事持ってくることができました。


# 応用
`ls -1`の結果をループ処理させれば、うまいこと任意のログを持ってくること可能に見えたので、少し工夫してみる。

```GetLog_2.yml
---
- hosts: TEST01

  gather_facts: false

  tasks:
    - name: Copy Script
      copy:
        src: "testscript.sh"
        dest: "/tmp/"
        owner: "root"
        group: "root"
        mode: "777"

    - name: Run script
      shell: "/tmp/testscript.sh"

    - name: Check Log
      shell: "ls -1 /var/log/scriptlog/*2024*"
      register: result

    - name: Get Log
      fetch:
        src: "{{ item }}"
        dest: "/var/log/GetData/"
        flat: yes
      with_items: "{{ result.stdout_lines }}"

    - name: DEL script
      file:
        state: absent
        path: /tmp/testscript.sh

```

```result.log
[root@ans-mngr ansible]# ansible-playbook test/GetLog_2.yml -i inventory/hosts.ini -v
Using /etc/ansible/ansible.cfg as config file

PLAY [TEST01] *********************************************************************************************************

TASK [Copy Script] ****************************************************************************************************
ok: [192.168.11.71] => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": false, "checksum": "d4ce3905fc0629dbf55d7705af5328fc3c4d2458", "dest": "/tmp/testscript.sh", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "path": "/tmp/testscript.sh", "size": 117, "state": "file", "uid": 0}

TASK [Run script] *****************************************************************************************************
changed: [192.168.11.71] => {"changed": true, "cmd": "/tmp/testscript.sh", "delta": "0:00:00.017548", "end": "2024-01-24 22:18:23.121420", "msg": "", "rc": 0, "start": "2024-01-24 22:18:23.103872", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

TASK [Check Log] ******************************************************************************************************
changed: [192.168.11.71] => {"changed": true, "cmd": "ls -1 /var/log/scriptlog/*2024*", "delta": "0:00:00.011361", "end": "2024-01-24 22:18:23.836569", "msg": "", "rc": 0, "start": "2024-01-24 22:18:23.825208", "stderr": "", "stderr_lines": [], "stdout": "/var/log/scriptlog/alma84-01_20240124211945.log\n/var/log/scriptlog/alma84-01_20240124212035.log\n/var/log/scriptlog/alma84-01_20240124212046.log\n/var/log/scriptlog/alma84-01_20240124212313.log\n/var/log/scriptlog/alma84-01_20240124212434.log\n/var/log/scriptlog/alma84-01_20240124212538.log\n/var/log/scriptlog/alma84-01_20240124212711.log\n/var/log/scriptlog/alma84-01_20240124214554.log\n/var/log/scriptlog/alma84-01_20240124221336.log\n/var/log/scriptlog/alma84-01_20240124221522.log\n/var/log/scriptlog/alma84-01_20240124221823.log", "stdout_lines": ["/var/log/scriptlog/alma84-01_20240124211945.log", "/var/log/scriptlog/alma84-01_20240124212035.log", "/var/log/scriptlog/alma84-01_20240124212046.log", "/var/log/scriptlog/alma84-01_20240124212313.log", "/var/log/scriptlog/alma84-01_20240124212434.log", "/var/log/scriptlog/alma84-01_20240124212538.log", "/var/log/scriptlog/alma84-01_20240124212711.log", "/var/log/scriptlog/alma84-01_20240124214554.log", "/var/log/scriptlog/alma84-01_20240124221336.log", "/var/log/scriptlog/alma84-01_20240124221522.log", "/var/log/scriptlog/alma84-01_20240124221823.log"]}

TASK [Get Log] ********************************************************************************************************
changed: [192.168.11.71] => (item=/var/log/scriptlog/alma84-01_20240124211945.log) => {"ansible_loop_var": "item", "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/var/log/GetData/alma84-01_20240124211945.log", "item": "/var/log/scriptlog/alma84-01_20240124211945.log", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "remote_md5sum": null}
changed: [192.168.11.71] => (item=/var/log/scriptlog/alma84-01_20240124212035.log) => {"ansible_loop_var": "item", "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/var/log/GetData/alma84-01_20240124212035.log", "item": "/var/log/scriptlog/alma84-01_20240124212035.log", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "remote_md5sum": null}
changed: [192.168.11.71] => (item=/var/log/scriptlog/alma84-01_20240124212046.log) => {"ansible_loop_var": "item", "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/var/log/GetData/alma84-01_20240124212046.log", "item": "/var/log/scriptlog/alma84-01_20240124212046.log", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "remote_md5sum": null}
changed: [192.168.11.71] => (item=/var/log/scriptlog/alma84-01_20240124212313.log) => {"ansible_loop_var": "item", "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/var/log/GetData/alma84-01_20240124212313.log", "item": "/var/log/scriptlog/alma84-01_20240124212313.log", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "remote_md5sum": null}
changed: [192.168.11.71] => (item=/var/log/scriptlog/alma84-01_20240124212434.log) => {"ansible_loop_var": "item", "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/var/log/GetData/alma84-01_20240124212434.log", "item": "/var/log/scriptlog/alma84-01_20240124212434.log", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "remote_md5sum": null}
changed: [192.168.11.71] => (item=/var/log/scriptlog/alma84-01_20240124212538.log) => {"ansible_loop_var": "item", "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/var/log/GetData/alma84-01_20240124212538.log", "item": "/var/log/scriptlog/alma84-01_20240124212538.log", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "remote_md5sum": null}
changed: [192.168.11.71] => (item=/var/log/scriptlog/alma84-01_20240124212711.log) => {"ansible_loop_var": "item", "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/var/log/GetData/alma84-01_20240124212711.log", "item": "/var/log/scriptlog/alma84-01_20240124212711.log", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "remote_md5sum": null}
ok: [192.168.11.71] => (item=/var/log/scriptlog/alma84-01_20240124214554.log) => {"ansible_loop_var": "item", "changed": false, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/var/log/GetData/alma84-01_20240124214554.log", "file": "/var/log/scriptlog/alma84-01_20240124214554.log", "item": "/var/log/scriptlog/alma84-01_20240124214554.log", "md5sum": "d41d8cd98f00b204e9800998ecf8427e"}
changed: [192.168.11.71] => (item=/var/log/scriptlog/alma84-01_20240124221336.log) => {"ansible_loop_var": "item", "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/var/log/GetData/alma84-01_20240124221336.log", "item": "/var/log/scriptlog/alma84-01_20240124221336.log", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "remote_md5sum": null}
changed: [192.168.11.71] => (item=/var/log/scriptlog/alma84-01_20240124221522.log) => {"ansible_loop_var": "item", "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/var/log/GetData/alma84-01_20240124221522.log", "item": "/var/log/scriptlog/alma84-01_20240124221522.log", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "remote_md5sum": null}
changed: [192.168.11.71] => (item=/var/log/scriptlog/alma84-01_20240124221823.log) => {"ansible_loop_var": "item", "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/var/log/GetData/alma84-01_20240124221823.log", "item": "/var/log/scriptlog/alma84-01_20240124221823.log", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "remote_md5sum": null}

TASK [DEL script] *****************************************************************************************************
changed: [192.168.11.71] => {"changed": true, "path": "/tmp/testscript.sh", "state": "absent"}

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


ansible管理サーバーのログ回収先
```
[root@ans-mngr GetData]# ls -l /var/log/GetData/
合計 0
-rw-r--r-- 1 root root 0  1月 24 22:18 alma84-01_20240124211945.log
-rw-r--r-- 1 root root 0  1月 24 22:18 alma84-01_20240124212035.log
-rw-r--r-- 1 root root 0  1月 24 22:18 alma84-01_20240124212046.log
-rw-r--r-- 1 root root 0  1月 24 22:18 alma84-01_20240124212313.log
-rw-r--r-- 1 root root 0  1月 24 22:18 alma84-01_20240124212434.log
-rw-r--r-- 1 root root 0  1月 24 22:18 alma84-01_20240124212538.log
-rw-r--r-- 1 root root 0  1月 24 22:18 alma84-01_20240124212711.log
-rw-r--r-- 1 root root 0  1月 24 21:45 alma84-01_20240124214554.log
-rw-r--r-- 1 root root 0  1月 24 22:18 alma84-01_20240124221336.log
-rw-r--r-- 1 root root 0  1月 24 22:18 alma84-01_20240124221522.log
-rw-r--r-- 1 root root 0  1月 24 22:18 alma84-01_20240124221823.log
[root@ans-mngr GetData]#
```


ls の出力に若干の差異が生じるため、srcの指定に修正が必要だったが、
コマンドの標準出力をwith_itemsに指定してループ処理が可能なことがわかった。



# あとがき
最近、現場では複数台のサーバーに操作することに関して効率性を求めて、Ansible前提で検討・依頼されることが多くなってきています。
そのため、冪等性云々などの本来の特徴や用途を無視したことでも「Ansibleを使ってできるか？」と問われることが多く、工夫しなければいけないことも多くなっていくだろうなと身構えています。
以前の現場で別部隊がやっていたPXEブートが楽しそうだったので、試そうと思いながらもなかなか時間取れず…今後も頑張ります。