Get basic vmware module working - vmware_guest_find

427 views
Skip to first unread message

Ted

unread,
May 9, 2018, 10:52:01 AM5/9/18
to Ansible Project
Hi,

I'm having issues with getting basic VMware modules working. For example vmware_guest_find. I'm running the below from a RHEL 7 server.

Versions installed:
pyvmomi 6.7.0
ansible
2.5.2
vSphere vCenter
6.0



playbook:
---
- hosts: vms

  roles
:
     
- vmware-template

Role task:
---
# tasks file for vmware-template

- name: Find Guest's Folder using name
  vmware_guest_find:
   hostname: "{{ vmware_esxi.hostname }}"
   validate_certs: "{{ vmware_esxi.validate_certs }}"
   username: "{{ vmware_esxi.username }}"
   password: "{{ vmware_esxi.password }}"
   datacenter: "{{ vmware_esxi.datacenter }}"
   name: "{{ vm_hostname }}"
  delegate_to: localhost
  tags: findvm

Role vars:(sensitive info hashed out)

---
# vars file for vmware-template
vmware_esxi
:
  hostname
: #######
  validate_certs
: no
  username
: #######
  password
: ########
  datacenter
: #######
 
vm_hostname
:
   
- testvm01

Hosts file:
[local]
localhost

[vms]
testvm01



Playbook run: (sensitive info hashed out)

# ansible-playbook -i hosts vmware-template.yml --tags findvm -vvvv
ansible
-playbook 2.5.2
  config file
= /etc/ansible/ansible.cfg
  configured
module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python
module location = /usr/lib/python2.7/site-packages/ansible
  executable location
= /bin/ansible-playbook
  python version
= 2.7.5 (default, May  3 2017, 07:55:04) [GCC 4.8.5 20150623 (Red Hat 4.8.5-14)]
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
Set default localhost to localhost
Parsed /etc/ansible/plays/hosts inventory source with ini plugin
Trying secret FileVaultSecret(filename='########') for vault_id=default
Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/default.pyc
PLAYBOOK
: vmware-template.yml **************************************************************************************************************************************************************
1 plays in vmware-template.yml
PLAY
[vms] *********************************************************************************************************************************************************************************
META
: ran handlers
TASK
[vmware-template : Find Guest's Folder using name] ************************************************************************************************************************************
task path: /etc/ansible/roles/vmware-template/tasks/main.yml:52
Using module file /usr/lib/python2.7/site-packages/ansible/modules/cloud/vmware/vmware_guest_find.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<localhost> EXEC /bin/sh -c '
echo ~ && sleep 0'
<localhost> EXEC /bin/sh -c '
( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1525877042.27-257095638403837 `" && echo ansible-tmp-1525877042.27-257095638403837="` echo /root/.ansible/tmp/ansible-tmp-1525877042.27-257095638403837 `" ) && sleep 0'
<localhost> PUT /root/.ansible/tmp/ansible-local-24670bWH_Sz/tmptyQ4GF TO /root/.ansible/tmp/ansible-tmp-1525877042.27-257095638403837/vmware_guest_find.py
<localhost> EXEC /bin/sh -c '
chmod u+x /root/.ansible/tmp/ansible-tmp-1525877042.27-257095638403837/ /root/.ansible/tmp/ansible-tmp-1525877042.27-257095638403837/vmware_guest_find.py && sleep 0'
<localhost> EXEC /bin/sh -c '
/usr/bin/python /root/.ansible/tmp/ansible-tmp-1525877042.27-257095638403837/vmware_guest_find.py && sleep 0'
<localhost> EXEC /bin/sh -c '
rm -f -r /root/.ansible/tmp/ansible-tmp-1525877042.27-257095638403837/ > /dev/null 2>&1 && sleep 0'
fatal: [testvm01 -> localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "datacenter": "#######",
            "hostname": "###########",
            "name": "['
##########']",
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "port": ###,
            "username": "#####",
            "uuid": null,
            "validate_certs": false
        }
    },
    "msg": "Unable to find folders for virtual machine ['testvm01
']"
}
        to retry, use: --limit @/etc/ansible/plays/vmware-template.retry
PLAY RECAP *********************************************************************************************************************************************************************************
testvm01         : ok=0    changed=0    unreachable=0    failed=1  



This is a pretty straight forward task but I don't understand why its failing. The vm exists, its powered on, VMware tools is running. I can see a new connection opening under the sessions tab in the vSphere client. I can query the VM from powercli.........

Does pyvmomi have to be installed on the vCenter host?

Any suggestions / comments are most welcome!

Thanks,

Ted



Nick Rogers

unread,
May 9, 2018, 2:54:56 PM5/9/18
to Ansible Project
It looks like your problem is using the "vm_hostname" in the task.

In my experiments, I either use "{{ inventory_hostname }}" for a per vm/host/target definition in the hosts group or a "with_items" loop:

- name: Find Guest's Folder using name

  vmware_guest_find:
   hostname: "{{ vmware_esxi.hostname }}"
   validate_certs: "{{ vmware_esxi.validate_certs }}"
   username: "{{ vmware_esxi.username }}"
   password: "{{ vmware_esxi.password }}"
   datacenter: "{{ vmware_esxi.datacenter }}"
   name: "{{ item }}"
  delegate_to: localhost
  tags: findvm
  with_items:
    - "{{ vm_hostname }}"

Ted

unread,
May 10, 2018, 5:06:32 AM5/10/18
to Ansible Project
Hi Nick,

Thanks for the response. I'm actually basing my tasks on your github repo you shared!
So I've tried what you suggested but its not returning any data.

Updated task:

- name: Find Guest's Folder using name
  vmware_guest_find:
   hostname: "{{ vmware_esxi.hostname }}"
   validate_certs: "{{ vmware_esxi.validate_certs }}"
   username: "{{ vmware_esxi.username }}"
   password: "{{ vmware_esxi.password }}"
   datacenter: "{{ vmware_esxi.datacenter }}"
   name: "{{ item }}"
  delegate_to: localhost
  tags: findvm
  with_items:
   - "{{ vm_hostname }}"

Playbook run:

# ansible-playbook -i hosts vmware-template.yml --tags findvm  

PLAY
[vms] *********************************************************************************************************************************************************************************
TASK
[vmware-template : Find Guest's Folder using name] ************************************************************************************************************************************
ok: [testvm01 -> localhost]
PLAY RECAP *********************************************************************************************************************************************************************************
testvm01         : ok=1    changed=0    unreachable=0    failed=0  

The VM is definitely in a folder.
I can see the session opening in the VMware client so I'm assuming that its connecting and versions of the various software components are compatible.
Must be missing something simple

Regards,


Ted

Nick Rogers

unread,
May 10, 2018, 12:50:49 PM5/10/18
to Ansible Project
The play is running properly, but I'm not sure what your intent is for the output. Try running it with -vvv, or if you register a variable like 
- name: "Find Guest's Folder using name"
  vmware_guest_find
:
   hostname
: "{{ vmware_esxi.hostname }}"
   validate_certs
: "{{ vmware_esxi.validate_certs }}"
   username
: "{{ vmware_esxi.username }}"
   password
: "{{ vmware_esxi.password }}"
   datacenter
: "{{ vmware_esxi.datacenter }}"
   name
: "{{ item }}"
  delegate_to
: localhost

 
register: vm_folder

  tags
: findvm
  with_items
:
   
- "{{ vm_hostname }}"

- name: debug vm_folder
  debug
:
   
var: vm_folder


You will then see what it's doing and can move on from there.
Reply all
Reply to author
Forward
0 new messages