Score:1

why ansible doesn't recognize the vcenter windows machines?

uz flag

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:
Score:2
in flag

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.

uz flag
First, I added ```--verbos``` to the playbook command, it just added this line: ```Using /ansible/ansible.cfg as config file``` to output before the ```PLAY``` lines everything else remain the same. In addition, I performed your suggestion - adding ```/vm/``` string - output and errors remained the same (as in the main post). More over, I don't understand why ```ansible_hostname``` work and ```guest_name``` doesn't...?
uz flag
I didn't solve question a but solve question b by setting ```folder: "/{{ datacenter_name }}/"```
in flag
This will not help you if you have more than one VM with the same name in different folders.
uz flag
another fact that I found: If I execute poweroff.yml task on my lab - the machine are turned-off successfully. When I execute poweron.yml task on my turned-off lab, I get ```fatal: [srraalabrst1.r10.local]: UNREACHABLE! => {"changed": false, "msg": "ntlm: HTTPConnectionPool(host='vm1.domain.com', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fcf6a6b6080>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true}```
in flag
Most probably you are trying the next task too fast after turning on the VM. You can configure ansible to retry this, but you should ask a new question about that.
uz flag
In addition by your help, I found with ```--verbos``` that the folder is really comes with ```/vm/```: ```"hw_folder": "/FOO/vm/PRODUCT/DOMAIN.COM/```. I set the vars.yml file with this path and the ```folder``` parameters with ```/vm/``` as well. Output is still remain the same. Will open a new question for that. Thanks @Gerald.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.