diff --git a/README.md b/README.md index 55b2760..731341c 100644 --- a/README.md +++ b/README.md @@ -241,14 +241,13 @@ ansible-playbook \ site.yaml ``` +Use `--forks` option to controll how many VMs should be managed in parallel. + Role variables -------------- ```yaml -# Whether to show logs or not -vmware_vm_provisioning_force_show_log: no - # List of VMs to provision (see README for details) vmware_vm_provisioning_vms: [] diff --git a/defaults/main.yaml b/defaults/main.yaml index a2212ce..b95bb93 100644 --- a/defaults/main.yaml +++ b/defaults/main.yaml @@ -1,8 +1,5 @@ --- -# Whether to show logs or not -vmware_vm_provisioning_force_show_log: no - # List of VMs to provision (see README for details) vmware_vm_provisioning_vms: [] diff --git a/tasks/main.yaml b/tasks/main.yaml index ef36ed6..d6b144a 100644 --- a/tasks/main.yaml +++ b/tasks/main.yaml @@ -1,8 +1,97 @@ --- -- name: Provision VMs - include_tasks: vm.yaml - with_items: "{{ vmware_vm_provisioning_vms | selectattr('name', 'equalto', inventory_hostname) | list }}" - loop_control: - loop_var: vm - no_log: "{{ not vmware_vm_provisioning_force_show_log }}" +- name: Set variable with VM details + set_fact: + vmware_vm_provisioning_vm: "{{ (vmware_vm_provisioning_vms | selectattr('name', 'equalto', inventory_hostname) | list)[0] }}" + when: > + vmware_vm_provisioning_vms | selectattr('name', 'equalto', inventory_hostname) | list | length > 0 + +- block: + - name: Power off VM + local_action: + module: vmware_guest + cluster: "{{ vmware_vm_provisioning_vm.cluster | default(vmware_vm_provisioning_cluster | default(omit)) }}" + datacenter: "{{ vmware_vm_provisioning_vm.datacenter | default(vmware_vm_provisioning_datacenter | default(omit)) }}" + esxi_hostname: "{{ vmware_vm_provisioning_vm.esxi_hostname | default(vmware_vm_provisioning_esxi_hostname | default(omit)) }}" + folder: "{{ vmware_vm_provisioning_vm.folder | default(vmware_vm_provisioning_folder | default(omit)) }}" + force: "{{ vmware_vm_provisioning_vm.force | default(vmware_vm_provisioning_force | default(omit)) }}" + hostname: "{{ vmware_vm_provisioning_vm.hostname | default(vmware_vm_provisioning_hostname | default(omit)) }}" + name: "{{ vmware_vm_provisioning_vm.name }}" + name_match: "{{ vmware_vm_provisioning_vm.name_match | default(vmware_vm_provisioning_name_match | default(omit)) }}" + password: "{{ vmware_vm_provisioning_vm.password | default(vmware_vm_provisioning_password | default(omit)) }}" + port: "{{ vmware_vm_provisioning_vm.port | default(vmware_vm_provisioning_port | default(omit)) }}" + state_change_timeout: "{{ vmware_vm_provisioning_vm.state_change_timeout | default(vmware_vm_provisioning_state_change_timeout | default(omit)) }}" + state: poweredoff + username: "{{ vmware_vm_provisioning_vm.username | default(vmware_vm_provisioning_username | default(omit)) }}" + validate_certs: "{{ vmware_vm_provisioning_vm.validate_certs | default(vmware_vm_provisioning_validate_certs | default(omit)) }}" + when: > + 'state' in vmware_vm_provisioning_vm and + vmware_vm_provisioning_vm.state in ['absent', 'rebuilt'] + failed_when: false + + - name: Remove VM + local_action: + module: vmware_guest + cluster: "{{ vmware_vm_provisioning_vm.cluster | default(vmware_vm_provisioning_cluster | default(omit)) }}" + datacenter: "{{ vmware_vm_provisioning_vm.datacenter | default(vmware_vm_provisioning_datacenter | default(omit)) }}" + esxi_hostname: "{{ vmware_vm_provisioning_vm.esxi_hostname | default(vmware_vm_provisioning_esxi_hostname | default(omit)) }}" + folder: "{{ vmware_vm_provisioning_vm.folder | default(vmware_vm_provisioning_folder | default(omit)) }}" + force: "{{ vmware_vm_provisioning_vm.force | default(vmware_vm_provisioning_force | default(omit)) }}" + hostname: "{{ vmware_vm_provisioning_vm.hostname | default(vmware_vm_provisioning_hostname | default(omit)) }}" + name: "{{ vmware_vm_provisioning_vm.name }}" + name_match: "{{ vmware_vm_provisioning_vm.name_match | default(vmware_vm_provisioning_name_match | default(omit)) }}" + password: "{{ vmware_vm_provisioning_vm.password | default(vmware_vm_provisioning_password | default(omit)) }}" + port: "{{ vmware_vm_provisioning_vm.port | default(vmware_vm_provisioning_port | default(omit)) }}" + state_change_timeout: "{{ vmware_vm_provisioning_vm.state_change_timeout | default(vmware_vm_provisioning_state_change_timeout | default(omit)) }}" + state: absent + username: "{{ vmware_vm_provisioning_vm.username | default(vmware_vm_provisioning_username | default(omit)) }}" + validate_certs: "{{ vmware_vm_provisioning_vm.validate_certs | default(vmware_vm_provisioning_validate_certs | default(omit)) }}" + when: > + 'state' in vmware_vm_provisioning_vm and + vmware_vm_provisioning_vm.state in ['absent', 'rebuilt'] + + - name: Provision VM + local_action: + module: vmware_guest + annotation: "{{ vmware_vm_provisioning_vm.annotation | default(vmware_vm_provisioning_annotation | default(omit)) }}" + cdrom: "{{ vmware_vm_provisioning_vm.cdrom | default(vmware_vm_provisioning_cdrom | default(omit)) }}" + cluster: "{{ vmware_vm_provisioning_vm.cluster | default(vmware_vm_provisioning_cluster | default(omit)) }}" + customization: "{{ + { + 'hostname': vmware_vm_provisioning_vm.name.split('.')[0], + 'domain': vmware_vm_provisioning_vm.name.split('.')[1:] | join('.') + } | combine( + vmware_vm_provisioning_vm.customization | default(vmware_vm_provisioning_customization | default({})) + ) }}" + customization_spec: "{{ vmware_vm_provisioning_vm.customization_spec | default(vmware_vm_provisioning_customization_spec | default(omit)) }}" + customvalues: "{{ vmware_vm_provisioning_vm.customvalues | default(vmware_vm_provisioning_customvalues | default(omit)) }}" + datacenter: "{{ vmware_vm_provisioning_vm.datacenter | default(vmware_vm_provisioning_datacenter | default(omit)) }}" + disk: "{{ vmware_vm_provisioning_vm.disks | default(vmware_vm_provisioning_disk | default(omit)) }}" + esxi_hostname: "{{ vmware_vm_provisioning_vm.esxi_hostname | default(vmware_vm_provisioning_esxi_hostname | default(omit)) }}" + folder: "{{ vmware_vm_provisioning_vm.folder | default(vmware_vm_provisioning_folder | default(omit)) }}" + force: "{{ vmware_vm_provisioning_vm.force | default(vmware_vm_provisioning_force | default(omit)) }}" + guest_id: "{{ vmware_vm_provisioning_vm.guest_id | default(vmware_vm_provisioning_guest_id | default(omit)) }}" + hardware: "{{ vmware_vm_provisioning_vm.hardware | default(vmware_vm_provisioning_hardware | default(omit)) }}" + hostname: "{{ vmware_vm_provisioning_vm.hostname | default(vmware_vm_provisioning_hostname | default(omit)) }}" + is_template: "{{ vmware_vm_provisioning_vm.is_template | default(vmware_vm_provisioning_is_template | default(omit)) }}" + linked_clone: "{{ vmware_vm_provisioning_vm.linked_clone | default(vmware_vm_provisioning_linked_clone | default(omit)) }}" + name: "{{ vmware_vm_provisioning_vm.name }}" + name_match: "{{ vmware_vm_provisioning_vm.name_match | default(vmware_vm_provisioning_name_match | default(omit)) }}" + networks: "{{ vmware_vm_provisioning_vm.networks | default(vmware_vm_provisioning_networks | default(omit)) }}" + password: "{{ vmware_vm_provisioning_vm.password | default(vmware_vm_provisioning_password | default(omit)) }}" + port: "{{ vmware_vm_provisioning_vm.port | default(vmware_vm_provisioning_port | default(omit)) }}" + resource_pool: "{{ vmware_vm_provisioning_vm.resource_pool | default(vmware_vm_provisioning_resource_pool | default(omit)) }}" + snapshot_src: "{{ vmware_vm_provisioning_vm.snapshot_src | default(vmware_vm_provisioning_snapshot_src | default(omit)) }}" + state: "{{ omit if 'state' in vmware_vm_provisioning_vm and vmware_vm_provisioning_vm.state == 'rebuilt' else vmware_vm_provisioning_vm.state | default(vmware_vm_provisioning_state | default(omit)) }}" + state_change_timeout: "{{ vmware_vm_provisioning_vm.state_change_timeout | default(vmware_vm_provisioning_state_change_timeout | default(omit)) }}" + template: "{{ vmware_vm_provisioning_vm.template | default(vmware_vm_provisioning_template | default(omit)) }}" + username: "{{ vmware_vm_provisioning_vm.username | default(vmware_vm_provisioning_username | default(omit)) }}" + uuid: "{{ vmware_vm_provisioning_vm.uuid | default(vmware_vm_provisioning_uuid | default(omit)) }}" + validate_certs: "{{ vmware_vm_provisioning_vm.validate_certs | default(vmware_vm_provisioning_validate_certs | default(omit)) }}" + vapp_properties: "{{ vmware_vm_provisioning_vm.vapp_properties | default(vmware_vm_provisioning_vapp_properties | default(omit)) }}" + wait_for_ip_address: "{{ vmware_vm_provisioning_vm.wait_for_ip_address | default(vmware_vm_provisioning_wait_for_ip_address | default(omit)) }}" + when: > + 'state' not in vmware_vm_provisioning_vm or + vmware_vm_provisioning_vm.state != 'absent' + when: > + vmware_vm_provisioning_vm is defined diff --git a/tasks/vm.yaml b/tasks/vm.yaml deleted file mode 100644 index e371471..0000000 --- a/tasks/vm.yaml +++ /dev/null @@ -1,88 +0,0 @@ ---- - -- name: Power off VM ({{ vm.name }}) - local_action: - module: vmware_guest - cluster: "{{ vm.cluster | default(vmware_vm_provisioning_cluster | default(omit)) }}" - datacenter: "{{ vm.datacenter | default(vmware_vm_provisioning_datacenter | default(omit)) }}" - esxi_hostname: "{{ vm.esxi_hostname | default(vmware_vm_provisioning_esxi_hostname | default(omit)) }}" - folder: "{{ vm.folder | default(vmware_vm_provisioning_folder | default(omit)) }}" - force: "{{ vm.force | default(vmware_vm_provisioning_force | default(omit)) }}" - hostname: "{{ vm.hostname | default(vmware_vm_provisioning_hostname | default(omit)) }}" - name: "{{ vm.name }}" - name_match: "{{ vm.name_match | default(vmware_vm_provisioning_name_match | default(omit)) }}" - password: "{{ vm.password | default(vmware_vm_provisioning_password | default(omit)) }}" - port: "{{ vm.port | default(vmware_vm_provisioning_port | default(omit)) }}" - state_change_timeout: "{{ vm.state_change_timeout | default(vmware_vm_provisioning_state_change_timeout | default(omit)) }}" - state: poweredoff - username: "{{ vm.username | default(vmware_vm_provisioning_username | default(omit)) }}" - validate_certs: "{{ vm.validate_certs | default(vmware_vm_provisioning_validate_certs | default(omit)) }}" - when: > - 'state' in vm and - vm.state in ['absent', 'rebuilt'] - failed_when: false - -- name: Remove VM ({{ vm.name }}) - local_action: - module: vmware_guest - cluster: "{{ vm.cluster | default(vmware_vm_provisioning_cluster | default(omit)) }}" - datacenter: "{{ vm.datacenter | default(vmware_vm_provisioning_datacenter | default(omit)) }}" - esxi_hostname: "{{ vm.esxi_hostname | default(vmware_vm_provisioning_esxi_hostname | default(omit)) }}" - folder: "{{ vm.folder | default(vmware_vm_provisioning_folder | default(omit)) }}" - force: "{{ vm.force | default(vmware_vm_provisioning_force | default(omit)) }}" - hostname: "{{ vm.hostname | default(vmware_vm_provisioning_hostname | default(omit)) }}" - name: "{{ vm.name }}" - name_match: "{{ vm.name_match | default(vmware_vm_provisioning_name_match | default(omit)) }}" - password: "{{ vm.password | default(vmware_vm_provisioning_password | default(omit)) }}" - port: "{{ vm.port | default(vmware_vm_provisioning_port | default(omit)) }}" - state_change_timeout: "{{ vm.state_change_timeout | default(vmware_vm_provisioning_state_change_timeout | default(omit)) }}" - state: absent - username: "{{ vm.username | default(vmware_vm_provisioning_username | default(omit)) }}" - validate_certs: "{{ vm.validate_certs | default(vmware_vm_provisioning_validate_certs | default(omit)) }}" - when: > - 'state' in vm and - vm.state in ['absent', 'rebuilt'] - -- name: Provision VM ({{ vm.name }}) - local_action: - module: vmware_guest - annotation: "{{ vm.annotation | default(vmware_vm_provisioning_annotation | default(omit)) }}" - cdrom: "{{ vm.cdrom | default(vmware_vm_provisioning_cdrom | default(omit)) }}" - cluster: "{{ vm.cluster | default(vmware_vm_provisioning_cluster | default(omit)) }}" - customization: "{{ - { - 'hostname': vm.name.split('.')[0], - 'domain': vm.name.split('.')[1:] | join('.') - } | combine( - vm.customization | default(vmware_vm_provisioning_customization | default({})) - ) }}" - customization_spec: "{{ vm.customization_spec | default(vmware_vm_provisioning_customization_spec | default(omit)) }}" - customvalues: "{{ vm.customvalues | default(vmware_vm_provisioning_customvalues | default(omit)) }}" - datacenter: "{{ vm.datacenter | default(vmware_vm_provisioning_datacenter | default(omit)) }}" - disk: "{{ vm.disks | default(vmware_vm_provisioning_disk | default(omit)) }}" - esxi_hostname: "{{ vm.esxi_hostname | default(vmware_vm_provisioning_esxi_hostname | default(omit)) }}" - folder: "{{ vm.folder | default(vmware_vm_provisioning_folder | default(omit)) }}" - force: "{{ vm.force | default(vmware_vm_provisioning_force | default(omit)) }}" - guest_id: "{{ vm.guest_id | default(vmware_vm_provisioning_guest_id | default(omit)) }}" - hardware: "{{ vm.hardware | default(vmware_vm_provisioning_hardware | default(omit)) }}" - hostname: "{{ vm.hostname | default(vmware_vm_provisioning_hostname | default(omit)) }}" - is_template: "{{ vm.is_template | default(vmware_vm_provisioning_is_template | default(omit)) }}" - linked_clone: "{{ vm.linked_clone | default(vmware_vm_provisioning_linked_clone | default(omit)) }}" - name: "{{ vm.name }}" - name_match: "{{ vm.name_match | default(vmware_vm_provisioning_name_match | default(omit)) }}" - networks: "{{ vm.networks | default(vmware_vm_provisioning_networks | default(omit)) }}" - password: "{{ vm.password | default(vmware_vm_provisioning_password | default(omit)) }}" - port: "{{ vm.port | default(vmware_vm_provisioning_port | default(omit)) }}" - resource_pool: "{{ vm.resource_pool | default(vmware_vm_provisioning_resource_pool | default(omit)) }}" - snapshot_src: "{{ vm.snapshot_src | default(vmware_vm_provisioning_snapshot_src | default(omit)) }}" - state: "{{ omit if 'state' in vm and vm.state == 'rebuilt' else vm.state | default(vmware_vm_provisioning_state | default(omit)) }}" - state_change_timeout: "{{ vm.state_change_timeout | default(vmware_vm_provisioning_state_change_timeout | default(omit)) }}" - template: "{{ vm.template | default(vmware_vm_provisioning_template | default(omit)) }}" - username: "{{ vm.username | default(vmware_vm_provisioning_username | default(omit)) }}" - uuid: "{{ vm.uuid | default(vmware_vm_provisioning_uuid | default(omit)) }}" - validate_certs: "{{ vm.validate_certs | default(vmware_vm_provisioning_validate_certs | default(omit)) }}" - vapp_properties: "{{ vm.vapp_properties | default(vmware_vm_provisioning_vapp_properties | default(omit)) }}" - wait_for_ip_address: "{{ vm.wait_for_ip_address | default(vmware_vm_provisioning_wait_for_ip_address | default(omit)) }}" - when: > - 'state' not in vm or - vm.state != 'absent'