네! Ansible에서 인벤토리 호스트를 그룹으로 나누어 실행하는 방법은 여러 가지가 있습니다.
6개씩 나눠서 실행하려면 batch 또는 **serial**을 사용하거나, **group_by**로 동적 그룹을 생성할 수 있습니다.
1. batch를 사용해 6개씩 실행 (Ansible 2.4+)
- name: Run tasks on hosts in batches of 6
hosts: all
serial: 6 # 한 번에 6개 호스트만 실행
tasks:
- name: Example task
debug:
msg: "Running on {{ inventory_hostname }}"
serial: 6 → 한 번에 6개 호스트만 실행
hosts: all → 전체 인벤토리 대상
2. group_by로 동적 그룹 생성 후 실행
- name: Create dynamic groups of 6 hosts
hosts: all
tasks:
- name: Group hosts into batches of 6
group_by:
key: "batch_{{ (play_hosts.index(inventory_hostname) // 6) + 1 }}"
- name: Run tasks on each batch
hosts: "batch_*" # batch_1, batch_2, ... 순서로 실행
tasks:
- name: Example task
debug:
msg: "Running on {{ inventory_hostname }}"
group_by → 호스트를 6개씩 그룹으로 나눔
hosts: "batch_*" → 생성된 그룹 순서대로 실행
3. add_host로 새 그룹 생성 후 실행
- name: Split hosts into groups of 6
hosts: localhost
tasks:
- name: Create host groups
add_host:
name: "{{ item }}"
groups: "batch_{{ (loop.index0 // 6) + 1 }}"
loop: "{{ groups['all'] }}"
when: item in groups['all']
- name: Run tasks on each batch
hosts: "batch_*" # batch_1, batch_2, ... 순서로 실행
tasks:
- name: Example task
debug:
msg: "Running on {{ inventory_hostname }}"
add_host → 호스트를 6개씩 그룹으로 추가
hosts: "batch_*" → 생성된 그룹 순서대로 실행
4. limit를 사용해 수동으로 그룹 지정
- name: Run tasks on first 6 hosts
hosts: all
limit: "host1,host2,host3,host4,host5,host6" # 수동으로 6개 지정
tasks:
- name: Example task
debug:
msg: "Running on {{ inventory_hostname }}"
limit → 특정 호스트만 실행 (수동 지정)
5. ansible-playbook 실행 시 --limit 옵션 사용
ansible-playbook playbook.yml --limit "host1,host2,host3,host4,host5,host6"
--limit → CLI에서 직접 호스트 지정
정리
| 방법 | 설명 | 장점 |
|---|
serial: 6 | 한 번에 6개 호스트만 실행 | 간단함 |
group_by | 호스트를 6개씩 그룹으로 나눔 | 동적 그룹 생성 |
add_host | 새 그룹을 생성해 실행 | 유연성 |
limit | 수동으로 호스트 지정 | 간단한 테스트용 |
추천 방법
- 가장 간단한 방법 →
serial: 6
- 동적 그룹이 필요할 때 →
group_by
- 수동으로 호스트 지정 →
limit
궁금한 점 있으면 언제든 질문 주세요! 😊