0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ansible Tips: shellモジュールのデバックモード実行ログを対象ノードのファイルにもリアルタイムで書き出す

Posted at

備忘

目的

Ansible shellモジュール利用の際、どこを実行しているかなどをリアルタイムで確認したい場合、ファイルに実行ログを書き出しておくことで対応できる可能性がある。
Ansibleのログとログファイルの両方に書き出したい場合の例を示す。

参考

tasks例

  - shell: |-
      set -x
      exec > >(tee -a "/tmp/ansible_shell.log") 2>&1
      echo "hello world."
    args:
      executable: /bin/bash  # AIX環境などではbashを導入の上で明示的にbash指定が必要
    register: r
  - debug:
      var: r.stdout_lines

exec > >(tee -a "/tmp/ansible_shell.log") 2>&1

exec &> >(tee -a "/tmp/ansible_shell.log")
ともまとめられる。

Ansibleログ出力

TASK [shell] *******************************************************************************************************************
changed: [localhost]

TASK [debug] *******************************************************************************************************************
ok: [localhost] => {
    "r.stdout_lines": [
        "+ echo 'hello world.'",
        "hello world."
    ]
}

ファイル出力

ターゲットマシン上で

$ cat /tmp/ansible_shell.log
+ echo 'hello world.'
hello world.

リアルタイムログ確認例

ターゲットマシン上で

$ touch /tmp/ansible_shell.log
$ tail -0 -f /tmp/ansible_shell.log
+ echo 'hello world.'
hello world.
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?