Lab 환경을 테스트하기 위해서 포트그룹을 일련번호로 여러개 만들어야할 일이 생겼습니다.
Lab-01-External : VLAN 1010
Lab-02-External : VLAN 1020
…
Lab-19-External : VLAN 1190
Lab-20-External : VLAN 1200
그래서 Ansible로 하면 어떻게할까하고 찾아봤습니다.
작업 내용은 간단하니, 3가지만 찾으면 됩니다.
- 포트그룹(portgroup) 관련 작업은 표준 스위치는
community.vmware.vmware_portgroup
, 분산 스위치는community.vmware.vmware_dvs_portgroup
모듈로 구분되어 있습니다. - 1씩 증가시키며 20까지 반복 : with_sequence: start=1 count=20
- 숫자를 00, 01, 10, 20과 같이 접두어 0을 사용해서 2자리로 표시 : with_sequence: start=1 count=20 format=%02d
이게 뭐라고 처음에 또 뻘건 오류 메시지가 줄줄줄…
Shell
x
failed: [localhost] (item=20) => {"ansible_loop_var": "item", "changed": false, "item": "20", "msg": "missing required arguments: port_binding, portgroup_name, switch_name"}
port_binding, portgroup_name, switch_name
3가지 파라미터가 필수 더라고요. port_binding
파라미터 지정 안한게 원인이었습니다.
포트그룹 만들고, VLAN만 설정하면 되어서 파라미터도 간단히 구성되었습니다.
YAML
name Create portgroup
vars_files
vars.yml
hosts localhost
becomefalse
gather_factsfalse
tasks
name Create vlan portgroup
community.vmware.vmware_dvs_portgroup
hostname'{{ vcenter_hostname }}'
username'{{ vcenter_username }}'
password'{{ vcenter_password }}'
switch_name"TOR-10G"
portgroup_name"Lab-{{ item }}-External"
vlan_id"1{{ item }}0"
port_binding ephemeral
state present
with_sequence"start=1 count=20 format=%02d"
delegate_to localhost
우히히~ 오류 없이 잘 됐습니다!
Shell
[ikhwan@mgmt-linux Lab-OpenHCI]$ ansible-playbook create-portgroup.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
[WARNING]: Collection community.vmware does not support Ansible version 2.14.14
PLAY [Create portgroup] **********************************************************************************************************
TASK [Create vlan portgroup] *****************************************************************************************************
changed: [localhost] => (item=01)
changed: [localhost] => (item=02)
changed: [localhost] => (item=03)
changed: [localhost] => (item=04)
changed: [localhost] => (item=05)
changed: [localhost] => (item=06)
changed: [localhost] => (item=07)
changed: [localhost] => (item=08)
changed: [localhost] => (item=09)
changed: [localhost] => (item=10)
changed: [localhost] => (item=11)
changed: [localhost] => (item=12)
changed: [localhost] => (item=13)
changed: [localhost] => (item=14)
changed: [localhost] => (item=15)
changed: [localhost] => (item=16)
changed: [localhost] => (item=17)
changed: [localhost] => (item=18)
changed: [localhost] => (item=19)
changed: [localhost] => (item=20)
PLAY RECAP ***********************************************************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[ikhwan@mgmt-linux Lab-OpenHCI]$
참고자료
- https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_dvs_portgroup_module.html
- https://docs.ansible.com/ansible/latest/collections/ansible/builtin/sequence_lookup.html
- https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_portgroup_module.html