4
5

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 5 years have passed since last update.

ansible-playbook実行時に複数tagを指定する

Last updated at Posted at 2018-03-05

はじめに

自分用メモ。
久しぶりにansibleを使うとplaybook実行時に複数tagを指定する方法を忘れている。
デリミタがなんだったかとか、スペース入れてよかったかとか。

ついでに挙動をまとめておく。

環境

  • ansible 2.2.2.0
  • macOS Sierra 10.12.4

By ansible-playbook -h

 -t TAGS, --tags=TAGS  only run plays and tasks tagged with these values

Sample Playbook

---
- hosts: 127.0.0.1
  connection: local
  tasks:
    - name: tag01
      tags: tag01
      lineinfile:
        state=present
        create=yes
        dest=/tmp/tags-test.log
        line=tag01
    - name: tag02
      tags: tag02
      lineinfile:
        state=present
        create=yes
        dest=/tmp/tags-test.log
        line=tag02
    - name: tag03
      tags: tag03
      lineinfile:
        state=present
        create=yes
        dest=/tmp/tags-test.log
        line=tag03

Using --tags TAG_NAME1,TAG_NAME2...

いつも使っている書式。
デリミタはカンマで引数の間にスペースを入れてはいけない。

$ ansible-playbook tags-test.yml --tags tag01,tag02,tag03

PLAY [127.0.0.1] ***************************************************************

TASK [setup] *******************************************************************
ok: [127.0.0.1]

TASK [tag01] *******************************************************************
changed: [127.0.0.1]

TASK [tag02] *******************************************************************
changed: [127.0.0.1]

TASK [tag03] *******************************************************************
changed: [127.0.0.1]

PLAY RECAP *********************************************************************
127.0.0.1                  : ok=4    changed=3    unreachable=0    failed=0


$ cat /tmp/tags-test.log
tag01
tag02
tag03

スペースを入れると怒られます。

$ ansible-playbook tags-test.yml --tags tag01, tag02, tag03
ERROR! the playbook: tag02, could not be found

クォートすれば一応実行はできる。
が、最初の引数しか有効にならない。

$ ansible-playbook tags-test.yml --tags "tag01, tag02, tag03"

PLAY [127.0.0.1] ***************************************************************

TASK [setup] *******************************************************************
ok: [127.0.0.1]

TASK [tag01] *******************************************************************
changed: [127.0.0.1]

PLAY RECAP *********************************************************************
127.0.0.1                  : ok=2    changed=1    unreachable=0    failed=0

$ cat /tmp/tags-test.log
tag01

Using -t TAG_NAME1,TAG_NAME2...

ショートオプションは-t
(初めにロングオプションで覚えてしまったので使っていない)

$ ansible-playbook tags-test.yml -t tag01,tag02,tag03

PLAY [127.0.0.1] ***************************************************************

TASK [setup] *******************************************************************
ok: [127.0.0.1]

TASK [tag01] *******************************************************************
changed: [127.0.0.1]

TASK [tag02] *******************************************************************
changed: [127.0.0.1]

TASK [tag03] *******************************************************************
changed: [127.0.0.1]

PLAY RECAP *********************************************************************
127.0.0.1                  : ok=4    changed=3    unreachable=0    failed=0


$ cat /tmp/tags-test.log
tag01
tag02
tag03

オプションを複数指定してみる

誤った指定方法。
最後の一つだけが有効になる。

$ ansible-playbook tags-test.yml -t tag01 -t tag02 -t tag03

PLAY [127.0.0.1] ***************************************************************

TASK [setup] *******************************************************************
ok: [127.0.0.1]

TASK [tag03] *******************************************************************
changed: [127.0.0.1]

PLAY RECAP *********************************************************************
127.0.0.1                  : ok=2    changed=1    unreachable=0    failed=0


$ cat /tmp/tags-test.log
tag03

まとめ

  • デリミタはカンマ
  • 引数の間にスペースを入れてはいけない
  • オプションを複数指定しても最後のひとつしか有効にならない
4
5
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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?