New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vmware- Removing VNIC's from virtual machines #40226
Comments
@mohdumar321 Greetings! Thanks for taking the time to open this issue. In order for the community to handle your issue effectively, we need a bit more information. Here are the items we could not find in your description:
Please set the description of this issue with this template: |
@mohdumar321 Thanks for reporting this issue. Currently, there is no way to remove an existing NIC from an existing VM. Right now, Would like to raise a PR for a module which maintains existing NICs in an existing VMs ? Let me know if you need any help from my side to achieve this. needs_info |
@Akasurde: Thanks for the update. I am planning to write a custom module which can be used to remove the VNIC's from VMware. Any help on this task will be appreciated. |
https://github.com/mohdumar321/ansible_custom_modules/blob/master/vmware_vnic.py import re
import time
import pyVmomi
from pyVmomi import vim, vmodl
from pyVim.connect import SmartConnect, Disconnect
import atexit
import argparse
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_text, to_native
from ansible.module_utils.vmware import (find_obj, gather_vm_facts, get_all_objs,
compile_folder_path_for_object, serialize_spec,
vmware_argument_spec, set_vm_power_state, PyVmomi)
def get_obj(content, vimtype, name):
obj = None
container = content.viewManager.CreateContainerView(
content.rootFolder, vimtype, True)
for c in container.view:
if c.name == name:
obj = c
break
return obj
def remove_vnic(si, vm, nic_number):
nic_prefix_label = 'Network adapter '
nic_label = nic_prefix_label + str(nic_number)
virtual_nic_device = None
for dev in vm.config.hardware.device:
if isinstance(dev, vim.vm.device.VirtualEthernetCard) \
and dev.deviceInfo.label == nic_label:
virtual_nic_device = dev
if not virtual_nic_device:
raise RuntimeError('Virtual {} could not be found.'.format(nic_label))
virtual_nic_spec = vim.vm.device.VirtualDeviceSpec()
virtual_nic_spec.operation = \
vim.vm.device.VirtualDeviceSpec.Operation.remove
virtual_nic_spec.device = virtual_nic_device
spec = vim.vm.ConfigSpec()
spec.deviceChange = [virtual_nic_spec]
task = vm.ReconfigVM_Task(spec=spec)
tasks.wait_for_tasks(si, [task])
return True
def main():
argument_spec = vmware_argument_spec()
argument_spec.update(dict(hostname=dict(type='str', required=True),
username=dict(type='str', required=True),
password=dict(type='str', required=True),
port=dict(type='int', default='443'),
vm_name=dict(type='str', required=True),
uuid=dict(type='int', required=True),
nic_number=dict(type='int', required=True)))
module = AnsibleModule(argument_spec=argument_spec)
# connect this thing
serviceInstance = SmartConnect(
host=module.hostname,
user=module.username,
pwd=module.password,
port=module.port)
# disconnect this thing
atexit.register(Disconnect, serviceInstance)
vm = None
if module.uuid:
search_index = serviceInstance.content.searchIndex
vm = search_index.FindByUuid(None, module.uuid, True)
elif args.vm_name:
content = serviceInstance.RetrieveContent()
vm = get_obj(content, [vim.VirtualMachine], module.vm_name)
if vm:
remove_vnic(serviceInstance, vm, int(module.nic_number))
else:
print ("VM not found")
if __name__ == "__main__":
main() Error------ @Akasurde @dav1x @nerzhul @pdellaert @warthog9 : help me in resolving the issue |
I think you have error here - replace
with
Also, other occurrences of module params. I see there is no variable called Please open a PR, so that it will be easy to comment and direct development procedure. |
@Akasurde : Thanks for the help. I am new to this. Can you please guide me how to open PR? |
@mohdumar321 I understand. You can read about module development here and here. 'Connection timed out' is raised when ESXi or vCenter is not reachable. Try debugging by check connectivity to ESXi hostname or vCenter hostname. |
resolved_by_pr #52075 |
@mohdumar321 A new module to manage VM network is added in 2.9 |
COMPONENT NAME
vmware_guest
ISSUE TYPE
SUMMARY
Hello All,
I am writing playbooks for Vmware automation using ansible.
I am stuck in Removing VNIC's on VM's using ansible as I am unable to Identify any modules available for that.
Looking forward to solutions.
The text was updated successfully, but these errors were encountered: