Contents
この投稿はAnsible 2 Advent Calendar 2019(通称裏アドベントカレンダー?)の10日めの記事です。
1週間全部俺の3回目です!
AnsibleをCLIで使っているとオペミス用でtagをコマンドのオプションのようにを実装したいなどの要件があったりなかったりするかもしれません(しかも必須とか)し、しないかもしれません。
ただ、実装したい場合はtagが多くなると何のタグがPlaybookで使えるか分からなくなるのでdebugモジュールを使ってUsageちっくなヘルプを表示してみます。
使用するrole
ここでは単純にdebugメッセージを表示するだけのroleを作ってみます。
ディレクトリ構成
exampleロールを作って使います。
1 2 3 4 5 6 7 8 |
roles/ └── example └── tasks ├── delete.yml ├── install.yml ├── main.yml └── update.yml |
main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
--- - name: include install.yml include_tasks: install.yml tags: - install - name: include update.yml include_tasks: update.yml tags: - update - name: include delete.yml include_tasks: delete.yml tags: - delete |
install
1 2 3 |
- debug: msg="install!!" tags: always |
update
1 2 3 |
- debug: msg="update!!" tags: always |
delete
1 2 3 |
- debug: msg="delete!!" tags: always |
ロール呼び出し元Playbook
ロールの呼び出し元PlaybookにdebugでUsageっぽいヘルプを書いてみます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
--- - name: example playbook hosts: localhost gather_facts: no tasks: - include_role: name: example tags: - never - example - debug: msg: - 'usage: ansible-playbook main.yml --tags [role name],[task]' - '' - 'tags:' - ' h, help show this help message' - ' example Role name to run' - ' install run install task for example role' - ' update run update task for example role' - ' delete run delete task for example role' when: - ansible_run_tags.0 == 'all' or ansible_run_tags.0 == 'h' or ansible_run_tags.0 == 'help' tags: always |
Playbookを実行
これを元に実行してみます。
何もタグを指定していない場合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# ansible-playbook main.yml [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [example playbook] ********************************************************************************************************************************************** TASK [debug] ********************************************************************************************************************************************************* ok: [localhost] => { "msg": [ "usage: ansible-playbook main.yml --tags example,[tags]", "", "tags:", " h, help show this help message", " example Role name to run", " install run install task for example role", " update run update task for example role", " delete run delete task for example role" ] } PLAY RECAP *********************************************************************************************************************************************************** localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
h,helpを指定した場合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# ansible-playbook main.yml --tags h [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [example playbook] ********************************************************************************************************************************************** TASK [debug] ********************************************************************************************************************************************************* ok: [localhost] => { "msg": [ "usage: ansible-playbook main.yml --tags example,[tags]", "", "tags:", " h, help show this help message", " example Role name to run", " install run install task for example role", " update run update task for example role", " delete run delete task for example role" ] } PLAY RECAP *********************************************************************************************************************************************************** localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# ansible-playbook main.yml --tags help [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [example playbook] ********************************************************************************************************************************************** TASK [debug] ********************************************************************************************************************************************************* ok: [localhost] => { "msg": [ "usage: ansible-playbook main.yml --tags example,[tags]", "", "tags:", " h, help show this help message", " example Role name to run", " install run install task for example role", " update run update task for example role", " delete run delete task for example role" ] } PLAY RECAP *********************************************************************************************************************************************************** localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
installを指定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# ansible-playbook main.yml --tags example,install [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [example playbook] ********************************************************************************************************************************************** TASK [include_role : example] **************************************************************************************************************************************** TASK [example : include install.yml] ********************************************************************************************************************************* included: /root/AdventCalendar/roles/example/tasks/install.yml for localhost TASK [example : debug] *********************************************************************************************************************************************** ok: [localhost] => { "msg": "install!!" } TASK [debug] ********************************************************************************************************************************************************* skipping: [localhost] PLAY RECAP *********************************************************************************************************************************************************** localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 |
updateを指定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# ansible-playbook main.yml --tags example,update [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [example playbook] ********************************************************************************************************************************************** TASK [include_role : example] **************************************************************************************************************************************** TASK [example : include update.yml] ********************************************************************************************************************************** included: /root/AdventCalendar/roles/example/tasks/update.yml for localhost TASK [example : debug] *********************************************************************************************************************************************** ok: [localhost] => { "msg": "update!!" } TASK [debug] ********************************************************************************************************************************************************* skipping: [localhost] PLAY RECAP *********************************************************************************************************************************************************** localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 |
deleteを指定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# ansible-playbook main.yml --tags example,delete [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [example playbook] ********************************************************************************************************************************************** TASK [include_role : example] **************************************************************************************************************************************** TASK [example : include delete.yml] ********************************************************************************************************************************** included: /root/AdventCalendar/roles/example/tasks/delete.yml for localhost TASK [example : debug] *********************************************************************************************************************************************** ok: [localhost] => { "msg": "delete!!" } TASK [debug] ********************************************************************************************************************************************************* skipping: [localhost] PLAY RECAP *********************************************************************************************************************************************************** localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 |
最後に
こんな感じでdebugモジュールを使えばヘルプとかも作れちゃいますね 🙂
と、言うことで小ネタでした!
3回目は以上です!次!