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 Playbook で task の結果が changed の時だけ別の task を実行する

Last updated at Posted at 2021-09-17

以下の記事の通りでも実現できますが、世間的には notify handlers を使うのが一般的なようです

それだけなんですが、日本語の記事を見つけられなかったので書いておきます
たぶんどこかにあるんでしょうが、changed_when:とかshell: の話に埋もれて発見できず…

具体例

タイムゾーンを Asia/Tokyo に合わせ、タイムゾーンの変更があった時だけ crond を再起動する

- hosts: xxxx
  user: xxxx
  become: yes
  tasks:
    - timezone:
        name: Asia/Tokyo
      register: set_timezone
    - when: set_timezone.changed
      systemd:
        state: restarted
        name: cron

タイムゾーンが元から Asia/Tokyo の場合は cron の再起動はスキップされます
register で task の実行結果情報を受け取るところがミソです
register については他の方の記事をどーぞ

0
0
2

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?