現状把握やレポートを作成する時にvSphereのVMのvNICに設定されているポートグループのVLAN情報を取得したかったのですが
vmware_guest_info
モジュールを使ってもVMが属しているポートグループのキー(内部管理用)しか取得できず欲しい情報が取れません。そこで、いくつかモジュールを組み合わせてVMを元に設定されている分散ポートグループ、スイッチ名、VLAN情報を取得してみました。
環境
項目 | バージョン |
---|---|
ESXi | 7.0 |
vCenter | 7.0 |
Python | 3.6.8 |
Ansible Base | 2.10.1 |
VMware Collection | 2020/09/22時点での最新版 |
Playbook
作成したPlaybook
作成したPlaybookは以下のものです。
change me
部分は各自の環境に変更してください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
--- - name: "Example playbook" hosts: localhost gather_facts: false vars: vcenter_hostname: change me vcenter_username: administrator@vsphere.local vcenter_password: change me datacenter_name: change me vm_name: change me tasks: - name: "Gather facts VM information" vmware_guest_info: hostname: "{{ vcenter_hostname }}" username: "{{ vcenter_username }}" password: "{{ vcenter_password }}" validate_certs: false datacenter: "{{ datacenter_name }}" name: "{{ vm_name }}" schema: vsphere properties: - config.hardware.device register: vm_info_results - name: "Set vm_portgroup_key variable" set_fact: vm_portgroup_keys: >- {{ vm_portgroup_keys | default([]) + [item.backing.port.portgroupKey] }} loop: "{{ vm_info_results.instance.config.hardware.device }}" when: - item.backing.port.portgroupKey is defined - name: "Gather facts all dvs portgroups" vmware_dvs_portgroup_info: hostname: "{{ vcenter_hostname }}" username: "{{ vcenter_username }}" password: "{{ vcenter_password }}" validate_certs: false datacenter: "{{ datacenter_name }}" register: all_dvs_portgsroups_results - name: "Set dvs_portgroups variable" set_fact: dvs_portgroups: >- {{ dvs_portgroups | default([]) + item.value }} with_dict: "{{ all_dvs_portgsroups_results.dvs_portgroup_info }}" - name: "Set configured_vm_portgroups variable" set_fact: configured_vm_portgroups: >- {{ configured_vm_portgroups | default([]) + dvs_portgroups | selectattr('key', '==', item) }} loop: "{{ vm_portgroup_keys }}" - name: "Find all dvs portgroups" vmware_dvs_portgroup_find: hostname: "{{ vcenter_hostname }}" username: "{{ vcenter_username }}" password: "{{ vcenter_password }}" validate_certs: false register: find_all_dvs_portgroups_results - name: "Set data variable" set_fact: data: >- {{ data | default([]) + [{ 'dvswitch': item.dvswitch, 'portgroup_name': item.name, 'pvlan': item.pvlan, 'trunk': item.trunk, 'vlan_id': item.vlan_id }] }} loop: "{{ find_all_dvs_portgroups_results.dvs_portgroups }}" when: - configured_vm_portgroups | selectattr('portgroup_name', '==', item.name) - debug: msg: - "Configured VLAN information list of {{ vm_name }}" - "{{ data }}" |
処理の流れ
処理の内容としては
- VM情報を取得
- VMに紐付いている分散ポートグループのキーを取得
- 全分散ポートグループを取得
- VMで取得したキーとマッチする分散ポートグループキーを検索
- キーがマッチしたら情報を格納
- 表示
というものになってます。
実行
実行すると以下のような感じで情報が取得できます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
TASK [debug] **************************************************************************************************************************************************************** ok: [localhost] => { "msg": [ "Configured VLAN information list of test1", [ { "dvswitch": "DSwitch", "portgroup_name": "DPortGroup 1", "pvlan": false, "trunk": false, "vlan_id": "2" }, { "dvswitch": "DSwitch", "portgroup_name": "DPortGroup", "pvlan": false, "trunk": false, "vlan_id": "999" } ] ] } |
VMに設定されている分散ポートグループやVLAN情報が取得できました 🙂