AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible troubleshooting - VMware Unknown error while connecting to vCenter or ESXi — Video Tutorial
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.
What You'll Learn
- Ansible troubleshooting - VMware Unknown error while connecting to vCenter or ESXi
- error code
- error execution
- fix code
- fix execution
- Conclusion
- Related Articles
Full Tutorial Content
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.
## Playbook
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 Playbook, 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 Playbook, we’re going to see the misspelled “vm-ware.example.com”.
error code
- vm_info.yml
```yaml
---
- name: info vm Playbook
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
```
- vars.yml
```yaml
---
vcenter_hostname: "vm-ware.example.com"
vcenter_datacenter: "vmwaredatacenter"
vcenter_validate_certs: false
vcenter_username: "username@vsphere.local"
vcenter_password: "MySecretPassword123"
vm_name: "myvm"
vcenter_destination_folder: "myvm"
vm_template: "mytemplate"
```
- inventory
```ini
localhost
```
error execution
```bash
$ 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 Playbook] *******************************************************************************
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 ski
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 3 min
- Category: troubleshooting
Read the full written article: Ansible troubleshooting - VMware Unknown error while connecting to vCenter or ESXi