#ansible #ansible-facts
Вопрос:
Окружающая среда:
ansible 2.10.7
config file = /app/ansible/ansible_linux_playbooks/ansible.cfg
configured module search path = ['/etc/ansible/library/modules']
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.6.8 (default, Aug 24 2020, 17:57:11) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
Поэтому у меня есть задача проверить, какие контроллеры и диск в настоящее время используются для виртуальной машины VMware с помощью модуля community.vmware.vmware_guest_disk_info.
Вывод этого модуля выглядит следующим образом:
TASK [vcsa_add_guest_disk : vm_guest_disk_info_gather] *******************************************************************************************************************************************************************
task path: /app/ansible/ansible_linux_playbooks/roles/vcsa_add_guest_disk/tasks/verify_guest_disk_info.yml:3
Monday 22 March 2021 09:31:15 -0400 (0:00:00.115) 0:00:03.699 **********
ok: [testserver06.tlb1.local.net] => {
"changed": false,
"guest_disk_info": {
"0": {
"backing_datastore": "lun001",
"backing_disk_mode": "persistent",
"backing_diskmode": "persistent",
"backing_eagerlyscrub": false,
"backing_filename": "[lun001] testserver06.tlb1.local.net/testserver06.tlb1.local.net-000002.vmdk",
"backing_thinprovisioned": true,
"backing_type": "FlatVer2",
"backing_uuid": "6000C29b-d712-xxxx-xxx-733be427fc4f",
"backing_writethrough": false,
"capacity_in_bytes": 21474836480,
"capacity_in_kb": 20971520,
"controller_bus_number": 0,
"controller_key": 1000,
"controller_type": "paravirtual",
"key": 2000,
"label": "Hard disk 1",
"summary": "20,971,520 KB",
"unit_number": 0
},
"1": {
"backing_datastore": "lun001",
"backing_disk_mode": "persistent",
"backing_diskmode": "persistent",
"backing_eagerlyscrub": false,
"backing_filename": "[lun001] testserver06.tlb1.local.net/testserver06.tlb1.local.net_2.vmdk",
"backing_thinprovisioned": true,
"backing_type": "FlatVer2",
"backing_uuid": "6000C290-d10b-xxx-xxxx-8d0d900e34ee",
"backing_writethrough": false,
"capacity_in_bytes": 12884901888,
"capacity_in_kb": 12582912,
"controller_bus_number": 2,
"controller_key": 1002,
"controller_type": "paravirtual",
"key": 2032,
"label": "Hard disk 2",
"summary": "12,582,912 KB",
"unit_number": 0
},
"2": {
"backing_datastore": "lun001",
"backing_disk_mode": "persistent",
"backing_diskmode": "persistent",
"backing_eagerlyscrub": false,
"backing_filename": "[lun001] testserver06.tlb1.local.net/testserver06.tlb1.local.net_1.vmdk",
"backing_thinprovisioned": true,
"backing_type": "FlatVer2",
"backing_uuid": "6000C298-dd8c-xxxx-xxxx-0e1af997af1e",
"backing_writethrough": false,
"capacity_in_bytes": 13958643712,
"capacity_in_kb": 13631488,
"controller_bus_number": 2,
"controller_key": 1002,
"controller_type": "paravirtual",
"key": 2033,
"label": "Hard disk 3",
"summary": "13,631,488 KB",
"unit_number": 1
}
}
}
Я достаю номера контроллеров и scsi-диск, назначенные каждой виртуальной машине через:
- name: debug_guest_disk_info
debug:
msg: "{{ reg_vm_guest_disk_info_gather | to_json | from_json | json_query(jmesquery) }}"
vars:
jmesquery: "guest_disk_info.*.{key: controller_bus_number, value: unit_number}"
register: results1
выход:
TASK [vcsa_add_guest_disk : debug_guest_disk_info] ***********************************************************************************************************************************************************************
task path: /app/ansible/ansible_linux_playbooks/roles/vcsa_add_guest_disk/tasks/verify_guest_disk_info.yml:14
Monday 22 March 2021 09:31:16 -0400 (0:00:00.874) 0:00:04.573 **********
ok: [testserver06.tlb1.local.net] => {}
MSG:
[{'key': 0, 'value': 0}, {'key': 2, 'value': 0}, {'key': 2, 'value': 1}]
I can determine the next/available controller numbers with this:
- name: debug_results
debug:
msg:
- "What is missing: {{ scsi_controller_max | difference(results1|to_json|from_json|json_query('msg[*].key') | map('int') | list ) }}"
vars:
- scsi_controller_max: "{{ range(4) | map('int') | list }}"
- scsi_drive_max: "{{ range(15) | map('int') | list }}"
OUTPUT:
TASK [vcsa_add_guest_disk : debug_results] *******************************************************************************************************************************************************************************
task path: /app/ansible/ansible_linux_playbooks/roles/vcsa_add_guest_disk/tasks/verify_guest_disk_info.yml:22
Monday 22 March 2021 09:31:16 -0400 (0:00:00.176) 0:00:04.749 **********
ok: [testserver06.tlb1.local.net] => {}
MSG:
['What is missing: [1, 3]']
So I know the next controller (if I need/want to add one), will be 1 or 3.
Question/problem:
I cannot figure out how to combine ‘results1’ or the ‘disk_info’ outputs to map what drives are missing (available unit_number) on each controller to use.
- Cannot use ‘count’ to determine as this only gives me how many drives
are there - Нельзя использовать «max», так как это не будет учитываться, если диски последовательно отсутствуют
Я не смог понять, как объединить «диктат списка» (я верю в то, что хочу, но даже не уверен в этом).
Я предполагаю, что данные будут выглядеть примерно так:
[{'key': 0, 'value': 0}, {'key': 2, 'value': [0,1]}]
Спасибо