Ansible : VMWare
Utilisation de la collection "community"
https://github.com/ansible-collections/community.vmware
La collection community pour VMWare contient un ensemble de rôles permettant diverses opérations. Celle-ci est la "vieille" collection; la collection https://github.com/ansible-collections/vmware.vmware_rest est disponible à partir de Vcenter 7.0 et permet de se passer d'un paquet de dépendances.
Install prérequis
<source> sudo python3 -m pip install --upgrade pip setuptools pip install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git python3 -m pip install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git </source>
Utilisation
Je part ici sur une utilisation basique avec un ESXi "StandAlone" (donc sans VCenter).
Récupération d'informations sur une vm
<source lang='yaml'> --- - hosts: all
become: True
collections: - community.vmware
tasks:
- name: Gather some information about a guest using MoID community.vmware.vmware_guest_info: hostname: "esx.ju.lab" username: "justine" password: "mdp" validate_certs: no datacenter: "ha-datacenter" #ha-datacenter est la valeur à utiliser pour un ESX standalone name: "lamp" schema: "vsphere" #Schéma de sortie des infos. Peut être à summary properties: ["config.hardware.memoryMB", "guest.disk", "overallStatus"] #Uniquement avec schema=vsphere delegate_to: localhost register: vm_moid_info
- name: Affiche debug: var: vm_moid_info
</source>
Donne quelque chose comme ça:
<source lang="json"> ok: [127.0.0.1] => {
"vm_moid_info": { "changed": false, "failed": false, "instance": { "config": { "hardware": { "memoryMB": 2048 } }, "guest": { "disk": [ { "_vimtype": "vim.vm.GuestInfo.DiskInfo", "capacity": 40130502656, "diskPath": "/", "filesystemType": null, "freeSpace": 34717876224, "mappings": [] }, { "_vimtype": "vim.vm.GuestInfo.DiskInfo", "capacity": 40130502656, "diskPath": "/var/www/clients/client6/web9/log", "filesystemType": null, "freeSpace": 34717876224, "mappings": [] }, { "_vimtype": "vim.vm.GuestInfo.DiskInfo", "capacity": 40130502656, "diskPath": "/var/www/clients/client7/web7/log", "filesystemType": null, "freeSpace": 34717876224, "mappings": [] }, { "_vimtype": "vim.vm.GuestInfo.DiskInfo", "capacity": 40130502656, "diskPath": "/var/www/clients/client7/web6/log", "filesystemType": null, "freeSpace": 34717876224, "mappings": [] }, { "_vimtype": "vim.vm.GuestInfo.DiskInfo", "capacity": 40130502656, "diskPath": "/var/www/clients/client6/web5/log", "filesystemType": null, "freeSpace": 34717876224, "mappings": [] }, { "_vimtype": "vim.vm.GuestInfo.DiskInfo", "capacity": 40130502656, "diskPath": "/var/www/clients/client5/web4/log", "filesystemType": null, "freeSpace": 34717876224, "mappings": [] }, { "_vimtype": "vim.vm.GuestInfo.DiskInfo", "capacity": 40130502656, "diskPath": "/var/www/clients/client4/web1/log", "filesystemType": null, "freeSpace": 34717876224, "mappings": [] }, { "_vimtype": "vim.vm.GuestInfo.DiskInfo", "capacity": 40130502656, "diskPath": "/var/www/clients/client3/web3/log", "filesystemType": null, "freeSpace": 34717876224, "mappings": [] }, { "_vimtype": "vim.vm.GuestInfo.DiskInfo", "capacity": 238787584, "diskPath": "/boot", "filesystemType": null, "freeSpace": 189932544, "mappings": [] } ] }, "overallStatus": "green" } }
} </source>
Snapshot
<syntaxhighlight lang='yaml'> --- - hosts: all
become: True
collections: - community.vmware
tasks:
- name: take a screenshot of the virtual machine console community.vmware.vmware_guest_snapshot: validate_certs: no hostname: "esx.ju.lab" username: "justine" password: "fougasse" datacenter: "ha-datacenter" name: "vmnaze" folder: "/ha-datacenter/vm/" snapshot_name: "Fait avec Ansible" description: "Fait avec Ansible :D" state: present quiesce: yes delegate_to: localhost register: take_screenshot
</syntaxhighlight>
Malheureusement:
<source lang="json"> PLAY [all] ***********************************************************************************************
TASK [Gathering Facts] *********************************************************************************** ok: [127.0.0.1]
TASK [take a screenshot of the virtual machine console] ************************************************** fatal: [127.0.0.1]: FAILED! => {"changed": false, "msg": "Failed to take snapshot due to VMware Licence restriction : Current license or ESXi version prohibits execution of the requested operation."}
PLAY RECAP *********************************************************************************************** 127.0.0.1 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0 </source>