LoginSignup
13
15

More than 5 years have passed since last update.

Ansible v2.0 からの "block" ディレクティブを使ってみる

Last updated at Posted at 2015-08-31

Ansible v2.0 (記事掲載時点ではalpha)から block ディレクティブが追加されている。

要はある条件下で、その block 内の task が実行されるということ。上記ページの例ではansible_distribution == 'CentOS' の時に3つのタスクが root で実行されることになるようだ。

when には 変数のブールも使えるので、例えば


vars:
  flag: true

tasks:
  - block:
    - yum: name={{ item }} state=installed
      with_items:
      - httpd
      - memcached

    - template: src=templates/src.j2 dest=/etc/foo.conf

    - service: name=bar state=started enabled=True
    when: flag

flag が true の時はこの block 内のタスクは実行されて false の時(またはwhen: not flagとするか)はスキップされる。

いろいろなシーンで使えそう。

13
15
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
13
15