Ansible – Vmware guest – No module named requests

Question:

I’m playing around with ansible, I came over the vmware guest module which allows snapshot operation. However, I’m facing some issues. My default python interpreter is Python 3.6.9 i.e if I enter python in my Ubuntu 18.04 machine, Python 3.6.9 is started. Now to the problem I’m currently facing. My playbook is:

- name: Snapshot creation on esx-1.damn.li
  hosts: host

  tasks:
  - name: Create snapshot on fw-pfsense-1
    vmware_guest_snapshot:
      hostname: "host"
      datacenter: "esx01.home"
      username: "root"
      validate_certs: no
      password: "dontguess"
      name: "fw-pfsense-1"
      state: "present"
      snapshot_name: "ansible_test"
      folder: "/vmfs/volumes/59714072-384cd3f4-9503-001fc69c0e8f/fw-pfsense-1-2"
      description: "Created by ansible"

I execute the playbook with:

ansible-playbook esx-1-create-snapshots.yaml -i ../inventory/hosts.yaml --ask-vault-pass

And ansible throws:

The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_vmware_guest_snapshot_payload_gipss3wl/ansible_vmware_guest_snapshot_payload.zip/ansible/module_utils/vmware.py", line 24, in <module>
    import requests
ImportError: No module named 'requests'

However, if I start python manually and import requests or the necessary module pyVmomi, I can import it.

Asked By: newduino

||

Answers:

From the vmware_guest_snapshot module documentation

Requirements

The below requirements are needed on the host that executes this module.

  • python >= 2.6
  • PyVmomi

PyVmomi currently has as a dependency requests>=2.3.0

All the info you have given about python in your question is related to what you are using on the controller (i.e. the machine running the playbook). You need to make sure that pyVmomi is installed on the target in the version of python discovered by ansible when connecting from the controller.

The easiest way to make sure the requirements are met before you try to use the module is to add the following task in your playbook:

- name: Make sure requirements are met to run vmware_guest_snapshot module
  become: true
  pip:
    name: PyVmomi
    state: present
Answered By: Zeitounator

Actually, the default Ansible will execute the task on the remote host, when you need to execute on the manager host locally you should add delegate_to: localhost

Since pyVmomi use API to control the vSphere cluster, you should run the task locally on the Ansible manager. So your task should be:

- name: Snapshot creation on esx-1.damn.li
  hosts: host

  tasks:
  - name: Create snapshot on fw-pfsense-1
    vmware_guest_snapshot:
      hostname: "host"
      datacenter: "esx01.home"
      username: "root"
      validate_certs: no
      password: "dontguess"
      name: "fw-pfsense-1"
      state: "present"
      snapshot_name: "ansible_test"
      folder: "/vmfs/volumes/59714072-384cd3f4-9503-001fc69c0e8f/fw-pfsense-1-2"
      description: "Created by ansible"
    delegate_to: localhost
Answered By: Nur Faizin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.