Tony_Wong
(Tony Wong)
April 13, 2020, 3:48pm
1
I am new to ansible and want to know how to add a esxi host to vcenter in a datacenter
I have the following in a yml file but not sure what else to do
hosts: localhost
connection: local
tasks:
name: include vars
include_vars:
name: Add ESXi Host to vCenter
vmware_host:
hostname: ‘{{ vcenter_hostname }}’
username: ‘{{ vcenter_username }}’
password: ‘{{ vcenter_password }}’
datacenter: datacenter_name
esxi_hostname: ‘{{ esxi_hostname }}’
esxi_username: ‘{{ esxi_username }}’
esxi_password: ‘{{ esxi_password }}’
state: present
delegate_to: localhost
Hi Tony,
Looking at the Documentation https://docs.ansible.com/ansible/latest/modules/vmware_host_module.html
- name: Add ESXi Host to vCenter
vmware_host:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter: datacenter_name
cluster: cluster_name
esxi_hostname: '{{ esxi_hostname }}'
esxi_username: '{{ esxi_username }}'
esxi_password: '{{ esxi_password }}'
state: present
delegate_to: localhost
This code will add the host to a Cluster,
Hi Tony,
There are two ways of adding an esxi host to vCenter:
In standalone mode (not part of a vCenter cluster)
Part of a vCenter cluster
See the following examples:
In standalone mode, first you’ll need a host folder to place your esxi hosts. Bear in mind here that vcenter_folder should be in the following form: /datacenter_name/host/name. For example if your dc is DC01:
/DC01/host/myhostfolder.
name: Create a host folder
vcenter_folder:
hostname: “{{ hostvars[inventory_hostname][‘vcenter’] }}”
username: “{{ vcenter_username | d(ansible_ssh_user) }}”
password: “{{ vcenter_password | d(ansible_ssh_pass) }}”
datacenter_name: “{{ datacenter_name }}”
folder_name: “{{ vcenter_folder }}”
folder_type: host
state: present
validate_certs: False
register: folder_result
delegate_to: localhost
run_once: True
retries: 10
until: folder_result is succeeded
name: Add host to vCenter (standalone)
vmware_host:
hostname: “{{ hostvars[inventory_hostname][‘vcenter’] }}”
username: “{{ vcenter_username | d(ansible_ssh_user) }}”
password: “{{ vcenter_password | d(ansible_ssh_pass) }}”
datacenter_name: “{{ datacenter_name }}”
folder: “{{ vcenter_folder }}”
esxi_hostname: “{{ hostvars[inventory_hostname][‘fqdn’] }}”
esxi_username: “{{ esxi_admin_username | d(‘root’) }}”
esxi_password: “{{ esxi_root_password | d(‘password’) }}”
state: present
validate_certs: False
delegate_to: localhost
register: host_result
retries: 10
until: host_result is succeeded
Next option is to add a esxi host into an existing cluster:
name: Attach esxi host to vCenter cluster
vmware_host:
hostname: “{{ hostvars[inventory_hostname][‘vcenter’] }}”
username: “{{ vcenter_username | d(ansible_ssh_user) }}”
password: “{{ vcenter_password | d(ansible_ssh_pass) }}”
datacenter_name: “{{ datacenter_name }}”
cluster_name: “{{ cluster_name }}”
esxi_hostname: “{{ hostvars[inventory_hostname][‘fqdn’] }}”
esxi_username: “{{ esxi_admin_username | d(‘root’) }}”
esxi_password: “{{ esxi_root_password | d(‘password’) }}”
state: present
validate_certs: False
delegate_to: localhost
Hope it helps,
Regards
Tony_Wong
(Tony Wong)
April 13, 2020, 7:47pm
4
Hi,
Thanks. what would the host be under ‘/DC01/host/myhostfolder’ ?
I have a parent folder that I need to add the host into but its not taking it.
can i specify a vcenter folder ?
Tony_Wong
(Tony Wong)
April 13, 2020, 7:58pm
5
how do i specify this folder? The is a vcenter folder and I need to add it to the HK datacenter
(attachments)
Best thing you can do to understand how vCenter is organized is using govc tool:
For example, this is my home lab:
➜ govc about
Name: VMware vCenter Server
Vendor: VMware, Inc.
Version: 6.7.0
Build: 15129973
OS type: linux-x64
API type: VirtualCenter
API version: 6.7.3
Product ID: vpx
UUID: 1a93cd0c-f7f7-4021-a232-7ca15c73f8bd
In this case the only esxi host belong to a datacenter called DC01, in cluster CLUSTER01, and esxi host is 192.168.10.200.
➜ govc host.info 192.168.10.200
Name: 192.168.10.200
Path: /DC01/host/CLUSTER01/192.168.10.200
Manufacturer: Dell Inc.
Logical CPUs: 4 CPUs @ 3408MHz
Processor type: Intel(R) Core™ i7-6700 CPU @ 3.40GHz
CPU usage: 698 MHz (5.1%)
Memory: 32628MB
Memory usage: 16856 MB (51.7%)
Boot time: 2020-03-21 16:00:52.643186 +0000 UTC
State: connected
Regards,
Tony_Wong
(Tony Wong)
April 13, 2020, 8:21pm
7
when i run that it gave me govc: please specify a datacenter
tony@ubuntu:~/ansible-vmware/ansible-add-esxi-host$ govc about
Name: VMware vCenter Server
Vendor: VMware, Inc.
Version: 6.7.0
Build: 15976728
OS type: linux-x64
API type: VirtualCenter
API version: 6.7.3
Product ID: vpx
UUID: 4f7ea0f6-72b5-4d7c-9119-755870852497
Tony_Wong
(Tony Wong)
April 14, 2020, 12:28am
8
I Keep getting folder not found. but govc shows the folder
Tony_Wong
(Tony Wong)
April 14, 2020, 12:49am
9
wow I finally got it
the folder should be “/Remote Offices/HK/Host”
(attachments)