Contents
Ansibleでタグを指定しない場合は全部のタスクが実行されてしまいますが、条件によってはタグを必須にしたい時(例えば、オペミスを発生させないなど)があるかと思います。
そこで、ここではタグを必須にするようなPlaybook例を書いてみようと思います。
Playbook
1 2 3 4 5 6 7 8 9 10 11 |
--- - name: tags test hosts: localhost gather_facts: no tasks: - debug: msg="No tags specified, please target tags specify." failed_when: ansible_run_tags.0 == 'all' - debug: msg="Hello, World!!" tags: hello |
タグは ansible_run_tags変数
にリスト型で格納されます。
何も指定しない場合は all
が入るので一番最初のタスクに failed_when
を使って失敗条件を指定します。
実行例
タグを指定せずに実行します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$ ansible-playbook example.yml [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [tags test] ********************************************************************************************************************************************* TASK [debug] ******************************************************************************************************************************************************* fatal: [localhost]: FAILED! => { "msg": "No tags specified, please target tags specify." } PLAY RECAP ********************************************************************************************************************************************************* localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0 |
タグを指定していないのでPlaybookは失敗します。
次にタグを指定してみます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$ ansible-playbook example.yml --tags hello [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [tags test] ********************************************************************************************************************************************* TASK [debug] ******************************************************************************************************************************************************* ok: [localhost] => { "msg": "Hello, World!!" } PLAY RECAP ********************************************************************************************************************************************************* localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Playbookは失敗せずにタスクが実行されました。
次に存在しないタグを指定してみます。
1 2 3 4 5 6 7 8 9 10 |
$ ansible-playbook example.yml --tags hoge [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [tags test] ********************************************************************************************************************************************* PLAY RECAP ********************************************************************************************************************************************************* |
指定したタグが設定されているタスクがないので、何も実行されずに処理が終了しました。
こんな感じにすれば、タグ指定忘れなどのオペミスを防げそうですね 🙂