community.vmware icon indicating copy to clipboard operation
community.vmware copied to clipboard

Unable to create snapshot

Open impsik opened this issue 2 years ago • 3 comments

SUMMARY

I had working playbook to find guest and make snapshot of it. At some point it's not working anymore

ISSUE TYPE
  • Bug Report
COMPONENT NAME

vmware_guest_find vmware_guest_snapshot community.vmware.vmware_guest_find community.vmware.vmware_guest_snapshot

ANSIBLE VERSION
ansible [core 2.11.12] 
  config file = /home/ansible/.ansible.cfg
  configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/ansible/.local/lib/python3.6/site-packages/ansible
  ansible collection location = /home/ansible/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.6.8 (default, Nov 16 2020, 16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
  jinja version = 3.0.3
  libyaml = True
COLLECTION VERSION
community.vmware 2.7.0
CONFIGURATION
COMMAND_WARNINGS(/home/ansible/.ansible.cfg) = True
DEFAULT_HOST_LIST(/home/ansible/.ansible.cfg) = ['/home/ansible/ansible_playbooks/ansible_hosts']
DEFAULT_LOG_PATH(/home/ansible/.ansible.cfg) = /home/ansible/ansible.log
DEPRECATION_WARNINGS(/home/ansible/.ansible.cfg) = False
HOST_KEY_CHECKING(/home/ansible/.ansible.cfg) = False
OS / ENVIRONMENT
STEPS TO REPRODUCE
  - name: "Find Guest's Folder using name"
    vmware_guest_find:
      hostname: "{{ vmware }}"
      username: "{{ username}}"
      password: "{{ password }}"
      validate_certs: no
      name: "{{ item }}"
    delegate_to: localhost
    ignore_errors: yes
    with_lines: 
      cat {{ servers }} | grep -v "^#"
    register: vm_folder

  - name: "Create snapshot"
    vmware_guest_snapshot:
      hostname: "{{ vmware }}"
      validate_certs: no
      username: "{{ username }}"
      password: "{{ password }}"
      datacenter: datastore1
      folder: "{{ vm_folder}}['folders'] }}"
      name: "{{ item }}"
      state: present
      quiesce: True
      snapshot_name: "{{ item }}-snapshot-{{ ansible_date_time.date }}-{{ ansible_date_time.time }}"
      description: "Made with ansible"
    delegate_to: localhost
    ignore_errors: yes
    with_lines: 
      cat {{ servers }} | grep -v "^#"
EXPECTED RESULTS

Find guest and make snapshot

ACTUAL RESULTS
failed: [localhost -> localhost] (item=REMOVED) => {
    "ansible_loop_var": "item",
    "changed": false,
    "invocation": {
        "module_args": {
            "datacenter": "datastore1",
            "description": "Snapshot descriptiopn",
            "folder": "{'results': [{'folders': ['/Devel/vm/Discovered virtual machine'], 'invocation': {'module_args': {'hostname': 'REMOVED', 'username': 'REMOVED', 'password': 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER', 'validate_certs': False, 'name': 'REMOVED', 'port': 443, 'use_instance_uuid': False, 'proxy_host': None, 'proxy_port': None, 'uuid': None}}, 'failed': False, 'changed': False, 'item': 'REMOVED', 'ansible_loop_var': 'item'}], 'skipped': False, 'msg': 'All items completed', 'changed': False}['folders'] }}",
            "hostname": "REMOVED",
            "memory_dump": false,
            "moid": null,
            "name": "VM name removed",
            "name_match": "first",
            "new_description": null,
            "new_snapshot_name": null,
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "port": 443,
            "proxy_host": null,
            "proxy_port": null,
            "quiesce": true,
            "remove_children": false,
            "snapshot_name": "REMOVED",
            "state": "present",
            "use_instance_uuid": false,
            "username": "REMOVED",
            "uuid": null,
            "validate_certs": false
        }
    },
    "item": "ReMOVED",
    "msg": "Unable to manage snapshots for non-existing VM ReMOVED"
}
...ignorin

Vcenter is latest, 7.0.3 3U

My latest try:

  - name: "Find Guest's Folder using name"
    community.vmware.vmware_guest_find:
      hostname: "{{ vmware }}"
      username: "{{ username}}"
      password: "{{ password }}"
      validate_certs: no
      name: "{{ item }}"
    delegate_to: localhost
    ignore_errors: yes
    with_lines: 
      cat {{ servers }} | grep -v "^#"
    register: vm_folder

   - debug:
       msg: "{{ vm_folder['results'][0]['folders'] }}"
     # ^ this returns right path

  - name: "Create snapshot"
    community.vmware.vmware_guest_snapshot:
      hostname: "{{ vmware }}"
      validate_certs: no
      username: "{{ username }}"
      password: "{{ password }}"
      datacenter: "{{ datacenter }}"
      folder: "{{ vm_folder['results'][0]['folders'] }}"
      folder: "{{ vm_folder}}['folders'] }}"
      name: "{{ item }}"
      state: present
      quiesce: True
      snapshot_name: "{{ item }}-snapshot-{{ ansible_date_time.date }}-{{ ansible_date_time.time }}"
      description: "Ansible tehtud automaatne snapshot {{ item }} masinast enne uuendust"
    delegate_to: localhost
    ignore_errors: yes
    with_lines: 
      cat {{ servers }} | grep -v "^#"

impsik avatar Jul 25 '22 12:07 impsik

I'm not really sure if your playbook would work correctly. You're trying to snapshot several VMs, right? However, I think that vm_folder will be set to one folder only (the folder of the VM vmware_guest_find last discovered). So if the VMs in server are in different folders, you will run into problems.

Why do you use such an unusual way, anyway? To define the CIs you want ansible to change, use the inventory.

   - debug:
       msg: "{{ vm_folder['results'][0]['folders'] }}"
     # ^ this returns right path

That's weird. I get 'dict object' has no attribute 'results' when I try this.

      folder: "{{ vm_folder['results'][0]['folders'] }}"
      folder: "{{ vm_folder}}['folders'] }}"

There's one folder too much.

      folder: "{{ vm_folder}}['folders'][0] }}"

This worked for me.

mariolenz avatar Jul 26 '22 05:07 mariolenz

@mariolenz Yes, i read VM names from a file. At the moment i have only 1 VM listed in file, for testing. That debug msg returns:

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "/Devel/vm/Discovered virtual machine"
    ]
}

That's correct folder for that VM. When i try to find 3 VM's from 3 different folders then community.vmware.vmware_guest_find module finds it correctly

folder: "{{ vm_folder}}['folders'][0] }}"

This gives me:

TASK [Create snapshot] ********************************************************************************************************************************************************************************************
failed: [localhost -> localhost] (item=REMOVED) => {"ansible_loop_var": "item", "changed": false, "item": "REMOVED", "msg": "Unable to manage snapshots for non-existing VM REMOVED"}
...ignoring

impsik avatar Jul 26 '22 07:07 impsik

@mariolenz You are absolutely right, folder: "{{ vm_folder}}['folders'][0] }}" works. I did rewrite my playbook and now it works. Thanks for suggesting to use inventory file. I was reading VM names from a file.

impsik avatar Jul 26 '22 11:07 impsik