説明
- hosts:
- ・ play を実行する対象ホストを絞り込む
- ・対象ホストは inventory ファイルに定義されていること
- gather_facts:
- ・実行時に facts 変数を収集するかどうかを指定する
- become: / become_user:
- ・ play 全体またはモジュールを root または特定のユーザーで実行する
構文
- hosts: <host-pattern>
- gather_facts: [yes | no]
- become: yes
- become_user: <useraccount>
host-pattern
| host-pattern | 対象ホスト |
|---|---|
| all | inventory ファイル内に記述したすべての対象ホスト |
| グループ名 | inventory ファイル内の指定したグループに含まれるすべての対象ホスト |
| ホスト名 | inventory ファイル内のホスト名で指定された対象ホスト |
gather_facts のパラメータ
| option | 説明 |
|---|---|
| yes no |
playbook の実行時に facts 変数を収集するかどうかを指定する |
例
■ inventory ファイル : hosts.ymlcentos:
hosts:
node-c0610:
node-c0706:
others:
hosts:
node-d0908:
■ hosts : all の場合
---
- hosts: all
tasks:
- name: 対象ホスト名の表示
debug:
msg: "{{ inventory_hostname }}"
実行結果 → すべてのホストで実行された
[ansibleman@ansiblesv ansible]$ ansible-playbook -i hosts.yml site.yml
PLAY [all] ****************************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [node-d0908]
ok: [node-c0706]
ok: [node-c0610]
TASK [対象ホスト名の表示] **********************************************************************************
ok: [node-c0610] => {
"msg": "node-c0610"
}
ok: [node-c0706] => {
"msg": "node-c0706"
}
ok: [node-d0908] => {
"msg": "node-d0908"
}
PLAY RECAP ****************************************************************************************
node-c0610 : ok=2 changed=0 unreachable=0 failed=0
node-c0706 : ok=2 changed=0 unreachable=0 failed=0
node-d0908 : ok=2 changed=0 unreachable=0 failed=0
[ansibleman@ansiblesv ansible]$
■ hosts : グループ名 の場合
---
- hosts: centos
tasks:
- name: 対象ホスト名の表示
debug:
msg: "{{ inventory_hostname }}"
実行結果 → cenos グループに含まれるホストだけが実行された
[ansibleman@ansiblesv ansible]$ ansible-playbook -i hosts.yml site.yml
PLAY [all] ****************************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [node-c0706]
ok: [node-c0610]
TASK [対象ホスト名の表示] **********************************************************************************
ok: [node-c0610] => {
"msg": "node-c0610"
}
ok: [node-c0706] => {
"msg": "node-c0706"
}
PLAY RECAP ****************************************************************************************
node-c0610 : ok=2 changed=0 unreachable=0 failed=0
node-c0706 : ok=2 changed=0 unreachable=0 failed=0
[ansibleman@ansiblesv ansible]$
■ hosts : ホスト名 の場合
---
- hosts: node-c0706
tasks:
- name: 対象ホスト名の表示
debug:
msg: "{{ inventory_hostname }}"
実行結果 → 指定したホストだけが実行された
[ansibleman@ansiblesv ansible]$ ansible-playbook -i hosts.yml site.yml
PLAY [node-c0706] *********************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [node-c0706]
TASK [対象ホスト名の表示] **********************************************************************************
ok: [node-c0706] => {
"msg": "node-c0706"
}
PLAY RECAP ****************************************************************************************
node-c0706 : ok=2 changed=0 unreachable=0 failed=0
[ansibleman@ansiblesv ansible]$
■ “gather_facts: no”の場合
---
- hosts: all
gather_facts: no
tasks:
- name: 対象ホスト名の表示
debug:
msg: "{{ inventory_hostname }}"
実行結果 → facts 変数を収集する “TASK [Gathering Facts]” が表示されていない
[ansibleman@ansiblesv ansible]$ ansible-playbook -i hosts.yml site.yml
PLAY [all] *****************************************************************************************
TASK [対象ホスト名の表示] ***********************************************************************************
ok: [node-c0610] => {
"msg": "node-c0610"
}
ok: [node-c0706] => {
"msg": "node-c0706"
}
ok: [node-d0908] => {
"msg": "node-d0908"
}
PLAY RECAP *****************************************************************************************
node-c0610 : ok=1 changed=0 unreachable=0 failed=0
node-c0706 : ok=1 changed=0 unreachable=0 failed=0
node-d0908 : ok=1 changed=0 unreachable=0 failed=0
[ansibleman@ansiblesv ansible]$
変更履歴
2019/03/10 “gather_facts” を追記した2019/04/30 become: / become_user: を追記した