Skip to content

Commit

Permalink
add vcenter_host_connection
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemorency committed Apr 19, 2024
1 parent 2f17fd8 commit 9241216
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 0 deletions.
6 changes: 6 additions & 0 deletions changelogs/fragments/mm-feature__add-vcenter-host-connection
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
minor_changes:
- added role vcenter_host_connection

trivial:
- added integration tests for role vcenter_host_connection
124 changes: 124 additions & 0 deletions roles/vcenter_host_connection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# vcenter_host_connection

Add, remove, connect, disconnect, or reconnect an ESXi host from a VCenter

## Requirements

pyvomi

## Role Variables

### Auth
- **vcenter_host_connection_hostname**:
- str, The hostname of the vcenter to which you want to connect. Required

- **vcenter_host_connection_username**:
- str, The username to use to authenticate to the vcenter to which you want to connect. Required

- **vcenter_host_connection_password**:
- str, The password to use to authenticate to the esxi or vcenter to which you want to connect. Required

- **vcenter_host_connection_validate_certs**:
- bool, If true then certificates will be validated when connecting to the vcenter for auth. Optional.

- **vcenter_host_connection_port**:
- int, The port of the vcenter to which you want to connect. Optional.

### Placement
- **vcenter_host_connection_folder**:
- str, The folder path where the ESXi host should be added. Required if the cluster name is not provided

- **vcenter_host_connection_datacenter**:
- str, The datacenter name where the ESXi host should be added. Required

- **vcenter_host_connection_cluster**:
- str, The cluster name where the ESXi host should be added. Required if the folder name is not provided

### Connection Settings
- **vcenter_host_connection_state**:
- str, The connection state of the ESXi host that you want to set. Default is `present`
- If set to `present`, add the host if host is absent, or update the location of the host if host already exists.
- If set to `absent`, remove the host if host is present, or do nothing if host already does not exists.
- If set to `add_or_reconnect`, add the host if it's absent else reconnect it and update the location.
- If set to `reconnect`, then reconnect the host if it's present and update the location.
- If set to `disconnected`, disconnect the host if the host already exists.

- **vcenter_host_connection_add_connected**:
- bool, If true then the host will be connected as soon as its added to vcenter. Optional

- **vcenter_host_connection_esxi_hostname**:
- str, The hostname of the ESXi host that you want to manage. Required

- **vcenter_host_connection_esxi_username**:
- str, The username for the ESXi host that you want to manage. Required when adding the host.

- **vcenter_host_connection_esxi_password**:
- str, The password for the ESXi host that you want to manage. Required when adding the host.

- **vcenter_host_connection_esxi_ssl_thumbprint**:
- str, The SSL thumbprint for the ESXi host that you want to manage. Optional.

- **vcenter_host_connection_fetch_ssl_thumbprint**:
- bool, If true, the ESXi host thumprint will be fetched and trusted prior to adding. Optional.

- **vcenter_host_connection_force_connection**:
- bool, If true, the connection status will be forced even if the host is managed by another vcenter. Optional

- **vcenter_host_connection_reconnect_disconnected**:
- bool, Reconnect disconnected hosts, if the state is present and the host already exists. Optional

### Other
- **vcenter_host_connection_proxy_host**:
- str, The hostname of a proxy host that should be used for all HTTPs communication by the role. Optional

- **vcenter_host_connection_proxy_port**:
- str, The port of a proxy host that should be used for all HTTPs communication by the role. Optional


## Dependencies

- NA

