ansible add esxi host to datacenter or folder

210 views
Skip to first unread message

Tony Wong

unread,
Apr 13, 2020, 11:48:40 AM4/13/20
to Ansible Project
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
~




please help as vmware ansible seems very limited in info 

David Foley

unread,
Apr 13, 2020, 12:35:42 PM4/13/20
to Ansible Project
- 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, 

Jorge Rúa

unread,
Apr 13, 2020, 3:02:27 PM4/13/20
to ansible...@googlegroups.com
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

--

Jorge Rua


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7d76788c-cc3f-4530-80cd-626b4700cf1c%40googlegroups.com.

Tony Wong

unread,
Apr 13, 2020, 3:47:23 PM4/13/20
to Ansible Project
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

unread,
Apr 13, 2020, 3:59:04 PM4/13/20
to ansible...@googlegroups.com
how do i specify this folder? The is a vcenter folder and I need to add it to the HK datacenter

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
Screen Shot 2020-04-13 at 12.57.45 PM.png

Jorge Rúa

unread,
Apr 13, 2020, 4:03:41 PM4/13/20
to ansible...@googlegroups.com
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(TM) 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

unread,
Apr 13, 2020, 4:22:02 PM4/13/20
to ansible...@googlegroups.com
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

unread,
Apr 13, 2020, 8:28:21 PM4/13/20
to Ansible Project

Screen Shot 2020-04-13 at 5.26.03 PM.png


I Keep getting folder not found. but govc shows the folder

Tony Wong

unread,
Apr 13, 2020, 8:50:11 PM4/13/20
to ansible...@googlegroups.com
wow I finally got it

the folder should be "/Remote Offices/HK/Host"

On Mon, Apr 13, 2020 at 5:28 PM Tony Wong <tdub...@gmail.com> wrote:

Screen Shot 2020-04-13 at 5.26.03 PM.png


I Keep getting folder not found. but govc shows the folder

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Tony Wong

unread,
Apr 13, 2020, 8:50:21 PM4/13/20
to ansible...@googlegroups.com
thank you
Reply all
Reply to author
Forward
0 new messages