備忘
目的
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.