## Example Playbook
```yaml
---
- name: Add an ESXi Host To VCenter
hosts: localhost
roles:
- role: cloud.vmware_ops.vcenter_host_connection
vars:
vcenter_host_connection_hostname: "{{ vcenter_hostname }}"
vcenter_host_connection_username: "{{ vcenter_username }}"
vcenter_host_connection_password: "{{ vcenter_password }}"
vcenter_host_connection_datacenter: dc1
vcenter_host_connection_cluster: cluster1
vcenter_host_connection_esxi_hostname: myesxi.contoso.org
vcenter_host_connection_esxi_username: root
vcenter_host_connection_esxi_password: supersecret!


- name: Remove an ESXi Host From VCenter
hosts: localhost
roles:
- role: cloud.vmware_ops.vcenter_host_connection
vars:
vcenter_host_connection_hostname: "{{ vcenter_hostname }}"
vcenter_host_connection_username: "{{ vcenter_username }}"
vcenter_host_connection_password: "{{ vcenter_password }}"
vcenter_host_connection_datacenter: dc1
vcenter_host_connection_cluster: cluster1
vcenter_host_connection_esxi_hostname: myesxi.contoso.org
vcenter_host_connection_state: absent
```
License
-------
GNU General Public License v3.0 or later
See [LICENCE](https://github.com/ansible-collections/cloud.aws_troubleshooting/blob/main/LICENSE) to see the full text.
Author Information
------------------
- Ansible Cloud Content Team
47 changes: 47 additions & 0 deletions roles/vcenter_host_connection/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
- name: Check General Mandatory Variables Are Defined
ansible.builtin.assert:
that:
- vcenter_host_connection_hostname is defined
- vcenter_host_connection_username is defined
- vcenter_host_connection_password is defined
- vcenter_host_connection_datacenter is defined
- vcenter_host_connection_cluster is defined or vcenter_host_connection_folder is defined
- vcenter_host_connection_esxi_hostname is defined
quiet: true
fail_msg: Variable must be set when using this role.

- name: Check Mandatory Variables For Adding A Host Are Defined
ansible.builtin.assert:
that:
- vcenter_host_connection_esxi_username is defined
- vcenter_host_connection_esxi_password is defined
quiet: true
fail_msg: Variable must be set when using this role.
when: >-
vcenter_host_connection_state is not defined or
vcenter_host_connection_state in ['present', 'add_or_reconnect']
- name: Manage ESXi Host Connection To vCenter
community.vmware.vmware_host:
hostname: "{{ vcenter_host_connection_hostname }}"
username: "{{ vcenter_host_connection_username }}"
password: "{{ vcenter_host_connection_password }}"
datacenter_name: "{{ vcenter_host_connection_datacenter }}"
cluster_name: "{{ vcenter_host_connection_cluster | default(omit) }}"
folder: "{{ vcenter_host_connection_folder | default(omit) }}"
port: "{{ vcenter_host_connection_port | default(omit) }}"
proxy_host: "{{ vcenter_host_connection_proxy_host | default(omit) }}"
proxy_port: "{{ vcenter_host_connection_proxy_port | default(omit) }}"
validate_certs: "{{ vcenter_host_connection_validate_certs | default(omit) }}"

add_connected: "{{ vcenter_host_connection_add_connected | default(omit) }}"
force_connection: "{{ vcenter_host_connection_force_connection | default(omit) }}"
reconnect_disconnected: "{{ vcenter_host_connection_reconnect_disconnected | default(omit) }}"
state: "{{ vcenter_host_connection_state | default(omit) }}"

esxi_hostname: "{{ vcenter_host_connection_esxi_hostname | default(omit) }}"
esxi_username: "{{ vcenter_host_connection_esxi_username | default(omit) }}"
esxi_password: "{{ vcenter_host_connection_esxi_password | default(omit) }}"
esxi_ssl_thumbprint: "{{ vcenter_host_connection_esxi_ssl_thumbprint | default(omit) }}"
fetch_ssl_thumbprint: "{{ vcenter_host_connection_fetch_ssl_thumbprint | default(omit) }}"
22 changes: 22 additions & 0 deletions tests/integration/targets/prepare_esx/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
- name: Install dependencies
ansible.builtin.pip:
name:
- requests
- pyVmomi

- name: Run esx vcSim
containers.podman.podman_container:
name: vmwareesx
image: docker.io/vmware/vcsim:latest
state: started
recreate: yes
expose:
- 8989
ports:
- 8990:8989
command: "vcsim -esx"

- name: Pause
ansible.builtin.pause:
seconds: 10
17 changes: 17 additions & 0 deletions tests/integration/targets/vcenter_host_connection_test/run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
- hosts: localhost
gather_facts: no
collections:
- community.general

tasks:
- name: Vcsim
ansible.builtin.import_role:
name: prepare_soap

- name: ESXi
ansible.builtin.import_role:
name: prepare_esx

- name: Import manage_vcenter_host_connection_test
ansible.builtin.import_role:
name: manage_vcenter_host_connection_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
source ../init.sh
exec ansible-playbook run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- name: Add ESXI Host
ansible.builtin.import_role:
name: cloud.vmware_ops.manage_vcenter_host_connection
vars:
manage_vcenter_host_connection_state: present

- name: Remove ESXI Host
ansible.builtin.import_role:
name: cloud.vmware_ops.manage_vcenter_host_connection
vars:
manage_vcenter_host_connection_state: absent
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
manage_vcenter_host_connection_hostname: "127.0.0.1"
manage_vcenter_host_connection_username: "test"
manage_vcenter_host_connection_password: "test"
manage_vcenter_host_connection_validate_certs: false
manage_vcenter_host_connection_port: 8989
manage_vcenter_host_connection_cluster: "DC0_C0"
manage_vcenter_host_connection_datacenter: "DC0"

manage_vcenter_host_connection_esxi_hostname: "127.0.0.1"
manage_vcenter_host_connection_esxi_username: test
manage_vcenter_host_connection_esxi_password: test

0 comments on commit 9241216

Please sign in to comment.