connect to multiple vcenters and esxi hosts without host file

53 views
Skip to first unread message

Tony Wong

unread,
May 4, 2023, 12:26:29 PM5/4/23
to Ansible Project
I got this pb but it is only working for single vcenter and I have to put the esxi hosts into a host file.

Is it possible to connect to multiple vcenters and lookup all the esxi hosts and connect to them instead of using host file?

vars.yml

vcenter_hostname: vcenter1.domain.com
hostname: "{{ inventory_hostname }}"


vcenter_creds.yml has my credentials and I run the pb with --ask-vault-pass

---

hostsall

  gather_factstrue

  connectionlocal

  vars_files:

    - vcenter_creds.yml

    - vars.yml

 

  tasks:

 

  - nameGather vmware host facts from vCenter

    community.vmware.vmware_host_facts:

      hostname"{{ vcenter_hostname }}"

      username"{{ vcenter_username }}"

      password"{{ vcenter_password }}"

      esxi_hostname"{{ hostname }}"

      validate_certsno

    registerhost_facts

    delegate_tolocalhost

Tony Wong

unread,
May 4, 2023, 1:43:55 PM5/4/23
to Ansible Project
Here is something I tried

---

nameEnable SSH on ESXi Hosts

  hostslocalhost

  connectionlocal

  vars_files:

    - vars2.yml

    - vcenter_creds.yml

 

  tasks:

    - nameConnect to vCenter servers and retrieve ESXi host information

      vmware_host:

        hostname"{{ item }}"

        username"{{ vcenter_username }}"

        password"{{ vcenter_password }}"

        validate_certsno

        gather_vm_hostsyes

        gather_nic_infoyes

        gather_vswitch_infoyes

      registervmware_info

      with_items"{{ vcenter_servers }}"



in my vars.yml file

vcenter_servers:

  - vcenter1.domain.com

  - vcenter2.domain.com



But I got error

TASK [Connect to vCenter servers and retrieve ESXi host information] *********************************************************************************************

failed: [localhost] (item=) => {"ansible_loop_var": "item", "changed": false, "item": "", "msg": "missing required arguments: datacenter_name, esxi_hostname"}

failed: [localhost] (item=) => {"ansible_loop_var": "item", "changed": false, "item": ""msg": "missing required arguments: datacenter_name, esxi_hostname"}

Ravi yadav

unread,
May 5, 2023, 3:18:11 AM5/5/23
to Ansible Project
please use vmware_vm_facts: module, it will collect all info connected to vmware, from there you can filter ESX names.

it will collect for all vcenter mentioned in your var2.yml file.

Ravi
Reply all
Reply to author
Forward
0 new messages