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 1 year has passed since last update.

Ansible Tips: shellモジュールの途中経過ログをansible-playbookのログと一緒にリアルタイムで表示する例

Posted at

https://qiita.com/hiroyuki_onodera/items/2fa11aeb3598284dbe54
の続き

あまり綺麗なやり方ではないが、一応できるということで、備忘として残す。

目標: ansible-playbookのログとshellモジュールのstdout, stderrを同じ画面に表示したい。

方法: tail -fをバックグラウンドで実行したターミナルでansible-playbookを実行

タスクの例

tasks
  - name: terraform apply --auto-approve
    shell:
      executable: /bin/bash
      cmd: |
        exec &> >(tee -a "/tmp/ansible_shell.log") 
        set -x
        cd terraform
        terraform apply --auto-approve

実行例 (対象がローカルノード)

  • tail -fをバックグラウンドで実行し、ログファイルを実行ターミナルにもリアルタイムに出力させておく
  • その後、ansible-playbookを行うと、ansible-playbookのログとshellモジュールのstdout, stderrが同じ実行ターミナルに表示される事になる
$ touch /tmp/ansible_shell.log

$ tail -f /tmp/ansible_shell.log &
[1] 87649

$ ansible-playbook -i localhost, -c local site.yml
...

TASK [terraform apply --auto-approve] ******************************************
+ cd terraform
+ terraform apply --auto-approve
Acquiring state lock. This may take a few moments...
ibm_pi_volume.ds_volume_share2: Refreshing state... [id=976c66a7-d403-4432-8ddb-f876cce6db0d/b7308887-0380-4f2d-a4e2-5ab8d6bada6a]
data.ibm_pi_network.power_networks_private: Reading...
...

Apply complete! Resources: 18 added, 0 changed, 0 destroyed.

changed: [localhost]

...

バックグラウンドtailを停止するには

% jobs
[1]  + running    tail -f /tmp/ansible_shell.log
$ kill %1
[1]  + terminated  tail -f /tmp/ansible_shell.log  
$       

まあ、普通は別ターミナルで見ると思いますが。。

リモートノードの場合

  • sshでtail -fを行なっておく
$ ssh remote_node "touch /tmp/ansible_shell.log; tail -f /tmp/ansible_shell.log" & 
[1] 89800
[1]  + suspended (tty input)  ssh remote_node "touch /tmp/ansible_shell.log; tail -f /tmp/ansible_shell.log"
$ ansible-playbook -i remote_node, site.yml 
...
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?