While trying to clone VM via ansible using the vmware_guest module, new VM gets created with the old hostname. The Customize option is not taking effect. The error message that i see in the logs as well as within V-center is
Customization of the guest operating system 'freebsd64Guest' is not supported.
The operating system is FreeBSD OS. Please note, I have installed openvm tools in the source vm and only then i took a template. So, I have been trying to create the VM from the template that already has openvm tools.
---
- name: Create a VM from a template
vmware_guest:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: '{{ validate_certs }}'
name: '{{ vm_name }}'
state: '{{ state }}'
template: '{{ vm_template }}'
datacenter: '{{ cluster_name }}'
folder: '{{ folder_name }}'
cluster: '{{ cluster }}'
hardware:
memory_mb: '{{ ram }}'
num_cpus: '{{ cpu }}'
scsi: '{{ scsi }}'
customization:
hostname: '{{ vm_name }}'
networks:
- name: VM-NETWORK
ip: '{{ ip_address }}'
netmask: '{{ netmask }}'
gateway: '{{ gateway }}'
type: '{{ type }}'
wait_for_ip_address: yes
delegate_to: localhost
register: deploy
~
Looks like, it's a bug/feature that's not currently available in Vmware itself for FreeBSD. Please see the matrix available -
http://partnerweb.vmware.com/programs/guestOS/guest-os-customization-matrix.pdf
I have received this info, after raising bug in
https://github.com/ansible/ansible/issues/43189#issuecomment-407339134
Related
In my vsphere, I am having total 3 DCs with name ABC,PQR and XYZ. Under each DC I have created main folder (TEAM) & subfolder(FOLDERA and FOLDERB) with same name. Here is how the folder structure look like:
I am trying to set individual permission to the subfolders and main folder under each DC using https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_object_role_permission_module.html . I have used below code for this
- name: Assign access to parent VM folder
community.vmware.vmware_object_role_permission:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs : false
object_name: '{{ root_vm_folder }}'
state: present
recursive : false
role: NoAccess
principal: '{{ item.useraccount }}'
delegate_to: localhost
loop: '{{ my-permission }}'
- name: Assign view permission for VM sub folder
community.vmware.vmware_object_role_permission:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs : false
object_name: '{{ item.vmfolder }}'
state: present
recursive : false
role: '{{ item.permission }}'
principal: '{{ item.useraccount }}'
loop: '{{ my-permission}}'
delegate_to: localhost
Here are the variables:
root_vm_folder: 'TEAM'
vmfolder:
- 'FOLDERA'
- 'FOLDERB'
my-permission:
- useraccount: 'devops'
permission: 'ViewOnly'
vmfolder: 'FolderA'
- useraccount: 'developer'
permission: 'ViewOnly'
vmfolder: 'FolderB'
But when we run the playbook the permission is only getting effective to the DC:ABC as it is the first one. There is no option to specify the DataCentre name in the module.
As the Folder names are global variable, is there any way we can set the permission for the folders and sub-folders with same name under each DC in a vsphere.
This is the playbook that I have for VM deletion in vCenter.
My requirement here is to add a validation to check if the VM is in "poweredoff" state before proceeding for VM deletion, the task of VM deletion should trigger only if the "VM to be removed" is in "poweredoff" state.
If the "VM to be removed" is in "poweredon" state then it should display an message saying "VM is in powered on state."
Need help in adding validation to playbook, Thanks in advance.
---
# VM Automation Playbook
- name: Remove VM
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Remove VM
vmware_guest:
hostname: '{{ vcenter_hostname }}' #The hostname or IP address of the vSphere vCenter or ESXi server.
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: False
datacenter: '{{ datacenter_name }}' #Destination datacenter for the deploy operation.
name: '{{ vm_name }}' #Name of the VM to be created.
force: yes
state: absent #Specify the state the virtual machine should be in.
According the Ansible Collection documentation of Community.Vmware you may use the module vmware_vm_info to return basic info pertaining to a VMware machine guest
- name: Get virtual machine info
vmware_vm_info:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
folder: '{{ of_datacenter }}'
validate_certs: no
vm_type: vm
delegate_to: localhost
register: vm_info
- name: Show 'power_state' of {{ vm_name }}
debug:
msg: "{{ item.power_state}}"
with_items:
- "{{ vm_info.virtual_machines | json_query(query) }}"
vars:
query: "[?guest_name=='{{ vm_name }}']"
as the Return Values will have a power_state.
If the "VM to be removed" is in "poweredon" state then it should display an message saying "VM is in powered on state."
Based on that you could proceed further with the result in the module assert.
i´m new to ansible and try to automate server interface configuration.
Just something default like ip, gateway, dns and so on.
I´ve a task, called config_interfaces;
---
- name: nmcli add Ethernet
community.general.nmcli:
type: ethernet
conn_name: '{{ item.conn_name }}'
ip4: '{{ item.ip4 }}'
gw4: '{{ item.gw4 }}'
dns4: '{{ item.dns4 }}'
never_default4: '{{ item.default4 }}'
state: present
with_items:
- '{{ nmcli_ethernet }}'
register: add_interface_output
- name: restart interfaces
command: bash -c "nmcli con up {{ add_interface_output.conn_name }}"
when: add_interface_output.changed == true
with_items: "{{ add_interface_output.changed }}"
Then I have my group_vars file, which contains the nmcli_etherrnet variable;
---
nmcli_ethernet:
- conn_name: ens33
ifname: ens33
ip4: '{{ config_interfaces.ip_lan }}'
gw4: '{{ ip_lan_gw }}'
dns4: '{{ ip_lan_dns }}'
default4: no
- conn_name: ens37
ifname: ens37
ip4: '{{ config_interfaces.ip_trans }}'
default4: yes
- conn_name: eth1
ifname: eth1
ip4: '{{ config_interfaces.ip_exch1 }}'
default4: yes
- conn_name: eth2
ifname: eth2
ip4: '{{ config_interfaces.ip_exch2 }}'
default4: yes
ip_lan_gw: "192.168.226.2"
ip_lan_dns:
- 8.8.8.8
- 8.8.4.4
never_default4: yes
It´s always failing with
FAILED! => {"msg": "'nmcli_ethernet' is undefined"}
I´m searching for days now and don´t know where i missunderstand the with_items function. I bet this is just a completely small issue, but i dind´t get it.
Please help
I deploy some low supported OS like Debian 9, Debian 8, Rehhat 6,7, Centos 7.
The IP configuration is unsupported at boot time, so I add only the VLAN/virtual network interface, then I use vmware_vm_shell to configure the OS step by step.
What I'm looking for is a trick to wait for an event like /proc/net/dev exists on remote VM to continue other steps
What I tried so far :
- hosts: localhost
tasks:
- name: Create a virtual machine "{{ vm_name }}"
vmware_guest:
datacenter: '{{ datacenter }}'
hostname: '{{ vcenter }}'
username: "{{ login }}"
password: "{{ passwd }}"
folder: "{{ folder }}"
name: "{{ vm_name }}"
template: '{{ template }}'
cluster: "{{ cluster }}"
state: poweredon
disk:
- size_gb: "{{ disksizeGB }}"
datastore: '{{ datastore }}'
hardware:
memory_mb: '{{ ramsizeMB }}'
num_cpus: '{{ vcpu_num }}'
hotadd_cpu: True
hotremove_cpu: True
hotadd_memory: True
networks: '{{ vlans }}'
#wait_for_ip_address: yes # ERR there's ifaces, but not ip at this time
register: deploy
- name: Wait for server to start
local_action:
module: wait_for
timeout=15
when: deploy.changed
The last wait code block sucks (waiting N seconds) , I would like something smarter.
Any idea ?
If I don't wait, I sometimes get error : fatal: [localhost]: FAILED! => {"changed": false, "msg": "VMWareTools is not installed or is not running in the guest. VMware Tools are necessary to run this module."}
because the VM is not booted. The template have vmware-tools.
https://docs.ansible.com/ansible/2.6/modules/vmware_guest_module.html#vmware-guest-module
https://docs.ansible.com/ansible/latest/modules/vmware_vm_shell_module.html
Ok, found myself :)
- name: wait for server to boot
vmware_vm_shell:
datacenter: '{{ datacenter }}'
hostname: 'vcenter{{ vcenter }}'
username: "{{ login }}"
password: "{{ passwd }}"
validate_certs: False
folder: "{{ folder }}"
vm_id: "{{ vm_name }}"
cluster: "{{ cluster }}"
vm_password: '{{ passwd }}'
vm_username: root
vm_shell: '/bin/sleep'
vm_shell_args: 0
when: deploy.changed and 'debian' in distro
register: has_reboot
until: has_reboot.failed != 'true'
delay: 2
retries: 150
We are working on Ansible Environemt. We wanted to connect to a Newly Deployed VM using its UUUID.
How to Get the UUID of a VMware Virtual Machine using Ansible so that i can establish the connection.
Did you check this link: The UUID Location and Format
It can be accessed by standard SMBIOS scanning software — for example
SiSoftware Sandra or the IBM utility smbios2 [...]
You must use the vmware_guest_facts module first, and retrieve the UUID. However, there are two identified as uuid, so I listed them both. I am assuming that the uuid you want is the instance_uuid.
tasks:
- name: get list of facts
vmware_guest_facts:
hostname: '{{ vc_name }}'
username: '{{ vc_user }}'
password: '{{ vc_pwd }}'
datacenter: "{{ dc_name }}"
name: "{{ vm_name }}"
folder: "{{ dc_folder }}"
validate_certs: False
register: vm_facts
- set_fact:
vm_uuid: "{{ vm_facts.instance.instance_uuid }}"
- debug:
msg: "product uuid hw : {{ vm_facts.instance.hw_product_uuid }}\n instance: {{ vm_facts.instance.instance_uuid }}"
Now continue on in your script and use {{ vm_uuid }} where you need the uuid to the VM.
Ansible module vmware_guest_facts has been deprecated. This will not run in Ansible 2.9. You need to use the vmware_guest_info module instead.
- name: Getting VMWARE UUID
hosts: localhost
gather_facts: false
connection: local
tasks:
- name: Get Virtual Machine info
vmware_guest_info:
validate_certs: no
hostname: "{{ vcenter_hostname }}"
username: "{{ Password }}"
password: "{{ pass }}"
validate_certs: no
datacenter: "{{ datacenter_name }}"
name: "{{ VM_Name }}"
schema: "vsphere"
properties:
delegate_to: localhost
register: vminfo
- debug:
var: vminfo.instance.config.uuid
The above code assumes you know the datacenter the VM is sitting on. If unsure of such you can also run the following code:
- name: Get UUID from given VM Name
block:
- name: Get virtual machine info
vmware_vm_info:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
folder: "/datacenter/vm/folder"
delegate_to: localhost
register: vm_info
- debug:
msg: "{{ item.uuid }}"
with_items:
- "{{ vm_info.virtual_machines | json_query(query) }}"
vars:
query: "[?guest_name=='DC0_H0_VM0']"