# Toughts on playbook to create snapshot on vcenter before ansible exuction playbook

**URL:** <https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160>\
**Category:** Get Help\
**Tags:** vmware, awx\
**Created:** [October 10, 2024, 1:29pm UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160 "2024-10-10T13:29:47Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![alainseys](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alainseys/32/2757_2.png) [@alainseys](https://forum.ansible.com/u/alainseys)\
**Post date:** [October 10, 2024, 1:29pm UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160/1 "2024-10-10T13:29:47Z")

</div>

Hello Guys,

Can you help me out with a issue i am facing , i want to create a snapshot before “ansible” is changing somthing on a server.

I have the creation of snapshot this works as expected but if i combine it with a other playbook this returns in invalid credenitals.

i have tried adding the password in the vault , in awx always the same error. My touthts are that create snapshot is targeting locahost and the playbook is targetting a specefic host.

Could this be the issue?  
Any other toughts to handle this?

```auto
---
# tasks file for createsnapshot
- name: Create Snapshot
  community.vmware.vmware_guest_snapshot:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    datacenter: "{{ vcenter_datacenter }}"
    validate_certs: false
    folder: "{{ vcenter_vm_folder }}"
    name: "{{ vm_name | default(inventory_hostname) }}"
    snapshot_name: "{{ vm_snapshot_name | default('Before execution ansible playbook') }}"
    description: "{{ vm_snapshot_name | default('Before execution ansible playbook') }}"
    state: present
    memory_dump: true
  delegate_to: localhost

```

 ![image](https://us1.discourse-cdn.com/ansible/original/2X/a/a7cf933d116dde747fc688e3b227f7e054d5753e.png)

```auto
- name: Deploy docker
  hosts: TEST_ONLY
  become: true
  vars_files:
    - ../vault.yml
  tasks:
    - name: Create snapshot
      ansible.builtin.import_role:
        name: vcenter/create-snapshot
      delegate_to: localhost

    - name: Install Docker
      ansible.builtin.import_role:
        name: linux/docker

```

the error i recieve

```auto
{
  "module_stdout": "",
  "module_stderr": "\nsu: Authentication failure\n",
  "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
  "rc": 1,
  "_ansible_no_log": false,
  "changed": false,
  "_ansible_delegated_vars": {
    "ansible_host": "localhost",
    "ansible_port": null,
    "ansible_user": "ansible",
    "ansible_connection": "local"
  }
}

```

---

<div class="post-metadata">

**Author:** ![disi](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/disi/32/1138_2.png) [@disi](https://forum.ansible.com/u/disi)\
**Post date:** [October 10, 2024, 3:00pm UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160/2 "2024-10-10T15:00:06Z")

</div>

Ah sorry I didn’t read it correctly, move the “become: true” further down maybe, to the task where it is needed?

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [October 10, 2024, 11:56pm UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160/3 "2024-10-10T23:56:23Z")

</div>

I think `delegate_to: localhost` in your first task is extra and should be removed:

```auto
- name: Deploy docker
  hosts: TEST_ONLY
  become: true
  vars_files:
    - ../vault.yml
  tasks:
    - name: Create snapshot
      ansible.builtin.import_role:
        name: vcenter/create-snapshot
      delegate_to: localhost <-- remove

    - name: Install Docker
      ansible.builtin.import_role:
        name: linux/docker

```

You already have `delegate_to: localhost` in your role task and that should be enough.

---

<div class="post-metadata">

**Author:** ![alainseys](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alainseys/32/2757_2.png) [@alainseys](https://forum.ansible.com/u/alainseys)\
**Post date:** [October 11, 2024, 7:20am UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160/4 "2024-10-11T07:20:35Z")

</div>

Hello, @bvitnik ,

I have tried this before did not resolve my issue same permission error.

> [@bvitnik](#):
>
> `ost` in your first task is extra and should be remov

---

<div class="post-metadata">

**Author:** ![alainseys](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alainseys/32/2757_2.png) [@alainseys](https://forum.ansible.com/u/alainseys)\
**Post date:** [October 11, 2024, 7:22am UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160/5 "2024-10-11T07:22:49Z")

</div>

@disi removed the become since this is not required in the playbook the roles themself have the required steps if called for.

But did not change anything.

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [October 11, 2024, 4:38pm UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160/6 "2024-10-11T16:38:37Z")

</div>

Are you sure the error message is exactly the same when you remove `delegate_to: localhost`? Can you post your inventory file (if any) and command line you are using to run Ansible? Can you also tell us which exact task fails?

P.S. Ah. I see now that you are using `import_role` instead of `include_role` so `delegate_to` should have no effect here. I thought it was messing with the include. You can ignore the first question.

---

<div class="post-metadata">

**Author:** ![mikemorency](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mikemorency/32/2672_2.png) [@mikemorency](https://forum.ansible.com/u/mikemorency)\
**Post date:** [October 11, 2024, 6:36pm UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160/7 "2024-10-11T18:36:30Z")

</div>

does the ‘ansible’ user exist on the localhost? It definitely seems like an error with `become` or connection vars.  
Try setting this on your vmware task

```auto
delegate_to: localhost
vars:
  ansible_become: false

```

What version of ansible is this? `_ansible_` vars were removed in like 2.8

---

<div class="post-metadata">

**Author:** ![alainseys](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alainseys/32/2757_2.png) [@alainseys](https://forum.ansible.com/u/alainseys)\
**Post date:** [October 11, 2024, 6:37pm UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160/8 "2024-10-11T18:37:10Z")

</div>

> [@bvitnik](#):
>
> ) and command line you are using to run

this is been run using AWX the inventory is simply containing 2 host

 ![image](https://us1.discourse-cdn.com/ansible/original/2X/9/97eba227a592b2b9bbd28d4ec6ebbc5665fffada.png)

 ![image](https://us1.discourse-cdn.com/ansible/original/2X/d/d11aaf33453ada1c65c8f40152f4e3fd3e075fb1.png)

 ![image](https://us1.discourse-cdn.com/ansible/original/2X/8/808ad08c5c5fa010bb08e268c1d0c6202381f5c7.png)

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [October 11, 2024, 7:09pm UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160/9 "2024-10-11T19:09:00Z")

</div>

Where is the “TEST\_ONLY” host (or group?) defined? You are using it here:

> [@bvitnik](#):
>
> ```auto
> - name: Deploy docker
> hosts: TEST_ONLY <---- here
> become: true
> vars_files:
> - ../vault.yml
> 
> ```

But I don’t see it on your screenshots. What I see on your screenshots are:

- An AWX inventory called “TEST ONLY” (under the hood it’s an inventory file)
- A host called “TEST\_HOST” specified inside the inventory from the previous point

The `hosts:` key in playbook accepts names of hosts and groups, not names of inventories if that is what you had in mind.

So either do something like this:

```auto
- name: Deploy docker
  hosts: all

```

to target all of the hosts, both “ANSIBLE-DEPLOY” and “TEST\_HOST” in your “TEST ONLY” inventory or like this:

```auto
- name: Deploy docker
  hosts: TEST_HOST

```

if you want to target only the host “TEST\_HOST” from your “TEST ONLY” inventory.

Just a note that underscore “\_” is an invalid (non allowed) character for hostname. You should avoid it.

---

<div class="post-metadata">

**Author:** ![alainseys](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alainseys/32/2757_2.png) [@alainseys](https://forum.ansible.com/u/alainseys)\
**Post date:** [October 13, 2024, 5:42pm UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160/10 "2024-10-13T17:42:11Z")

</div>

let me clairify it a bit more

the creation of the snapshot should been taken on the localhost (Execution Enviroment that then talks to the vcenter to create the snap).

Then the installation of docker should be done on the hosts that are in TEST ONLY. Without that i am adjusting the modules so the modules keep repeatable

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [October 13, 2024, 10:17pm UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160/11 "2024-10-13T22:17:24Z")

</div>

`vmware_guest_snapshot` module is always used with `delegate_to: localhost`. That’s perfectly understandable and that’s how it was designed to work.

On the other hand, my comments are still holding. Also, the question “Can you also tell us which exact task fails?” is unfortunately left unanswered.

---

<div class="post-metadata">

**Author:** ![alainseys](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alainseys/32/2757_2.png) [@alainseys](https://forum.ansible.com/u/alainseys)\
**Post date:** [October 14, 2024, 8:25am UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160/12 "2024-10-14T08:25:50Z")

</div>

Hy,

i have simpelfied the playbook and put the creation of the snapshot first in line and there it is failing. My gues because it is set to all.

And the creation of the snapshot relies on localhost (what is not in the hostgroup).

so the failing task is creating the snapshot (if i run the task seperate targetting locahost direct this works).

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [October 14, 2024, 10:20am UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160/13 "2024-10-14T10:20:11Z")

</div>

I’m not sure I follow 🙂. Examples of the code that works and examples that does not work would be helpful.

---

<div class="post-metadata">

**Author:** ![alainseys](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alainseys/32/2757_2.png) [@alainseys](https://forum.ansible.com/u/alainseys)\
**Post date:** [October 14, 2024, 10:33am UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160/14 "2024-10-14T10:33:10Z")

</div>

I have found a workable solution instead of importing the roles i include them with a small adjustment on role (become on role level instead of playbook).

This now works as expected, if run the playbook this creates the snapshot of the vm updates the message of the snapshot

 ![image](https://us1.discourse-cdn.com/ansible/original/2X/9/95adbb310d2b2b22913a2b963893f6fcd35f805f.png)

 ![image](https://us1.discourse-cdn.com/ansible/original/2X/7/7056c9246b6d69563515f960319d0dc2c77d9dd1.png)

```auto
- name: Deploy docker
  hosts: all
  tasks:
    - name: Create snapshot vm
      ansible.builtin.include_role:
        name: vcenter/create-snapshot
      vars:
        vm_name: "{{ inventory_hostname }}"
        vm_snapshot_name: "Before execution ansible playbook - Installation docker"

    - name: Install Docker
      ansible.builtin.include_role:
        name: linux/docker

```

@bvitnik thanks for the help

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 13, 2024, 10:33am UTC](https://forum.ansible.com/t/toughts-on-playbook-to-create-snapshot-on-vcenter-before-ansible-exuction-playbook/10160/15 "2024-11-13T10:33:29Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
