Skip to content
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

Closed
mohdumar321 opened this issue May 16, 2018 · 17 comments
Closed

Vmware- Removing VNIC's from virtual machines #40226

mohdumar321 opened this issue May 16, 2018 · 17 comments
Labels
affects_2.6 This issue/PR affects Ansible v2.6 bug This issue/PR relates to a bug. cloud has_pr This issue has an associated PR. module This issue/PR relates to a module. needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. support:community This issue/PR relates to code supported by the Ansible community. vmware VMware community

Comments

@mohdumar321
Copy link

mohdumar321 commented May 16, 2018

COMPONENT NAME

vmware_guest

ISSUE TYPE
  • Bug Report
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.

@ansibot
Copy link
Contributor

ansibot commented May 16, 2018

@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:

  • issue type
  • ansible version
  • component name

Please set the description of this issue with this template:
https://raw.githubusercontent.com/ansible/ansible/devel/.github/ISSUE_TEMPLATE.md

click here for bot help

@ansibot ansibot added affects_2.6 This issue/PR affects Ansible v2.6 needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels May 16, 2018
@ansibot
Copy link
Contributor

ansibot commented May 18, 2018

@ansibot ansibot added bug This issue/PR relates to a bug. cloud module This issue/PR relates to a module. support:community This issue/PR relates to code supported by the Ansible community. vmware VMware community and removed support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels May 18, 2018
@Akasurde
Copy link
Member

Akasurde commented May 18, 2018

@mohdumar321 Thanks for reporting this issue. Currently, there is no way to remove an existing NIC from an existing VM.

Right now, vmware_guest is loaded with a lot of features, so we decided to segregate feature in different modules (See around 16:50:07 in IRC meeting log). We need community help in bridging this gap.

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 Akasurde removed the needs_triage Needs a first human triage before being processed. label May 18, 2018
@mohdumar321
Copy link
Author

@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.

@mohdumar321
Copy link
Author

mohdumar321 commented May 24, 2018

https://github.com/mohdumar321/ansible_custom_modules/blob/master/vmware_vnic.py
Module------------

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------

image

@Akasurde @dav1x @nerzhul @pdellaert @warthog9 : help me in resolving the issue

@Akasurde
Copy link
Member

@mohdumar321

I think you have error here - replace

host=module.hostname,

with

host=module.params['hostname'],

Also, other occurrences of module params. I see there is no variable called args which is used in if args.uuid

Please open a PR, so that it will be easy to comment and direct development procedure.

@mohdumar321
Copy link
Author

mohdumar321 commented May 24, 2018

@Akasurde : Thanks for the help.

I am new to this. Can you please guide me how to open PR?

@mohdumar321
Copy link
Author

mohdumar321 commented May 24, 2018

I have modified and after that, I got this error.

image

image

@Akasurde
Copy link
Member

@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.

@mohdumar321
Copy link
Author

@Akasurde

I have created a pull request. VMware Module to perform operations on VNIC #41726.

Created a module for performing operations on VNIC's and tested as well.

Review It.

@ansibot ansibot added support:core This issue/PR relates to code supported by the Ansible Engineering Team. and removed support:community This issue/PR relates to code supported by the Ansible community. labels Sep 16, 2018
@ansibot ansibot added support:community This issue/PR relates to code supported by the Ansible community. and removed support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Oct 2, 2018
@ansibot
Copy link
Contributor

ansibot commented Nov 25, 2018

cc @ckotte
click here for bot help

@ansibot
Copy link
Contributor

ansibot commented Feb 17, 2019

@Akasurde
Copy link
Member

resolved_by_pr #52075

@Akasurde Akasurde added the has_pr This issue has an associated PR. label Feb 17, 2019
@ansibot
Copy link
Contributor

ansibot commented Feb 25, 2019

@ansibot
Copy link
Contributor

ansibot commented May 8, 2019

cc @goneri
click here for bot help

@ansibot
Copy link
Contributor

ansibot commented Jun 1, 2019

@Akasurde
Copy link
Member

Akasurde commented Jul 8, 2019

@mohdumar321 A new module to manage VM network is added in 2.9 vmware_guest_network for managing the VM networking.
Thanks.

@Akasurde Akasurde closed this as completed Jul 8, 2019
@ansible ansible locked and limited conversation to collaborators Aug 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.6 This issue/PR affects Ansible v2.6 bug This issue/PR relates to a bug. cloud has_pr This issue has an associated PR. module This issue/PR relates to a module. needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. support:community This issue/PR relates to code supported by the Ansible community. vmware VMware community
Projects
None yet
Development

No branches or pull requests

3 participants