# ansible add esxi host to datacenter or folder

**URL:** <https://forum.ansible.com/t/ansible-add-esxi-host-to-datacenter-or-folder/32673>\
**Category:** Ansible Project\
**Created:** [April 13, 2020, 3:48pm UTC](https://forum.ansible.com/t/ansible-add-esxi-host-to-datacenter-or-folder/32673 "2020-04-13T15:48:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [April 13, 2020, 3:48pm UTC](https://forum.ansible.com/t/ansible-add-esxi-host-to-datacenter-or-folder/32673/1 "2020-04-13T15:48:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [April 13, 2020, 4:35pm UTC](https://forum.ansible.com/t/ansible-add-esxi-host-to-datacenter-or-folder/32673/2 "2020-04-13T16:35:42Z")

</div>

Hi Tony,

Looking at the Documentation [https://docs.ansible.com/ansible/latest/modules/vmware\_host\_module.html](https://docs.ansible.com/ansible/latest/modules/vmware_host_module.html)

```auto
- 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

```

```auto
This code will add the host to a Cluster, 

```

---

<div class="post-metadata">

**Author:** ![Jorge\_Rua1](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jorge\_Rua1](https://forum.ansible.com/u/Jorge_Rua1)\
**Post date:** [April 13, 2020, 7:01pm UTC](https://forum.ansible.com/t/ansible-add-esxi-host-to-datacenter-or-folder/32673/3 "2020-04-13T19:01:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [April 13, 2020, 7:47pm UTC](https://forum.ansible.com/t/ansible-add-esxi-host-to-datacenter-or-folder/32673/4 "2020-04-13T19:47:23Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [April 13, 2020, 7:58pm UTC](https://forum.ansible.com/t/ansible-add-esxi-host-to-datacenter-or-folder/32673/5 "2020-04-13T19:58:36Z")

</div>

how do i specify this folder? The is a vcenter folder and I need to add it to the HK datacenter

> **(attachments)**
>
> ![Screen Shot 2020-04-13 at 12.57.45 PM.png](https://us1.discourse-cdn.com/ansible/original/2X/b/bc864511d3a2764d22bbf4522c1aca377b57f17b.png)

---

<div class="post-metadata">

**Author:** ![Jorge\_Rua1](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jorge\_Rua1](https://forum.ansible.com/u/Jorge_Rua1)\
**Post date:** [April 13, 2020, 8:03pm UTC](https://forum.ansible.com/t/ansible-add-esxi-host-to-datacenter-or-folder/32673/6 "2020-04-13T20:03:07Z")

</div>

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](http://host.info) 192.168.10.200  
Name: 192.168.10.200  
Path: **/DC01/host/CLUSTER01/[192.168.10.200](http://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,

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [April 13, 2020, 8:21pm UTC](https://forum.ansible.com/t/ansible-add-esxi-host-to-datacenter-or-folder/32673/7 "2020-04-13T20:21:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [April 14, 2020, 12:28am UTC](https://forum.ansible.com/t/ansible-add-esxi-host-to-datacenter-or-folder/32673/8 "2020-04-14T00:28:21Z")

</div>

![Screen Shot 2020-04-13 at 5.26.03 PM.png](https://us1.discourse-cdn.com/ansible/original/2X/9/9a166875ac0edb59e3d6b5c503f2e32ed3e2d049.png)

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

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [April 14, 2020, 12:49am UTC](https://forum.ansible.com/t/ansible-add-esxi-host-to-datacenter-or-folder/32673/9 "2020-04-14T00:49:41Z")

</div>

wow I finally got it

the folder should be “/Remote Offices/HK/Host”

> **(attachments)**
>
> ![Screen Shot 2020-04-13 at 5.26.03 PM.png](https://us1.discourse-cdn.com/ansible/original/2X/9/9a166875ac0edb59e3d6b5c503f2e32ed3e2d049.png)

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [April 14, 2020, 12:49am UTC](https://forum.ansible.com/t/ansible-add-esxi-host-to-datacenter-or-folder/32673/10 "2020-04-14T00:49:52Z")

</div>

thank you

> **(attachments)**
>
> ![Screen Shot 2020-04-13 at 5.26.03 PM.png](https://us1.discourse-cdn.com/ansible/original/2X/9/9a166875ac0edb59e3d6b5c503f2e32ed3e2d049.png)
