目次
序章
大規模なプロジェクトでは、通常、Playbook には多くのタスクが含まれます。この Playbook を実行するたびに、すべてのタスクを再度実行します。実際、実際の使用プロセスでは、一部のタスクのみを実行したい場合があり、プレイブック全体を完全に実行したくない場合があります。今回はタグを使用する必要があります。(プレイブックのデバッグ中に非常に頻繁に使用されます)
タグを使用して、プレイブック内の特定のタスクに「タグ付け」できます。プレイブックを実行するときに、タグを選択することで、実行のみするタスクまたは実行しないタスクを指定できます。
タスクタグ
タスクにタグを付けます. この章にはあまり内容がありません. 例から始めましょう:
例 1: httpd をインストールし、3 つのタグを同時に定義します: install_httpd、conf_httpd、および start_httpd
- hosts: 192.168.194.129
gather_facts: no
tasks:
- name: install http
yum:
name: httpd
state: present
tags: install_httpd
- name: configuration httpd
template:
src: /root/ansible_test/ansible_2/httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
owner: apache
group: apache
backup: yes
notify:
- restart httpd
tags: config_httpd
- name: start httpd
service:
name: httpd
state: started
enabled: yes
tags: start_httpd
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
以下の結果:
[root@clinet ansible_2]# ansible-playbook yum_file/tag/tag_test.yml
PLAY [192.168.194.129] **********************************************************************************************************************************************************************************
TASK [install http] *************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host 192.168.194.129 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.194.129]
TASK [configuration httpd] ******************************************************************************************************************************************************************************
changed: [192.168.194.129]
TASK [start httpd] **************************************************************************************************************************************************************************************
changed: [192.168.194.129]
RUNNING HANDLER [restart httpd] *************************************************************************************************************************************************************************
changed: [192.168.194.129]
PLAY RECAP **********************************************************************************************************************************************************************************************
192.168.194.129 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@clinet ansible_2]#
[root@clinet ansible_2]#
1. 指定したタグのタスクを実行する
タグを実行します。
[root@clinet ansible_2]# ansible-playbook --tags="start_httpd" yum_file/tag/tag_test.yml
PLAY [192.168.194.129] **********************************************************************************************************************************************************************************
TASK [start httpd] **************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host 192.168.194.129 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.194.129]
PLAY RECAP **********************************************************************************************************************************************************************************************
192.168.194.129 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@clinet ansible_2]#
複数のタグを実行:
[root@clinet ansible_2]# ansible-playbook --tags="install_httpd,start_httpd" yum_file/tag/tag_test.yml
PLAY [192.168.194.129] **********************************************************************************************************************************************************************************
TASK [install http] *************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host 192.168.194.129 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.194.129]
TASK [start httpd] **************************************************************************************************************************************************************************************
ok: [192.168.194.129]
PLAY RECAP **********************************************************************************************************************************************************************************************
192.168.194.129 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@clinet ansible_2]#
2. 指定したタグのタスクを除外する
指定したタグを持つタスクを除外します。つまり、指定したタグ以外のタスクは実行されず、他のすべてのタスクが実行されます。
[root@clinet ansible_2]# ansible-playbook --skip-tags="install_httpd" yum_file/tag/tag_test.yml
PLAY [192.168.194.129] **********************************************************************************************************************************************************************************
TASK [configuration httpd] ******************************************************************************************************************************************************************************
[WARNING]: Platform linux on host 192.168.194.129 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.194.129]
TASK [start httpd] **************************************************************************************************************************************************************************************
ok: [192.168.194.129]
PLAY RECAP **********************************************************************************************************************************************************************************************
192.168.194.129 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@clinet ansible_2]#
3. Playbook 内のすべてのタグを表示する
‐‐list‐tags パラメータを使用して、指定されたプレイブック内のすべてのタグを一覧表示します
[root@clinet ansible_2]# ansible-playbook --list-tags yum_file/tag/tag_test.yml
playbook: yum_file/tag/tag_test.yml
play #1 (192.168.194.129): 192.168.194.129 TAGS: []
TASK TAGS: [config_httpd, install_httpd, start_httpd]
[root@clinet ansible_2]#
[root@clinet ansible_2]#
4.タグ付け方法
4.1 1 つのタスクと 1 つのタグ
タスク:
- 名前: http
yum のインストール:
名前: httpd
状態: 現在の
タグ: install_httpd
4.2 タスクごとに複数のタグ
方法 1:
タスク:
- 名前: http
yum のインストール:
名前: httpd
状態: 現在の
タグ:- install_httpd1
- install_httpd2
方法 2:
タグ: ['install_httpd1', 'install_httpd2']
方法 3:タグ: install_httpd1,install_httpd2
4.3 プレイブックにタグを付ける
プレイに一連のタグが指定されている場合、プレイの下にあるすべてのタスクはタグを自動的に継承し、各タスクは独自のタグをカスタマイズすることもできます。
‐ name: configure webservers
hosts: all
remote_user: ansible
tags:
‐ httpd
tasks:
...
Ansible 組み込みタグ
1.いつも
タスクが always タグでマークされると、それが Playbook の完全な実行であろうと、指定されたタグの実行であろうと、指定したタグに関係なく、タスクは常に実行されます。「--skip-tags=always」オプションを明示的に指定しない限り、タスクは実行されません。
例 1: always タグを構成し、playbook を実行し、always タグ以外の実行を指定し、always タグが実行されるかどうかを確認する
- hosts: 192.168.194.129
gather_facts: no
tasks:
- name: install http
yum:
name: httpd
state: present
tags: always
- name: configuration httpd
template:
src: /root/ansible_test/ansible_2/httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
owner: apache
group: apache
backup: yes
notify:
- restart httpd
tags: config_httpd
- name: start httpd
service:
name: httpd
state: started
enabled: yes
tags: start_httpd
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
例 2: always タグの always 実行をキャンセルするには --skip-tags=always を渡す必要があります
2.決して
このタグは、always の正反対であり、"--tags=never" オプションが明示的に指定されない限り実行されません。
3.タグ付け
# すべてのタグ付けされたタスクが実行されます。
yum_file/tag/tag_test.yml でタグ付けされた ansible‐playbook‐‐タグ
# タグ付けされたタスクはすべて実行されず、always タグは実行されません
ansible-playbook --skip-tags でタグ付けされた yum_file/tag/tag_test.yml
4.タグなし
# タグの付いていないすべてのタスクが実行され、always タグが付いているタスクも実行されます
ansibl‐プレイブック‐‐タグタグなし yum_file/tag/tag_test.yml
# タグ付けされていないタスクはすべて実行されません
ansibl-playbook --skip-tags タグなし yum_file/tag/tag_test.yml
5.すべて
すべてのタスクが実行されることを示します。デフォルトでは、ラベルが指定されていない場合、ラベルが使用されます
要約する
タスクのラベリング機能は、現在 Playbook のデバッグで最も多く使用されています. Playbook の実行で特定のタスクに問題が発生した場合、その後の変更テストのためにタスクにラベルを付けます. これも効果的に重複を回避します.プレイブックを最初から実行する時間。他の使い方はありますか、ガイドへようこそ!