Ansible Documentation
Blocksblock
block を使うと複数のタスクをまとめてグループとして定義できます。各タスクに共通する when などのディレクティブはこのグループに設定できます。設定したディレクティブは block に含まれる各タスクに継承されます。CentOS で動作しているサーバーに nginx をインストールする play です。
---
- hosts: all
tasks:
- name: epel-release と nginx のインストール
yum:
name: "{{ item }}"
state: latest
loop:
- epel-release
- nginx
when: ansible_facts['distribution'] == 'CentOS'
become: yes
- name: conf ファイルの編集
lineinfile:
path: /etc/nginx/nginx.conf
regexp: '^ server_name _;'
line: ' server_name node-c0706;'
when: ansible_facts['distribution'] == 'CentOS'
become: yes
- name: nginx サービスの起動
systemd:
state: started
name: nginx.service
enabled: yes
when: ansible_facts['distribution'] == 'CentOS'
become: yes
- name: firewall に http サービスを許可
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
when: ansible_facts['distribution'] == 'CentOS'
become: yes
block を使って書き直した play です。
---
- hosts: all
tasks:
- name: CentOS サーバーに ngix をインストール
block:
- name: epel-release と nginx のインストール
yum:
name: "{{ item }}"
state: latest
loop:
- epel-release
- nginx
- name: conf ファイルの編集
lineinfile:
path: /etc/nginx/nginx.conf
regexp: '^ server_name _;'
line: ' server_name node-c0706;'
- name: nginx サービスの起動
systemd:
state: started
name: nginx.service
enabled: yes
- name: firewall に http サービスを許可
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
when: ansible_facts['distribution'] == 'CentOS'
become: yes
block に指定された when と become の 2 つのディレクティブが blcok 内の 4 つのタスクそれぞれに追加された後に各タスクが実行されます。それにより実行時の play は block のあり / なしに関わらず同じものになります。結果、どちらも同じ実行結果になります。
ansibleman@ubuntu-pc:~/ansible/block$ ansible-playbook -i hosts.yml site.yml
PLAY [all] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [node-c0706]
ok: [node-u1804]
TASK [epel-release と nginx のインストール] ********************************************************************************
skipping: [node-u1804] => (item=epel-release)
skipping: [node-u1804] => (item=nginx)
changed: [node-c0706] => (item=epel-release)
changed: [node-c0706] => (item=nginx)
TASK [conf ファイルの編集] ************************************************************************************************
skipping: [node-u1804]
changed: [node-c0706]
TASK [nginx サービスの起動] ***********************************************************************************************
skipping: [node-u1804]
changed: [node-c0706]
TASK [firewall に http サービスを許可] *************************************************************************************
skipping: [node-u1804]
changed: [node-c0706]
PLAY RECAP *********************************************************************************************************
node-c0706 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node-u1804 : ok=1 changed=0 unreachable=0 failed=0 skipped=4 rescued=0 ignored=0
ansibleman@ubuntu-pc:~/ansible/block$
block と loop
block に loop ディレクティブの指定はできません。Ansible Documantation に with the exception of loops と明記されています。次の play は block に loop ディレクティブを指定した例です。---
- hosts: all
gather_facts: no
tasks:
- name: block に loop を指定した例
block:
- debug:
var: item
loop:
- 1
- 2
- 3
実行結果です。block に loop ディレクティブの指定はできないため 'loop' is not a valid attribute for a Block とエラーメッセージが表示されています。
ansibleman@ubuntu-pc:~/ansible/block$ ansible-playbook -i hosts.yml site.yml
ERROR! 'loop' is not a valid attribute for a Block
The error appears to be in '/home/ansibleman/ansible/block/site.yml': line 6, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- name: block に loop を指定した例
^ here
ansibleman@ubuntu-pc:~/ansible/block$
block のようにタスクのグループ(固まり)に loop ディレクティブを適用する場合は include_tasks に loop を指定します。