Ansible Pilot

Ansible troubleshooting - VMware Unknown error while connecting to vCenter or ESXi

Let’s troubleshoot together the Ansible fatal error “Unknown error while connecting to vCenter or ESXi API, [Errno -2] Name or service not known” to find the root cause, misspelled hostname in your Ansible Playbook, a connection problem connecting eventually using a secure VPN connection and successfully run our Ansible For VMware Playbook code.

May 17, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

Ansible troubleshooting - VMware Unknown error while connecting to vCenter or ESXi

Today we’re going to talk about Ansible troubleshooting, specifically about the “Unknown error while connecting to vCenter or ESXi API, [Errno -2] Name or service not known” message and enable Ansible For VMware.

This fatal error message happens when the Ansible controller is not able to connect to your VMware Infrastructure. The root cause might be a misspelled hostname in your Ansible Playbook, a connection problem connecting eventually using a secure VPN connection, or a configuration on the firewall on the target host.

I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

The Best Resources For Ansible

Certifications

Video Course

Printed Book

eBooks

demo

How to reproduce, troubleshoot, and fix the error:

“Unknown error while connecting to vCenter or ESXi API [Errno -2] Name or service not known”

In this demo, I’m going to reproduce the error and fix using the correct VMware hostname and verify the network configuration on a demo machine.

Let’s suppose our infrastructure is accessible at the hostname “vmware.example.com”. Later in this demo, we’re going to see the misspelled “vm-ware.example.com”.

error code

---
- name: info vm demo
  hosts: localhost
  become: false
  gather_facts: false
  collections:
    - community.vmware
  pre_tasks:
    - include_vars: vars.yml
  tasks:
    - name: get VM info
      vmware_guest_info:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        datacenter: "{{ vcenter_datacenter }}"
        validate_certs: "{{ vcenter_validate_certs }}"
        name: "{{ vm_name }}"
      register: detailed_vm_info

    - name: print VM info
      ansible.builtin.debug:
        var: detailed_vm_info
---
vcenter_hostname: "vm-ware.example.com"
vcenter_datacenter: "vmwaredatacenter"
vcenter_validate_certs: false
vcenter_username: "[email protected]"
vcenter_password: "MySecretPassword123"
vm_name: "myvm"
vcenter_destination_folder: "myvm"
vm_template: "mytemplate"
localhost

error execution

$ ansible-playbook vm_info.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [info vm demo] *******************************************************************************
TASK [include_vars] *******************************************************************************
ok: [localhost]
TASK [get VM info] ********************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Unknown error while connecting to vCenter or ESXi API at vm-ware.example.com:443 : [Errno -2] Name or service not known"}
PLAY RECAP ****************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

fix code

---
vcenter_hostname: "vmware.example.com"
vcenter_datacenter: "vmwaredatacenter"
vcenter_validate_certs: false
vcenter_username: "[email protected]"
vcenter_password: "MySecretPassword123"
vm_name: "myvm"
vcenter_destination_folder: "myvm"
vm_template: "mytemplate"

fix execution

$ ansible-playbook -i inventory vm_info.yml
PLAY [info vm demo] *******************************************************************************
TASK [include_vars] *******************************************************************************
ok: [localhost]
TASK [get VM info] ********************************************************************************
ok: [localhost]
TASK [print VM info] ******************************************************************************
ok: [localhost] => {
    "detailed_tag_info": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false,
        "failed": false,
        "instance": {
            "annotation": "",
            "current_snapshot": null,
            "customvalues": {},
            "guest_consolidation_needed": false,
            "guest_question": null,
            "guest_tools_status": "guestToolsNotRunning",
            "guest_tools_version": "0",
            "hw_cluster": "prod-cluster",
            "hw_cores_per_socket": 1,
            "hw_datastores": [
                "Datastore-1"
            ],
            "hw_esxi_host": "vmware.example.com",
            "hw_eth0": {
                "addresstype": "assigned",
                "ipaddresses": null,
                "label": "Network adapter 1",
                "macaddress": "00:50:56:a5:fd:4a",
                "macaddress_dash": "00-50-56-a5-fd-4a",
                "portgroup_key": null,
                "portgroup_portkey": null,
                "summary": "VM Network"
            },
            "hw_files": [
                "[Datastore-1] myvm/myvm.vmx",
                "[Datastore-1] myvm/myvm.vmsd",
                "[Datastore-1] myvm/myvm.vmdk"
            ],
            "hw_folder": "/vmwaredatacenter/vm/myvm",
            "hw_guest_full_name": null,
            "hw_guest_ha_state": null,
            "hw_guest_id": null,
            "hw_interfaces": [
                "eth0"
            ],
            "hw_is_template": false,
            "hw_memtotal_mb": 1024,
            "hw_name": "myvm",
            "hw_power_status": "poweredOff",
            "hw_processor_count": 1,
            "hw_product_uuid": "422549b9-7e76-fb2b-da34-e9c6c8b071de",
            "hw_version": "vmx-11",
            "instance_uuid": "5025d22d-cea7-4d1c-41f9-5cd80b9603dc",
            "ipv4": null,
            "ipv6": null,
            "module_hw": true,
            "moid": "vm-17923",
            "snapshots": [],
            "vimref": "vim.VirtualMachine:vm-17923",
            "vnc": {}
        }
    }
}
PLAY RECAP ****************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Recap

Now you know better how to troubleshoot the Ansible “VMware Unknown error while connecting to vCenter or ESXi” message and implement your Ansible For VMware automation.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases