2015-08-24 17:48:17 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-01-12 16:30:28 +00:00
|
|
|
# Copyright: (c) 2015, Joseph Callen <jcallen () csc.com>
|
|
|
|
# Copyright: (c) 2017, Ansible Project
|
2019-02-25 08:38:33 +00:00
|
|
|
# Copyright: (c) 2018, Christian Kotte <christian.kotte@gmx.de>
|
2017-07-29 15:05:38 +00:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2018-01-12 16:30:28 +00:00
|
|
|
ANSIBLE_METADATA = {
|
|
|
|
'metadata_version': '1.1',
|
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'
|
|
|
|
}
|
2017-03-14 16:07:22 +00:00
|
|
|
|
2017-07-24 17:02:58 +00:00
|
|
|
DOCUMENTATION = r'''
|
2015-08-24 17:48:17 +00:00
|
|
|
---
|
|
|
|
module: vmware_host
|
2019-02-25 08:38:33 +00:00
|
|
|
short_description: Add, remove, or move an ESXi host to, from, or within vCenter
|
2015-08-24 17:48:17 +00:00
|
|
|
description:
|
2019-02-25 08:38:33 +00:00
|
|
|
- This module can be used to add, reconnect, or remove an ESXi host to or from vCenter.
|
|
|
|
- This module can also be used to move an ESXi host to a cluster or folder, or vice versa, within the same datacenter.
|
2017-07-24 17:02:58 +00:00
|
|
|
version_added: '2.0'
|
|
|
|
author:
|
|
|
|
- Joseph Callen (@jcpowermac)
|
|
|
|
- Russell Teague (@mtnbikenc)
|
2017-12-13 16:13:12 +00:00
|
|
|
- Maxime de Roucy (@tchernomax)
|
2019-02-25 08:38:33 +00:00
|
|
|
- Christian Kotte (@ckotte)
|
2015-08-24 17:48:17 +00:00
|
|
|
notes:
|
2019-02-25 08:38:33 +00:00
|
|
|
- Tested on vSphere 5.5, 6.0, 6.5 and 6.7
|
2015-08-24 17:48:17 +00:00
|
|
|
requirements:
|
2017-07-24 17:02:58 +00:00
|
|
|
- python >= 2.6
|
|
|
|
- PyVmomi
|
2019-02-25 08:38:33 +00:00
|
|
|
- ssl
|
|
|
|
- socket
|
|
|
|
- hashlib
|
2015-08-24 17:48:17 +00:00
|
|
|
options:
|
2017-07-24 17:02:58 +00:00
|
|
|
datacenter_name:
|
|
|
|
description:
|
|
|
|
- Name of the datacenter to add the host.
|
2018-05-07 15:20:48 +00:00
|
|
|
- Aliases added in version 2.6.
|
2017-07-24 17:02:58 +00:00
|
|
|
required: yes
|
2018-05-07 15:20:48 +00:00
|
|
|
aliases: ['datacenter']
|
2019-02-25 08:38:33 +00:00
|
|
|
type: str
|
2017-07-24 17:02:58 +00:00
|
|
|
cluster_name:
|
|
|
|
description:
|
|
|
|
- Name of the cluster to add the host.
|
2018-05-07 15:20:48 +00:00
|
|
|
- If C(folder) is not set, then this parameter is required.
|
|
|
|
- Aliases added in version 2.6.
|
|
|
|
aliases: ['cluster']
|
2019-02-25 08:38:33 +00:00
|
|
|
type: str
|
2018-05-07 15:20:48 +00:00
|
|
|
folder:
|
|
|
|
description:
|
|
|
|
- Name of the folder under which host to add.
|
|
|
|
- If C(cluster_name) is not set, then this parameter is required.
|
|
|
|
- "For example, if there is a datacenter 'dc1' under folder called 'Site1' then, this value will be '/Site1/dc1/host'."
|
|
|
|
- "Here 'host' is an invisible folder under VMware Web Client."
|
|
|
|
- "Another example, if there is a nested folder structure like '/myhosts/india/pune' under
|
|
|
|
datacenter 'dc2', then C(folder) value will be '/dc2/host/myhosts/india/pune'."
|
|
|
|
- "Other Examples: "
|
|
|
|
- " - '/Site2/dc2/Asia-Cluster/host'"
|
|
|
|
- " - '/dc3/Asia-Cluster/host'"
|
|
|
|
version_added: "2.6"
|
2019-02-25 08:38:33 +00:00
|
|
|
aliases: ['folder_name']
|
|
|
|
type: str
|
2018-05-07 15:20:48 +00:00
|
|
|
add_connected:
|
|
|
|
description:
|
|
|
|
- If set to C(True), then the host should be connected as soon as it is added.
|
|
|
|
- This parameter is ignored if state is set to a value other than C(present).
|
|
|
|
default: True
|
2019-02-25 08:38:33 +00:00
|
|
|
type: bool
|
2018-05-07 15:20:48 +00:00
|
|
|
version_added: "2.6"
|
2017-07-24 17:02:58 +00:00
|
|
|
esxi_hostname:
|
|
|
|
description:
|
|
|
|
- ESXi hostname to manage.
|
|
|
|
required: yes
|
2019-02-25 08:38:33 +00:00
|
|
|
type: str
|
2017-07-24 17:02:58 +00:00
|
|
|
esxi_username:
|
|
|
|
description:
|
|
|
|
- ESXi username.
|
2017-12-13 16:13:12 +00:00
|
|
|
- Required for adding a host.
|
2019-02-25 08:38:33 +00:00
|
|
|
- Optional for reconnect. If both C(esxi_username) and C(esxi_password) are used
|
2017-12-13 16:13:12 +00:00
|
|
|
- Unused for removing.
|
2018-05-07 15:20:48 +00:00
|
|
|
- No longer a required parameter from version 2.5.
|
2019-02-25 08:38:33 +00:00
|
|
|
type: str
|
2017-07-24 17:02:58 +00:00
|
|
|
esxi_password:
|
|
|
|
description:
|
|
|
|
- ESXi password.
|
2017-12-13 16:13:12 +00:00
|
|
|
- Required for adding a host.
|
|
|
|
- Optional for reconnect.
|
|
|
|
- Unused for removing.
|
2018-05-07 15:20:48 +00:00
|
|
|
- No longer a required parameter from version 2.5.
|
2019-02-25 08:38:33 +00:00
|
|
|
type: str
|
2017-07-24 17:02:58 +00:00
|
|
|
state:
|
|
|
|
description:
|
2019-02-25 08:38:33 +00:00
|
|
|
- If set to C(present), add the host if host is absent.
|
|
|
|
- If set to C(present), update the location of the host if host already exists.
|
|
|
|
- If set to C(absent), remove the host if host is present.
|
|
|
|
- If set to C(absent), do nothing if host already does not exists.
|
|
|
|
- If set to C(add_or_reconnect), add the host if it's absent else reconnect it and update the location.
|
|
|
|
- If set to C(reconnect), then reconnect the host if it's present and update the location.
|
2017-07-24 17:02:58 +00:00
|
|
|
default: present
|
2018-05-07 15:20:48 +00:00
|
|
|
choices: ['present', 'absent', 'add_or_reconnect', 'reconnect']
|
2019-02-25 08:38:33 +00:00
|
|
|
type: str
|
2018-01-12 16:30:28 +00:00
|
|
|
esxi_ssl_thumbprint:
|
|
|
|
description:
|
2018-05-07 15:20:48 +00:00
|
|
|
- "Specifying the hostsystem certificate's thumbprint."
|
|
|
|
- "Use following command to get hostsystem certificate's thumbprint - "
|
2018-01-12 16:30:28 +00:00
|
|
|
- "# openssl x509 -in /etc/vmware/ssl/rui.crt -fingerprint -sha1 -noout"
|
2019-02-25 08:38:33 +00:00
|
|
|
- Only used if C(fetch_thumbprint) isn't set to C(true).
|
2018-01-12 16:30:28 +00:00
|
|
|
version_added: 2.5
|
2018-02-21 15:20:42 +00:00
|
|
|
default: ''
|
2019-02-25 08:38:33 +00:00
|
|
|
type: str
|
|
|
|
aliases: ['ssl_thumbprint']
|
|
|
|
fetch_ssl_thumbprint:
|
|
|
|
description:
|
|
|
|
- Fetch the thumbprint of the host's SSL certificate.
|
|
|
|
- This basically disables the host certificate verification (check if it was signed by a recognized CA).
|
|
|
|
- Disable this option if you want to allow only hosts with valid certificates to be added to vCenter.
|
|
|
|
- If this option is set to C(false) and the certificate can't be verified, an add or reconnect will fail.
|
|
|
|
- Unused when C(esxi_ssl_thumbprint) is set.
|
|
|
|
- Optional for reconnect, but only used if C(esxi_username) and C(esxi_password) are used.
|
|
|
|
- Unused for removing.
|
|
|
|
type: bool
|
|
|
|
version_added: 2.8
|
|
|
|
default: True
|
|
|
|
force_connection:
|
|
|
|
description:
|
|
|
|
- Force the connection if the host is already being managed by another vCenter server.
|
|
|
|
type: bool
|
|
|
|
version_added: 2.8
|
|
|
|
default: True
|
|
|
|
reconnect_disconnected:
|
|
|
|
description:
|
|
|
|
- Reconnect disconnected hosts.
|
|
|
|
- This is only used if C(state) is set to C(present) and if the host already exists.
|
|
|
|
type: bool
|
|
|
|
version_added: 2.8
|
|
|
|
default: True
|
2016-01-12 17:00:22 +00:00
|
|
|
extends_documentation_fragment: vmware.documentation
|
2015-08-24 17:48:17 +00:00
|
|
|
'''
|
|
|
|
|
2017-07-24 17:02:58 +00:00
|
|
|
EXAMPLES = r'''
|
|
|
|
- name: Add ESXi Host to vCenter
|
|
|
|
vmware_host:
|
|
|
|
hostname: '{{ vcenter_hostname }}'
|
|
|
|
username: '{{ vcenter_username }}'
|
|
|
|
password: '{{ vcenter_password }}'
|
2019-02-25 08:38:33 +00:00
|
|
|
datacenter: datacenter_name
|
|
|
|
cluster: cluster_name
|
2017-07-24 17:02:58 +00:00
|
|
|
esxi_hostname: '{{ esxi_hostname }}'
|
|
|
|
esxi_username: '{{ esxi_username }}'
|
|
|
|
esxi_password: '{{ esxi_password }}'
|
|
|
|
state: present
|
2018-08-01 03:40:57 +00:00
|
|
|
delegate_to: localhost
|
2017-12-13 16:13:12 +00:00
|
|
|
|
2018-05-07 15:20:48 +00:00
|
|
|
- name: Add ESXi Host to vCenter under a specific folder
|
|
|
|
vmware_host:
|
|
|
|
hostname: '{{ vcenter_hostname }}'
|
|
|
|
username: '{{ vcenter_username }}'
|
|
|
|
password: '{{ vcenter_password }}'
|
2019-02-25 08:38:33 +00:00
|
|
|
datacenter: datacenter_name
|
2018-05-07 15:20:48 +00:00
|
|
|
folder: '/Site2/Asia-Cluster/host'
|
|
|
|
esxi_hostname: '{{ esxi_hostname }}'
|
|
|
|
esxi_username: '{{ esxi_username }}'
|
|
|
|
esxi_password: '{{ esxi_password }}'
|
|
|
|
state: present
|
|
|
|
add_connected: True
|
2018-08-01 03:40:57 +00:00
|
|
|
delegate_to: localhost
|
2018-05-07 15:20:48 +00:00
|
|
|
|
2017-12-13 16:13:12 +00:00
|
|
|
- name: Reconnect ESXi Host (with username/password set)
|
|
|
|
vmware_host:
|
|
|
|
hostname: '{{ vcenter_hostname }}'
|
|
|
|
username: '{{ vcenter_username }}'
|
|
|
|
password: '{{ vcenter_password }}'
|
2019-02-25 08:38:33 +00:00
|
|
|
datacenter: datacenter_name
|
|
|
|
cluster: cluster_name
|
2017-12-13 16:13:12 +00:00
|
|
|
esxi_hostname: '{{ esxi_hostname }}'
|
|
|
|
esxi_username: '{{ esxi_username }}'
|
|
|
|
esxi_password: '{{ esxi_password }}'
|
|
|
|
state: reconnect
|
2018-08-01 03:40:57 +00:00
|
|
|
delegate_to: localhost
|
2017-12-13 16:13:12 +00:00
|
|
|
|
|
|
|
- name: Reconnect ESXi Host (with default username/password)
|
|
|
|
vmware_host:
|
|
|
|
hostname: '{{ vcenter_hostname }}'
|
|
|
|
username: '{{ vcenter_username }}'
|
|
|
|
password: '{{ vcenter_password }}'
|
2019-02-25 08:38:33 +00:00
|
|
|
datacenter: datacenter_name
|
|
|
|
cluster: cluster_name
|
2017-12-13 16:13:12 +00:00
|
|
|
esxi_hostname: '{{ esxi_hostname }}'
|
|
|
|
state: reconnect
|
2018-08-01 03:40:57 +00:00
|
|
|
delegate_to: localhost
|
2018-01-12 16:30:28 +00:00
|
|
|
|
|
|
|
- name: Add ESXi Host with SSL Thumbprint to vCenter
|
|
|
|
vmware_host:
|
|
|
|
hostname: '{{ vcenter_hostname }}'
|
|
|
|
username: '{{ vcenter_username }}'
|
|
|
|
password: '{{ vcenter_password }}'
|
2019-02-25 08:38:33 +00:00
|
|
|
datacenter: datacenter_name
|
|
|
|
cluster: cluster_name
|
2018-01-12 16:30:28 +00:00
|
|
|
esxi_hostname: '{{ esxi_hostname }}'
|
|
|
|
esxi_username: '{{ esxi_username }}'
|
|
|
|
esxi_password: '{{ esxi_password }}'
|
|
|
|
esxi_ssl_thumbprint: "3C:A5:60:6F:7A:B7:C4:6C:48:28:3D:2F:A5:EC:A3:58:13:88:F6:DD"
|
|
|
|
state: present
|
2018-08-01 03:40:57 +00:00
|
|
|
delegate_to: localhost
|
2017-07-24 17:02:58 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = r'''
|
2018-05-07 15:20:48 +00:00
|
|
|
result:
|
|
|
|
description: metadata about the new host system added
|
|
|
|
returned: on successful addition
|
|
|
|
type: str
|
2019-02-25 08:38:33 +00:00
|
|
|
sample: "Host already connected to vCenter 'vcenter01' in cluster 'cluster01'"
|
2015-08-24 17:48:17 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
from pyVmomi import vim, vmodl
|
2019-02-25 08:38:33 +00:00
|
|
|
import ssl
|
|
|
|
import socket
|
|
|
|
import hashlib
|
2015-08-24 17:48:17 +00:00
|
|
|
except ImportError:
|
2018-01-12 16:30:28 +00:00
|
|
|
pass
|
2015-08-24 17:48:17 +00:00
|
|
|
|
2017-07-24 17:02:58 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2018-01-12 16:30:28 +00:00
|
|
|
from ansible.module_utils._text import to_native
|
2019-02-25 08:38:33 +00:00
|
|
|
from ansible.module_utils.vmware import (
|
|
|
|
PyVmomi, TaskError, vmware_argument_spec,
|
|
|
|
wait_for_task, find_host_by_cluster_datacenter, find_hostsystem_by_name
|
|
|
|
)
|
2017-07-24 17:02:58 +00:00
|
|
|
|
2015-08-24 17:48:17 +00:00
|
|
|
|
2018-01-12 16:30:28 +00:00
|
|
|
class VMwareHost(PyVmomi):
|
2019-02-25 08:38:33 +00:00
|
|
|
"""Class to manage vCenter connection"""
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
def __init__(self, module):
|
2018-01-12 16:30:28 +00:00
|
|
|
super(VMwareHost, self).__init__(module)
|
2019-02-25 08:38:33 +00:00
|
|
|
self.vcenter = module.params['hostname']
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
self.datacenter_name = module.params['datacenter_name']
|
|
|
|
self.cluster_name = module.params['cluster_name']
|
2018-05-07 15:20:48 +00:00
|
|
|
self.folder_name = module.params['folder']
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
self.esxi_hostname = module.params['esxi_hostname']
|
|
|
|
self.esxi_username = module.params['esxi_username']
|
|
|
|
self.esxi_password = module.params['esxi_password']
|
|
|
|
self.state = module.params['state']
|
2018-01-12 16:30:28 +00:00
|
|
|
self.esxi_ssl_thumbprint = module.params.get('esxi_ssl_thumbprint', '')
|
2019-02-25 08:38:33 +00:00
|
|
|
self.force_connection = module.params.get('force_connection')
|
|
|
|
self.fetch_ssl_thumbprint = module.params.get('fetch_ssl_thumbprint')
|
|
|
|
self.reconnect_disconnected = module.params.get('reconnect_disconnected')
|
|
|
|
self.host_update = self.host = self.cluster = self.folder = self.host_parent_compute_resource = None
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
|
|
|
|
def process_state(self):
|
2019-02-25 08:38:33 +00:00
|
|
|
"""Check the current state"""
|
2018-01-12 16:30:28 +00:00
|
|
|
host_states = {
|
|
|
|
'absent': {
|
|
|
|
'present': self.state_remove_host,
|
2019-02-25 08:38:33 +00:00
|
|
|
'update': self.state_remove_host,
|
2018-01-12 16:30:28 +00:00
|
|
|
'absent': self.state_exit_unchanged,
|
|
|
|
},
|
|
|
|
'present': {
|
|
|
|
'present': self.state_exit_unchanged,
|
2019-02-25 08:38:33 +00:00
|
|
|
'update': self.state_update_host,
|
2018-01-12 16:30:28 +00:00
|
|
|
'absent': self.state_add_host,
|
|
|
|
},
|
|
|
|
'add_or_reconnect': {
|
|
|
|
'present': self.state_reconnect_host,
|
2019-02-25 08:38:33 +00:00
|
|
|
'update': self.state_update_host,
|
2018-01-12 16:30:28 +00:00
|
|
|
'absent': self.state_add_host,
|
|
|
|
},
|
|
|
|
'reconnect': {
|
|
|
|
'present': self.state_reconnect_host,
|
2019-02-25 08:38:33 +00:00
|
|
|
'update': self.state_update_host,
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
}
|
2018-01-12 16:30:28 +00:00
|
|
|
}
|
2015-08-24 17:48:17 +00:00
|
|
|
|
2018-01-12 16:30:28 +00:00
|
|
|
try:
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
host_states[self.state][self.check_host_state()]()
|
|
|
|
except vmodl.RuntimeFault as runtime_fault:
|
2018-01-12 16:30:28 +00:00
|
|
|
self.module.fail_json(msg=to_native(runtime_fault.msg))
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
except vmodl.MethodFault as method_fault:
|
2018-01-12 16:30:28 +00:00
|
|
|
self.module.fail_json(msg=to_native(method_fault.msg))
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
except Exception as e:
|
2018-01-12 16:30:28 +00:00
|
|
|
self.module.fail_json(msg=to_native(e))
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
|
2019-02-25 08:38:33 +00:00
|
|
|
def check_host_state(self):
|
|
|
|
"""Check current state"""
|
|
|
|
# Check if the host is already connected to vCenter
|
|
|
|
self.host_update = find_hostsystem_by_name(self.content, self.esxi_hostname)
|
|
|
|
if self.host_update:
|
|
|
|
# The host name is unique in vCenter; A host with the same name cannot exist in another datacenter
|
|
|
|
# However, the module will fail later if the target folder/cluster is in another datacenter as the host
|
|
|
|
# Check if the host is connected under the target cluster
|
|
|
|
if self.cluster_name:
|
|
|
|
self.host, self.cluster = self.search_cluster(self.datacenter_name, self.cluster_name, self.esxi_hostname)
|
|
|
|
if self.host:
|
|
|
|
state = 'present'
|
|
|
|
else:
|
|
|
|
state = 'update'
|
|
|
|
# Check if the host is connected under the target folder
|
|
|
|
elif self.folder_name:
|
|
|
|
self.folder = self.search_folder(self.folder_name)
|
|
|
|
for child in self.folder.childEntity:
|
|
|
|
if not child or not isinstance(child, vim.ComputeResource):
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
if isinstance(child.host[0], vim.HostSystem) and child.name == self.esxi_hostname:
|
|
|
|
self.host_parent_compute_resource = child
|
|
|
|
self.host = child.host[0]
|
|
|
|
break
|
|
|
|
except IndexError:
|
|
|
|
continue
|
|
|
|
if self.host:
|
|
|
|
state = 'present'
|
|
|
|
else:
|
|
|
|
state = 'update'
|
|
|
|
else:
|
|
|
|
state = 'absent'
|
|
|
|
return state
|
|
|
|
|
|
|
|
def search_folder(self, folder_name):
|
|
|
|
"""
|
|
|
|
Search folder in vCenter
|
|
|
|
Returns: folder object
|
|
|
|
"""
|
|
|
|
search_index = self.content.searchIndex
|
|
|
|
folder_obj = search_index.FindByInventoryPath(folder_name)
|
|
|
|
if not (folder_obj and isinstance(folder_obj, vim.Folder)):
|
|
|
|
self.module.fail_json(msg="Folder '%s' not found" % folder_name)
|
|
|
|
return folder_obj
|
|
|
|
|
|
|
|
def search_cluster(self, datacenter_name, cluster_name, esxi_hostname):
|
|
|
|
"""
|
|
|
|
Search cluster in vCenter
|
|
|
|
Returns: host and cluster object
|
|
|
|
"""
|
|
|
|
return find_host_by_cluster_datacenter(
|
|
|
|
self.module, self.content, datacenter_name, cluster_name, esxi_hostname
|
|
|
|
)
|
|
|
|
|
|
|
|
def state_exit_unchanged(self):
|
|
|
|
"""Exit with status message"""
|
|
|
|
if self.reconnect_disconnected and self.host_update.runtime.connectionState == 'disconnected':
|
|
|
|
self.state_reconnect_host()
|
|
|
|
else:
|
|
|
|
if self.folder_name:
|
|
|
|
result = "Host already connected to vCenter '%s' in folder '%s'" % (self.vcenter, self.folder_name)
|
|
|
|
elif self.cluster_name:
|
|
|
|
result = "Host already connected to vCenter '%s' in cluster '%s'" % (self.vcenter, self.cluster_name)
|
|
|
|
self.module.exit_json(changed=False, result=str(result))
|
|
|
|
|
|
|
|
def state_add_host(self):
|
|
|
|
"""Add ESXi host to a cluster of folder in vCenter"""
|
|
|
|
changed = True
|
|
|
|
result = None
|
|
|
|
|
|
|
|
if self.module.check_mode:
|
|
|
|
result = "Host would be connected to vCenter '%s'" % self.vcenter
|
|
|
|
else:
|
|
|
|
host_connect_spec = self.get_host_connect_spec()
|
|
|
|
as_connected = self.params.get('add_connected')
|
|
|
|
esxi_license = None
|
|
|
|
resource_pool = None
|
|
|
|
task = None
|
|
|
|
if self.folder_name:
|
|
|
|
self.folder = self.search_folder(self.folder_name)
|
|
|
|
try:
|
|
|
|
task = self.folder.AddStandaloneHost(
|
|
|
|
spec=host_connect_spec, compResSpec=resource_pool,
|
|
|
|
addConnected=as_connected, license=esxi_license
|
|
|
|
)
|
|
|
|
except vim.fault.InvalidLogin as invalid_login:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Cannot authenticate with the host : %s" % to_native(invalid_login)
|
|
|
|
)
|
|
|
|
except vim.fault.HostConnectFault as connect_fault:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="An error occurred during connect : %s" % to_native(connect_fault)
|
|
|
|
)
|
|
|
|
except vim.fault.DuplicateName as duplicate_name:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The folder already contains a host with the same name : %s" %
|
|
|
|
to_native(duplicate_name)
|
|
|
|
)
|
|
|
|
except vmodl.fault.InvalidArgument as invalid_argument:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="An argument was specified incorrectly : %s" % to_native(invalid_argument)
|
|
|
|
)
|
|
|
|
except vim.fault.AlreadyBeingManaged as already_managed:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host is already being managed by another vCenter server : %s" %
|
|
|
|
to_native(already_managed)
|
|
|
|
)
|
|
|
|
except vmodl.fault.NotEnoughLicenses as not_enough_licenses:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="There are not enough licenses to add this host : %s" % to_native(not_enough_licenses)
|
|
|
|
)
|
|
|
|
except vim.fault.NoHost as no_host:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Unable to contact the host : %s" % to_native(no_host)
|
|
|
|
)
|
|
|
|
except vmodl.fault.NotSupported as not_supported:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The folder is not a host folder : %s" % to_native(not_supported)
|
|
|
|
)
|
|
|
|
except vim.fault.NotSupportedHost as host_not_supported:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host is running a software version that is not supported : %s" %
|
|
|
|
to_native(host_not_supported)
|
|
|
|
)
|
|
|
|
except vim.fault.AgentInstallFailed as agent_install:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Error during vCenter agent installation : %s" % to_native(agent_install)
|
|
|
|
)
|
|
|
|
except vim.fault.AlreadyConnected as already_connected:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host is already connected to the vCenter server : %s" % to_native(already_connected)
|
|
|
|
)
|
|
|
|
except vim.fault.SSLVerifyFault as ssl_fault:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host certificate could not be authenticated : %s" % to_native(ssl_fault)
|
|
|
|
)
|
|
|
|
elif self.cluster_name:
|
|
|
|
self.host, self.cluster = self.search_cluster(
|
|
|
|
self.datacenter_name,
|
|
|
|
self.cluster_name,
|
|
|
|
self.esxi_hostname
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
task = self.cluster.AddHost_Task(
|
|
|
|
spec=host_connect_spec, asConnected=as_connected,
|
|
|
|
resourcePool=resource_pool, license=esxi_license
|
|
|
|
)
|
|
|
|
except vim.fault.InvalidLogin as invalid_login:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Cannot authenticate with the host : %s" % to_native(invalid_login)
|
|
|
|
)
|
|
|
|
except vim.fault.HostConnectFault as connect_fault:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="An error occurred during connect : %s" % to_native(connect_fault)
|
|
|
|
)
|
|
|
|
except vim.fault.DuplicateName as duplicate_name:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The cluster already contains a host with the same name : %s" %
|
|
|
|
to_native(duplicate_name)
|
|
|
|
)
|
|
|
|
except vim.fault.AlreadyBeingManaged as already_managed:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host is already being managed by another vCenter server : %s" %
|
|
|
|
to_native(already_managed)
|
|
|
|
)
|
|
|
|
except vmodl.fault.NotEnoughLicenses as not_enough_licenses:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="There are not enough licenses to add this host : %s" % to_native(not_enough_licenses)
|
|
|
|
)
|
|
|
|
except vim.fault.NoHost as no_host:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Unable to contact the host : %s" % to_native(no_host)
|
|
|
|
)
|
|
|
|
except vim.fault.NotSupportedHost as host_not_supported:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host is running a software version that is not supported; "
|
|
|
|
"It may still be possible to add the host as a stand-alone host : %s" %
|
|
|
|
to_native(host_not_supported)
|
|
|
|
)
|
|
|
|
except vim.fault.TooManyHosts as too_many_hosts:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="No additional hosts can be added to the cluster : %s" % to_native(too_many_hosts)
|
|
|
|
)
|
|
|
|
except vim.fault.AgentInstallFailed as agent_install:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Error during vCenter agent installation : %s" % to_native(agent_install)
|
|
|
|
)
|
|
|
|
except vim.fault.AlreadyConnected as already_connected:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host is already connected to the vCenter server : %s" % to_native(already_connected)
|
|
|
|
)
|
|
|
|
except vim.fault.SSLVerifyFault as ssl_fault:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host certificate could not be authenticated : %s" % to_native(ssl_fault)
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
changed, result = wait_for_task(task)
|
|
|
|
result = "Host connected to vCenter '%s'" % self.vcenter
|
|
|
|
except TaskError as task_error:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Failed to add host to vCenter '%s' : %s" % (self.vcenter, to_native(task_error))
|
|
|
|
)
|
|
|
|
|
|
|
|
self.module.exit_json(changed=changed, result=result)
|
|
|
|
|
2018-05-07 15:20:48 +00:00
|
|
|
def get_host_connect_spec(self):
|
|
|
|
"""
|
|
|
|
Function to return Host connection specification
|
|
|
|
Returns: host connection specification
|
|
|
|
"""
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
host_connect_spec = vim.host.ConnectSpec()
|
|
|
|
host_connect_spec.hostName = self.esxi_hostname
|
|
|
|
host_connect_spec.userName = self.esxi_username
|
|
|
|
host_connect_spec.password = self.esxi_password
|
2019-02-25 08:38:33 +00:00
|
|
|
host_connect_spec.force = self.force_connection
|
|
|
|
# Get the thumbprint of the SSL certificate
|
|
|
|
if self.fetch_ssl_thumbprint and self.esxi_ssl_thumbprint == '':
|
|
|
|
# We need to grab the thumbprint manually because it's not included in
|
|
|
|
# the task error via an SSLVerifyFault exception anymore
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sock.settimeout(1)
|
|
|
|
wrapped_socket = ssl.wrap_socket(sock)
|
|
|
|
try:
|
|
|
|
wrapped_socket.connect((self.esxi_hostname, 443))
|
|
|
|
except socket.error as socket_error:
|
|
|
|
self.module.fail_json(msg="Cannot connect to host : %s" % socket_error)
|
|
|
|
else:
|
|
|
|
der_cert_bin = wrapped_socket.getpeercert(True)
|
|
|
|
# thumb_md5 = hashlib.md5(der_cert_bin).hexdigest()
|
|
|
|
thumb_sha1 = self.format_number(hashlib.sha1(der_cert_bin).hexdigest())
|
|
|
|
# thumb_sha256 = hashlib.sha256(der_cert_bin).hexdigest()
|
|
|
|
wrapped_socket.close()
|
|
|
|
host_connect_spec.sslThumbprint = thumb_sha1
|
|
|
|
else:
|
|
|
|
host_connect_spec.sslThumbprint = self.esxi_ssl_thumbprint
|
2018-05-07 15:20:48 +00:00
|
|
|
return host_connect_spec
|
|
|
|
|
2019-02-25 08:38:33 +00:00
|
|
|
@staticmethod
|
|
|
|
def format_number(number):
|
|
|
|
"""Format number"""
|
|
|
|
string = str(number)
|
|
|
|
return ':'.join(a + b for a, b in zip(string[::2], string[1::2]))
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
|
2019-02-25 08:38:33 +00:00
|
|
|
def state_reconnect_host(self):
|
|
|
|
"""Reconnect host to vCenter"""
|
|
|
|
changed = True
|
|
|
|
result = None
|
|
|
|
|
|
|
|
if self.module.check_mode:
|
|
|
|
result = "Host would be reconnected to vCenter '%s'" % self.vcenter
|
|
|
|
else:
|
|
|
|
self.reconnect_host(self.host)
|
|
|
|
result = "Host reconnected to vCenter '%s'" % self.vcenter
|
|
|
|
self.module.exit_json(changed=changed, result=str(result))
|
|
|
|
|
|
|
|
def reconnect_host(self, host_object):
|
|
|
|
"""Reconnect host to vCenter"""
|
2017-12-13 16:13:12 +00:00
|
|
|
reconnecthost_args = {}
|
|
|
|
reconnecthost_args['reconnectSpec'] = vim.HostSystem.ReconnectSpec()
|
|
|
|
reconnecthost_args['reconnectSpec'].syncState = True
|
|
|
|
|
2019-02-25 08:38:33 +00:00
|
|
|
if self.esxi_username and self.esxi_password:
|
|
|
|
# Build the connection spec as well and fetch thumbprint if enabled
|
|
|
|
# Usefull if you reinstalled a host and it uses a new self-signed certificate
|
2018-05-07 15:20:48 +00:00
|
|
|
reconnecthost_args['cnxSpec'] = self.get_host_connect_spec()
|
2019-02-25 08:38:33 +00:00
|
|
|
try:
|
|
|
|
task = host_object.ReconnectHost_Task(**reconnecthost_args)
|
|
|
|
except vim.fault.InvalidLogin as invalid_login:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Cannot authenticate with the host : %s" % to_native(invalid_login)
|
|
|
|
)
|
|
|
|
except vim.fault.InvalidState as invalid_state:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host is not disconnected : %s" % to_native(invalid_state)
|
|
|
|
)
|
|
|
|
except vim.fault.InvalidName as invalid_name:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host name is invalid : %s" % to_native(invalid_name)
|
|
|
|
)
|
|
|
|
except vim.fault.HostConnectFault as connect_fault:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="An error occurred during reconnect : %s" % to_native(connect_fault)
|
|
|
|
)
|
|
|
|
except vmodl.fault.NotSupported as not_supported:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="No host can be added to this group : %s" % to_native(not_supported)
|
|
|
|
)
|
|
|
|
except vim.fault.AlreadyBeingManaged as already_managed:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host is already being managed by another vCenter server : %s" % to_native(already_managed)
|
|
|
|
)
|
|
|
|
except vmodl.fault.NotEnoughLicenses as not_enough_licenses:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="There are not enough licenses to add this host : %s" % to_native(not_enough_licenses)
|
|
|
|
)
|
|
|
|
except vim.fault.NoHost as no_host:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Unable to contact the host : %s" % to_native(no_host)
|
|
|
|
)
|
|
|
|
except vim.fault.NotSupportedHost as host_not_supported:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host is running a software version that is not supported : %s" %
|
|
|
|
to_native(host_not_supported)
|
|
|
|
)
|
|
|
|
except vim.fault.SSLVerifyFault as ssl_fault:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host certificate could not be authenticated : %s" % to_native(ssl_fault)
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
changed, result = wait_for_task(task)
|
|
|
|
except TaskError as task_error:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Failed to reconnect host to vCenter '%s' due to %s" %
|
|
|
|
(self.vcenter, to_native(task_error))
|
|
|
|
)
|
2015-08-24 17:48:17 +00:00
|
|
|
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
def state_remove_host(self):
|
2019-02-25 08:38:33 +00:00
|
|
|
"""Remove host from vCenter"""
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
changed = True
|
|
|
|
result = None
|
2019-02-25 08:38:33 +00:00
|
|
|
if self.module.check_mode:
|
|
|
|
result = "Host would be removed from vCenter '%s'" % self.vcenter
|
|
|
|
else:
|
|
|
|
# Check parent type
|
|
|
|
parent_type = self.get_parent_type(self.host_update)
|
|
|
|
if parent_type == 'cluster':
|
|
|
|
self.put_host_in_maintenance_mode(self.host_update)
|
|
|
|
try:
|
|
|
|
if self.folder_name:
|
|
|
|
task = self.host_parent_compute_resource.Destroy_Task()
|
|
|
|
elif self.cluster_name:
|
|
|
|
task = self.host.Destroy_Task()
|
|
|
|
except vim.fault.VimFault as vim_fault:
|
|
|
|
self.module.fail_json(msg=vim_fault)
|
|
|
|
try:
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
changed, result = wait_for_task(task)
|
2019-02-25 08:38:33 +00:00
|
|
|
result = "Host removed from vCenter '%s'" % self.vcenter
|
|
|
|
except TaskError as task_error:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Failed to remove the host from vCenter '%s' : %s" % (self.vcenter, to_native(task_error))
|
|
|
|
)
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
self.module.exit_json(changed=changed, result=str(result))
|
2015-08-24 17:48:17 +00:00
|
|
|
|
2019-02-25 08:38:33 +00:00
|
|
|
def put_host_in_maintenance_mode(self, host_object):
|
|
|
|
"""Put host in maintenance mode, if not already"""
|
|
|
|
if not host_object.runtime.inMaintenanceMode:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
maintenance_mode_task = host_object.EnterMaintenanceMode_Task(300, True, None)
|
|
|
|
except vim.fault.InvalidState as invalid_state:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host is already in maintenance mode : %s" % to_native(invalid_state)
|
|
|
|
)
|
|
|
|
except vim.fault.Timedout as timed_out:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The maintenance mode operation timed out : %s" % to_native(timed_out)
|
|
|
|
)
|
|
|
|
except vim.fault.Timedout as timed_out:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The maintenance mode operation was canceled : %s" % to_native(timed_out)
|
|
|
|
)
|
|
|
|
wait_for_task(maintenance_mode_task)
|
|
|
|
except TaskError as task_err:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Failed to put the host in maintenance mode : %s" % to_native(task_err)
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_parent_type(self, host_object):
|
|
|
|
"""
|
|
|
|
Get the type of the parent object
|
|
|
|
Returns: string with 'folder' or 'cluster'
|
|
|
|
"""
|
|
|
|
object_type = None
|
|
|
|
# check 'vim.ClusterComputeResource' first because it's also an
|
|
|
|
# instance of 'vim.ComputeResource'
|
|
|
|
if isinstance(host_object.parent, vim.ClusterComputeResource):
|
|
|
|
object_type = 'cluster'
|
|
|
|
elif isinstance(host_object.parent, vim.ComputeResource):
|
|
|
|
object_type = 'folder'
|
|
|
|
return object_type
|
2015-08-24 17:48:17 +00:00
|
|
|
|
2019-02-25 08:38:33 +00:00
|
|
|
def state_update_host(self):
|
|
|
|
"""Move host to a cluster or a folder, or vice versa"""
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
changed = True
|
|
|
|
result = None
|
2019-02-25 08:38:33 +00:00
|
|
|
reconnect = False
|
2015-08-24 17:48:17 +00:00
|
|
|
|
2019-02-25 08:38:33 +00:00
|
|
|
# Check if the host is disconnected if reconnect disconnected hosts is true
|
|
|
|
if self.reconnect_disconnected and self.host_update.runtime.connectionState == 'disconnected':
|
|
|
|
reconnect = True
|
2015-08-24 17:48:17 +00:00
|
|
|
|
2019-02-25 08:38:33 +00:00
|
|
|
# Check parent type
|
|
|
|
parent_type = self.get_parent_type(self.host_update)
|
2017-12-13 16:13:12 +00:00
|
|
|
|
2019-02-25 08:38:33 +00:00
|
|
|
if self.folder_name:
|
|
|
|
if self.module.check_mode:
|
|
|
|
if reconnect or self.state == 'add_or_reconnect' or self.state == 'reconnect':
|
|
|
|
result = "Host would be reconnected and moved to folder '%s'" % self.folder_name
|
|
|
|
else:
|
|
|
|
result = "Host would be moved to folder '%s'" % self.folder_name
|
|
|
|
else:
|
|
|
|
# Reconnect the host if disconnected or if specified by state
|
|
|
|
if reconnect or self.state == 'add_or_reconnect' or self.state == 'reconnect':
|
|
|
|
self.reconnect_host(self.host_update)
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
if parent_type == 'folder':
|
|
|
|
# Move ESXi host from folder to folder
|
|
|
|
task = self.folder.MoveIntoFolder_Task([self.host_update.parent])
|
|
|
|
elif parent_type == 'cluster':
|
|
|
|
self.put_host_in_maintenance_mode(self.host_update)
|
|
|
|
# Move ESXi host from cluster to folder
|
|
|
|
task = self.folder.MoveIntoFolder_Task([self.host_update])
|
|
|
|
except vim.fault.DuplicateName as duplicate_name:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The folder already contains an object with the specified name : %s" %
|
|
|
|
to_native(duplicate_name)
|
|
|
|
)
|
|
|
|
except vim.fault.InvalidFolder as invalid_folder:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The parent of this folder is in the list of objects : %s" %
|
|
|
|
to_native(invalid_folder)
|
|
|
|
)
|
|
|
|
except vim.fault.InvalidState as invalid_state:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Failed to move host, this can be due to either of following :"
|
|
|
|
" 1. The host is not part of the same datacenter, 2. The host is not in maintenance mode : %s" %
|
|
|
|
to_native(invalid_state)
|
|
|
|
)
|
|
|
|
except vmodl.fault.NotSupported as not_supported:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The target folder is not a host folder : %s" %
|
|
|
|
to_native(not_supported)
|
|
|
|
)
|
|
|
|
except vim.fault.DisallowedOperationOnFailoverHost as failover_host:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host is configured as a failover host : %s" %
|
|
|
|
to_native(failover_host)
|
|
|
|
)
|
|
|
|
except vim.fault.VmAlreadyExistsInDatacenter as already_exists:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host's virtual machines are already registered to a host in "
|
|
|
|
"the destination datacenter : %s" % to_native(already_exists)
|
|
|
|
)
|
|
|
|
changed, result = wait_for_task(task)
|
|
|
|
except TaskError as task_error_exception:
|
|
|
|
task_error = task_error_exception.args[0]
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Failed to move host %s to folder %s due to %s" %
|
|
|
|
(self.esxi_hostname, self.folder_name, to_native(task_error))
|
|
|
|
)
|
|
|
|
if reconnect or self.state == 'add_or_reconnect' or self.state == 'reconnect':
|
|
|
|
result = "Host reconnected and moved to folder '%s'" % self.folder_name
|
|
|
|
else:
|
|
|
|
result = "Host moved to folder '%s'" % self.folder_name
|
|
|
|
elif self.cluster_name:
|
|
|
|
if self.module.check_mode:
|
|
|
|
result = "Host would be moved to cluster '%s'" % self.cluster_name
|
|
|
|
else:
|
|
|
|
if parent_type == 'cluster':
|
|
|
|
# Put host in maintenance mode if moved from another cluster
|
|
|
|
self.put_host_in_maintenance_mode(self.host_update)
|
|
|
|
resource_pool = None
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
task = self.cluster.MoveHostInto_Task(
|
|
|
|
host=self.host_update, resourcePool=resource_pool
|
|
|
|
)
|
|
|
|
except vim.fault.TooManyHosts as too_many_hosts:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="No additional hosts can be added to the cluster : %s" % to_native(too_many_hosts)
|
|
|
|
)
|
|
|
|
except vim.fault.InvalidState as invalid_state:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="The host is already part of a cluster and is not in maintenance mode : %s" %
|
|
|
|
to_native(invalid_state)
|
|
|
|
)
|
|
|
|
except vmodl.fault.InvalidArgument as invalid_argument:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Failed to move host, this can be due to either of following :"
|
|
|
|
" 1. The host is is not a part of the same datacenter as the cluster,"
|
|
|
|
" 2. The source and destination clusters are the same : %s" %
|
|
|
|
to_native(invalid_argument)
|
|
|
|
)
|
|
|
|
changed, result = wait_for_task(task)
|
|
|
|
except TaskError as task_error_exception:
|
|
|
|
task_error = task_error_exception.args[0]
|
|
|
|
self.module.fail_json(
|
|
|
|
msg="Failed to move host to cluster '%s' due to : %s" %
|
|
|
|
(self.cluster_name, to_native(task_error))
|
|
|
|
)
|
|
|
|
if reconnect or self.state == 'add_or_reconnect' or self.state == 'reconnect':
|
|
|
|
result = "Host reconnected and moved to cluster '%s'" % self.cluster_name
|
|
|
|
else:
|
|
|
|
result = "Host moved to cluster '%s'" % self.cluster_name
|
2017-12-13 16:13:12 +00:00
|
|
|
|
2019-02-25 08:38:33 +00:00
|
|
|
self.module.exit_json(changed=changed, msg=str(result))
|
2015-08-24 17:48:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-02-25 08:38:33 +00:00
|
|
|
"""Main"""
|
2015-08-24 17:48:17 +00:00
|
|
|
argument_spec = vmware_argument_spec()
|
2017-07-24 17:02:58 +00:00
|
|
|
argument_spec.update(
|
2018-05-07 15:20:48 +00:00
|
|
|
datacenter_name=dict(type='str', required=True, aliases=['datacenter']),
|
|
|
|
cluster_name=dict(type='str', aliases=['cluster']),
|
2017-07-24 17:02:58 +00:00
|
|
|
esxi_hostname=dict(type='str', required=True),
|
2018-05-07 15:20:48 +00:00
|
|
|
esxi_username=dict(type='str'),
|
|
|
|
esxi_password=dict(type='str', no_log=True),
|
2019-02-25 08:38:33 +00:00
|
|
|
esxi_ssl_thumbprint=dict(type='str', default='', aliases=['ssl_thumbprint']),
|
|
|
|
fetch_ssl_thumbprint=dict(type='bool', default=True),
|
2017-12-13 16:13:12 +00:00
|
|
|
state=dict(default='present',
|
|
|
|
choices=['present', 'absent', 'add_or_reconnect', 'reconnect'],
|
2018-05-07 15:20:48 +00:00
|
|
|
type='str'),
|
2019-02-25 08:38:33 +00:00
|
|
|
folder=dict(type='str', aliases=['folder_name']),
|
2018-05-07 15:20:48 +00:00
|
|
|
add_connected=dict(type='bool', default=True),
|
2019-02-25 08:38:33 +00:00
|
|
|
force_connection=dict(type='bool', default=True),
|
|
|
|
reconnect_disconnected=dict(type='bool', default=True),
|
2018-01-12 16:30:28 +00:00
|
|
|
)
|
2017-07-24 17:02:58 +00:00
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
|
|
|
supports_check_mode=True,
|
2017-12-13 16:13:12 +00:00
|
|
|
required_if=[
|
|
|
|
['state', 'present', ['esxi_username', 'esxi_password']],
|
2018-01-12 16:30:28 +00:00
|
|
|
['state', 'add_or_reconnect', ['esxi_username', 'esxi_password']]
|
2018-05-07 15:20:48 +00:00
|
|
|
],
|
|
|
|
required_one_of=[
|
|
|
|
['cluster_name', 'folder'],
|
|
|
|
],
|
|
|
|
mutually_exclusive=[
|
|
|
|
['cluster_name', 'folder'],
|
2018-01-12 16:30:28 +00:00
|
|
|
]
|
|
|
|
)
|
2015-08-24 17:48:17 +00:00
|
|
|
|
Resolves issue with vmware_host module for v2.0
When this module was written back in May 2015 we were using 1.9.x. Being lazy I added to param the objects that the other functions would need. What I have noticed is in 2.0 exit_json is trying to jsonify those complex objects and failing. This PR resolves that issue with the vmware_host module.
@kamsz reported this issue in https://github.com/ansible/ansible-modules-extras/pull/1568
Playbook
```
- name: Add Host
local_action:
module: vmware_host
hostname: "{{ mgmt_ip_address }}"
username: "{{ vcsa_user }}"
password: "{{ vcsa_pass }}"
datacenter_name: "{{ mgmt_vdc }}"
cluster_name: "{{ mgmt_cluster }}"
esxi_hostname: "{{ hostvars[item].hostname }}"
esxi_username: "{{ esxi_username }}"
esxi_password: "{{ site_passwd }}"
state: present
with_items: groups['foundation_esxi']
```
Module Testing
```
TASK [Add Host] ****************************************************************
task path: /opt/autodeploy/projects/emmet/site_deploy.yml:214
ESTABLISH LOCAL CONNECTION FOR USER: root
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937 )" )
localhost PUT /tmp/tmppmr9i9 TO /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693866.1-87710459703937/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834 )" )
localhost PUT /tmp/tmpVB81f2 TO /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693943.8-75870536677834/" > /dev/null 2>&1
localhost EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563 )" )
localhost PUT /tmp/tmpFB7VQB TO /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host
localhost EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/vmware_host; rm -rf "/root/.ansible/tmp/ansible-tmp-1454693991.56-163414752982563/" > /dev/null 2>&1
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-01) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp001", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-01", "result": "'vim.HostSystem:host-15'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-02) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp002", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-02", "result": "'vim.HostSystem:host-20'"}
changed: [foundation-vcsa -> localhost] => (item=foundation-esxi-03) => {"changed": true, "invocation": {"module_args": {"cluster_name": "Foundation", "datacenter_name": "Test-Lab", "esxi_hostname": "cscesxtmp003", "esxi_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "esxi_username": "root", "hostname": "172.27.0.100", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "state": "present", "username": "root"}, "module_name": "vmware_host"}, "item": "foundation-esxi-03", "result": "'vim.HostSystem:host-21'"}
```
2016-02-05 19:48:12 +00:00
|
|
|
vmware_host = VMwareHost(module)
|
|
|
|
vmware_host.process_state()
|
2015-08-24 17:48:17 +00:00
|
|
|
|
2017-07-29 15:05:38 +00:00
|
|
|
|
2015-08-24 17:48:17 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|