Skip to content

Commit

Permalink
Add esxi_hostname as argument in vmware_vswitch (#30498)
Browse files Browse the repository at this point in the history
Fix adds esxi_hostname as Ansible module argument for user
to define ESXi hostname to deploy VMware vSwitch.

Fixes: #24647

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
  • Loading branch information
Akasurde committed Dec 1, 2017
1 parent f5a2e26 commit e4254da
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
56 changes: 39 additions & 17 deletions lib/ansible/modules/cloud/vmware/vmware_vswitch.py
Expand Up @@ -52,6 +52,11 @@
- Add or remove the switch.
default: present
choices: [ absent, present ]
esxi_hostname:
description:
- Manage the vSwitch using this ESXi host system
version_added: "2.5"
aliases: [ 'host' ]
extends_documentation_fragment:
- vmware.documentation
'''
Expand Down Expand Up @@ -88,16 +93,27 @@
- vmnic2
mtu: 9000
delegate_to: localhost
- name: Add a VMware vSwitch to a specific host system
vmware_vswitch:
hostname: 192.168.10.1
username: esxi_username
password: esxi_password
esxi_hostname: DC0_H0
switch_name: vswitch_001
nic_name: vmnic0
mtu: 9000
delegate_to: localhost
'''


from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six import iteritems
from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec, get_all_objs
try:
from pyVmomi import vim, vmodl
HAS_PYVMOMI = True
except ImportError:
HAS_PYVMOMI = False

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.vmware import vmware_argument_spec, get_all_objs, connect_to_api
pass


def find_vswitch_by_name(host, vswitch_name):
Expand All @@ -107,19 +123,17 @@ def find_vswitch_by_name(host, vswitch_name):
return None


class VMwareHostVirtualSwitch(object):

class VMwareHostVirtualSwitch(PyVmomi):
def __init__(self, module):
super(VMwareHostVirtualSwitch, self).__init__(module)
self.host_system = None
self.content = None
self.vss = None
self.module = module
self.switch = module.params['switch']
self.number_of_ports = module.params['number_of_ports']
self.nics = module.params['nics']
self.mtu = module.params['mtu']
self.state = module.params['state']
self.content = connect_to_api(self.module)
self.esxi_hostname = module.params['esxi_hostname']

def process_state(self):
try:
Expand Down Expand Up @@ -182,11 +196,20 @@ def state_update_vswitch(self):
self.module.exit_json(changed=False, msg="Currently not implemented.")

def check_vswitch_configuration(self):
host = get_all_objs(self.content, [vim.HostSystem])
if not host:
hosts = get_all_objs(self.content, [vim.HostSystem])
if not hosts:
self.module.fail_json(msg="Unable to find host")

self.host_system = list(host.keys())[0]
desired_host_system = None
if self.esxi_hostname:
for host_system_obj, host_system_name in iteritems(hosts):
if host_system_name == self.esxi_hostname:
desired_host_system = host_system_obj

if desired_host_system:
self.host_system = desired_host_system
else:
self.host_system = list(hosts.keys())[0]
self.vss = find_vswitch_by_name(self.host_system, self.switch)

if self.vss is None:
Expand All @@ -203,12 +226,11 @@ def main():
number_of_ports=dict(type='int', default=128),
mtu=dict(type='int', default=1500),
state=dict(type='str', default='present', choices=['absent', 'present'])),
esxi_hostname=dict(type='str', aliases=['host']),
)

module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False)

if not HAS_PYVMOMI:
module.fail_json(msg='pyvmomi is required for this module')
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=False)

host_virtual_switch = VMwareHostVirtualSwitch(module)
host_virtual_switch.process_state()
Expand Down
28 changes: 28 additions & 0 deletions test/integration/targets/vmware_vswitch/tasks/main.yml
Expand Up @@ -126,3 +126,31 @@
#- assert:
# that:
# - remove_nic_again_run.changed == false


- name: get a list of Host Systems from vcsim
uri:
url: "{{ 'http://' + vcsim + ':5000/govc_find?filter=H' }}"
register: host_systems

- name: get a host system
set_fact: hs1="{{ host_systems['json'][0] | basename }}"

- debug: var=hs1

- name: Add vswitch to a specific host system
vmware_vswitch:
validate_certs: False
hostname: "{{ vcsim }}"
username: "{{ vcsim_instance['json']['username'] }}"
password: "{{ vcsim_instance['json']['password'] }}"
switch: vmswitch_0002
nics: vnic_1
esxi_hostname: hs1
register: add_vswitch_with_host_system

- debug: var=add_vswitch_with_host_system

- assert:
that:
- add_vswitch_with_host_system.changed == true

0 comments on commit e4254da

Please sign in to comment.