ansible – Why Ansible Doesn’t Recognize the vCenter Windows Machines?

ansibleconfigurationdeploymentvirtualizationvmware-vcenter

I'm pretty new with Ansible so I might configured things wrong
[I have a Docker container running Ansible service in it
I have an Ansible repository that include the Ansible files (this is a .Git repository]

My will was to automatically revert each lab in vCenter server to a specific snapshot
So, I (with the help of ansible-roles-explained-with-examples guide):

  • Created a role with ansible-galaxy init command name vcenter (see directory tree below)
  • Created some vcenter tasks files inside tasks folder (see directory tree below). Here is an example of poweroff.yml task file:
- name: Set the state of a virtual machine to poweroff
  community.vmware.vmware_guest_powerstate:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    folder: "/{{ datacenter_name }}/{{ folder }}"
    # name: "{{ guest_name }}"
    name: "{{ ansible_hostname }}"
    validate_certs: no
    state: powered-off
  delegate_to: localhost
  register: deploy
  • Supplied vCenter credentials in vcenter\vars\main.yml file, like this:
# vars file for vcenter
vcenter_hostname: vcenter.foo.com
vcenter_username: [email protected]
vcenter_password: f#0$o#1$0o
datacenter_name: FOO_Fighters
# datastore_name: 
cluster_name: FOO
folder: '/FOO/PRODUCT/DOMAIN.COM/' 
  • Included the tasks in tasks\main.yml file with import-task key, like this:
---
# tasks file for roles/vcenter
- import_tasks: poweroff.yml
# - import_tasks: poweron.yml
# - import_tasks: revert.yml
# - import_tasks: shutdown.yml
  • Created a all.yml inside group_vars folder in inventories library (i don't know if its a professional way to do like that) that include all winrm details like this:
---
#WinRM Protocol Details
ansible_user: DOMAIN\user
ansible_password: f#0$o#1$0o
ansible_connection: winrm
ansible_port: 5985
ansible_winrm_scheme: http
ansible_winrm_server_cert_validation: ignore
ansible_winrm_transport: ntlm
ansible_winrm_read_timeout_sec: 60
ansible_winrm_operation_timeout_sec: 58
  • Created a revert_lab.yml playbook that include the role, like this
---
- name: revert an onpremis lab
  hosts: all
  roles:
  - vcenter

My ansible.cfg is like this:

[defaults]
inventory = /ansible/inventories
roles_path = ./roles:..~/ansible/roles

I executed the playbook to revert all the machines in the lab:

ansible-playbook playbooks/revert_vcenter_lab.yml -i inventories/test/onpremis/domain.com/lab_r.yml

The error I got was:

TASK [Gathering Facts] ****************************************************************************************************************************************************
[WARNING]: Error when collecting winrm facts: You cannot call a method on a null-valued expression.  At line:15 char:17  + ...
$ansibleFacts.ansible_win_rm_certificate_expires = $_.Not ...  +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      + CategoryInfo          :  
InvalidOperation: (:) [], RuntimeException      + FullyQualifiedErrorId : InvokeMethodOnNull      at <ScriptBlock>, <No file>: line 15  at <ScriptBlock>, <No file>: line  
13
ok: [vm1.domain.com]
ok: [vm2.domain.com]
ok: [vm3.domain.com]
ok: [vm4.domain.com]
ok: [vm5.domain.com]
ok: [vm6.domain.com]
ok: [vm7.domain.com]
ok: [vm8.domain.com]

TASK [vcenter : Set the state of a virtual machine to poweroff] ***********************************************************************************************************
fatal: [vm1.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM1'"}
fatal: [vm2.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM2'"}
fatal: [vm3.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM3'"}
fatal: [vm4.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM4'"}
fatal: [vm5.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM5'"}
fatal: [vm6.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM6'"}
fatal: [vm7.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM7'"}
fatal: [vm8.domain.com -> localhost]: FAILED! => {"changed": false, "msg": "Unable to set power state for non-existing virtual machine : 'VM8'"}

PLAY RECAP ****************************************************************************************************************************************************************
vm1.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
vm2.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
vm3.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
vm4.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
vm5.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
vm6.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
vm7.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
vm8.domain.com   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

a) How do I get rid of the Error when collecting winrm facts error? (It is look like that the playbook is not recognize the all.yml file with the win, but why?)
b) How do I fix the error "Unable to set power state for non-existing virtual machine…"? (We can see that the playbook access to the machines by fqdns mentioned in the lab_r.yml file (from the inventories library) but the error relates to the machine name as displayed in the vCenter platform…)

My repository:

C:.
├───ansible
│   │   ansible.cfg
│   ├───inventories
│   │   └───test
│   │       ├───cloud
│   │       └───onpremis
│   │           └───domain.com
│   │               │   lab_j.yml
│   │               │   lab_r.yml
│   │               └───group_vars
│   │                       all.yml
│   ├───playbooks
│   │       revert_lab.yml
│   └───roles
│       └───vcenter
│           ├───tasks
│           │       main.yml
│           │       poweroff.yml
│           │       poweron.yml
│           │       revert.yml
│           │       shutdown.yml
│           └───vars
│                   main.yml

My inventory lab_r.yml – this is a partial schema

---
all:
  children:
    root:
      children:
        center:
          children:
            appservers:
              hosts:
                vm1.domain.com:
            qservers:
              hosts:
                vm2.domain.com:
            dbservers:
              hosts:
                vm3.domain.com:

Best Answer

It's not very obvious from the documentation, but the string /vm/ is missing in your folder path.

- name: Set the state of a virtual machine to poweroff
  community.vmware.vmware_guest_powerstate:
    folder: "/{{ datacenter_name }}/vm/{{ folder }}"
    name: "{{ ansible_hostname }}"

I guess it is needed to distinguish between other resources in the datacenter, datastores, hosts, etc.