# This topic needs a title

**URL:** <https://forum.ansible.com/t/this-topic-needs-a-title/38861>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [September 4, 2024, 2:37am UTC](https://forum.ansible.com/t/this-topic-needs-a-title/38861 "2024-09-04T02:37:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [September 4, 2024, 2:37am UTC](https://forum.ansible.com/t/this-topic-needs-a-title/38861/1 "2024-09-04T02:37:21Z")

</div>

Hi,

I’ve been trying to solve this but I truly still have lots to learn… Could somebody help?

Given that a VM “creation\_time” snapshot property has this format: “2024-05-26T10:01:21.219280+00:00”.

How could I build a filter for the “community.vmware.vmware\_all\_snapshots\_info” module that would give me VMs whose snapshots were created 2 weeks ago or older?

- name: Collect snapshots older than 2 weeks  
community.vmware.vmware\_all\_snapshots\_info:  
datacenter: “{{ datacenter\_name }}”  
validate\_certs: false  
filters:  
creation\_time: “”  
match\_type: includes  
register: old\_snapshots

Thanks…

Alex

_The contents of this message and any attachment(s) are confidential, proprietary to the City of Edmonton, and are intended only for the addressed recipient. If you have received this in error, please disregard the contents, inform the sender of the misdirection, and remove it from your system. The copying, dissemination, or distribution of this message, if misdirected, is strictly prohibited._

---

<div class="post-metadata">

**Author:** ![mahespth](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mahespth/32/252_2.png) [@mahespth](https://forum.ansible.com/u/mahespth)\
**Post date:** [September 4, 2024, 7:10am UTC](https://forum.ansible.com/t/this-topic-needs-a-title/38861/2 "2024-09-04T07:10:45Z")

</div>

Alex,

Does this wok for you as i’ve previously used something similar.

Regards

- name: Gather VMs with snapshots older than 2 weeks  
hosts: localhost  
gather\_facts: no  
tasks:

- name: Get all snapshots info  
community.vmware.vmware\_all\_snapshots\_info:  
hostname: “{{ vcenter\_hostname }}”  
username: “{{ vcenter\_username }}”  
password: “{{ vcenter\_password }}”  
validate\_certs: no  
register: snapshots\_info

- name: Filter snapshots older than 2 weeks  
set\_fact:  
old\_snapshots: “{{ snapshots\_info.virtual\_machines | selectattr(‘snapshots’, ‘defined’) | selectattr(‘snapshots’, ‘select’, ‘creation\_time \<= old\_date’) }}”  
vars:  
old\_date: “{{ (now() - timedelta(weeks=2)).strftime(‘%Y-%m-%dT%H:%M:%S’) }}”

- name: Display VMs with snapshots older than 2 weeks  
debug:  
var: old\_snapshots

---

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [September 4, 2024, 8:05pm UTC](https://forum.ansible.com/t/this-topic-needs-a-title/38861/3 "2024-09-04T20:05:48Z")

</div>

Hi Stephen,

First of all, thanks for helping…  
(Long read ahead, sorry.)

I’m afraid that did not work for me.

I’m collecting all the snapshots using “community.vmware.vmware\_all\_snapshots\_info”:

- name: Collect snapshots older than 2 weeks  
community.vmware.vmware\_all\_snapshots\_info:  
datacenter: “{{ datacenter\_name }}”  
validate\_certs: false  
register: snapshots\_info

Which does not return “snapshots\_info.virtual\_machines”. Instead, it returns “snapshots\_info.vmware\_all\_snapshots\_info” So, I had to adjust your suggestion to:

- name: Filter snapshots older than 2 weeks  
ansible.builtin.set\_fact:  
old\_snapshots: “{{ snapshots\_info.vmware\_all\_snapshots\_info | selectattr(‘vm\_name’, ‘defined’) | selectattr(‘vm\_name’, ‘select’, ‘creation\_time \<= old\_date’) }}”  
vars:  
old\_date: “{{ (now() - timedelta(weeks=2)).strftime(‘%Y-%m-%dT%H:%M:%S’) }}”

But that gave me this:  
TASK [Filter snapshots older than 2 weeks] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: line 0  
fatal: [localhost]: FAILED! =\> {“changed”: false}

So, as a test, I tried to define “old\_date” separately using:

- name: Set 2 weeks ago date  
ansible.builtin.set\_fact:  
old\_date: “{{ (now() - timedelta(weeks=2)).strftime(‘%Y-%m-%dT%H:%M:%S’) }}”

Which gave me the error below. (That error makes me believe that our environment (AAP 2.4 + Ansible-core 2.15) perhaps is missing something.)  
TASK [Set 2 weeks ago date] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘timedelta’ is undefined. ‘timedelta’ is undefined\n\nThe error appears to be in ‘/runner/project/dealing\_with\_snapshots.yml’: line 28, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Set 2 weeks ago date\n ^ here\n”}

When somehow the “timedelta” error is resolved, do you think I’d be able to populate the “old\_date” variable prior to calling " community.vmware.vmware\_all\_snapshots\_info" and then go with something like this? (Sorry for my lack of knowledge if what I’m asking is complete nonsense…)

- name: Collect snapshots older than 2 weeks  
community.vmware.vmware\_all\_snapshots\_info:  
datacenter: “{{ datacenter\_name }}”  
validate\_certs: false  
filters:  
creation\_time: " {{ selectattr(‘vm\_name’, ‘select’, ‘creation\_time \<= old\_date’) }}"  
register: snapshots\_info

The reason I’m bringing back the idea of using “filters” from vmware\_all\_snapshots\_info is that it would return a, potentially, much shorter list of VMs for me to deal with.

Regards,

Alex

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [September 4, 2024, 8:30pm UTC](https://forum.ansible.com/t/this-topic-needs-a-title/38861/4 "2024-09-04T20:30:16Z")

</div>

If you don’t have now(), you can probably use this:

```auto
old_date: "{{ lookup('ansible.builtin.pipe', 'date -d \"now - 14 days\" +%Y-%m-%dT%H:%M:%S') }}"

```

---

<div class="post-metadata">

**Author:** ![Alex\_Wanderley](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alex_wanderley/32/3232_2.png) [@Alex\_Wanderley](https://forum.ansible.com/u/Alex_Wanderley)\
**Post date:** [September 4, 2024, 11:29pm UTC](https://forum.ansible.com/t/this-topic-needs-a-title/38861/5 "2024-09-04T23:29:45Z")

</div>

Hello Todd,

After a slight change on your suggestion (space, instead of “T”: +%Y-%m-%d" "%H:%M:%S), I could have “old\_date” as a time object:

- name: Set 2 weeks ago date  
ansible.builtin.set\_fact:

old\_date: “{{ lookup(‘ansible.builtin.pipe’, ‘date -d "now - 14 days" +%Y-%m-%d" "%H:%M:%S’) | to\_datetime }}”

So, now I can use that in Stephen’s suggestion:- name: Filter snapshots older than 2 weeks  
ansible.builtin.set\_fact:  
old\_snapshots: “{{ snapshots\_info.vmware\_all\_snapshots\_info | selectattr(‘vm\_name’, ‘select’, ‘(creation\_time \<= old\_date’) }}”

But that is still erroring out:

TASK [Filter snapshots older than 2 weeks] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: line 0

fatal: [localhost]: FAILED! =\> {“changed”: false}

I’m very grateful for all your help, but I don’t intend to consume much more of your time with my problem. So, one last question if you don’t mind:  
Would I be able to convert “creation\_time” which is a string like “2024-05-26T10:01:21.219280+00:00” into a date/time object formatted as “old\_date” mentioned up above? (As I think that the error is related to me trying to compare a string to a date/time object.)

Note: “creation\_time | to\_datetime” does not work. As, in short, it gave me:  
“… The error was: time data ‘2024-05-26T10:01:21.219280+00:00’ does not match format ‘%Y-%m-%d %H:%M:%S’\n\n…”

Again, thank you…

Alex

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [September 5, 2024, 3:59am UTC](https://forum.ansible.com/t/this-topic-needs-a-title/38861/6 "2024-09-05T03:59:43Z")

</div>

Hello Alex,

That error looks very much like the original error. I don’t have vmware snapshots I can play with, so I (and I presume Stephen Maher) are limited to throwing suggestions over the wall so to speak.

Anyway, my guess is that your creation\_time is some complex type - well, more complex than a string anyway, while our Jinja expressions are limited to producing strings. I would be curious to know what “| type\_debug” after your creation\_time produces. It should show the type of the preceding expression. In any case, I don’t think your `old_date` ends up being a datetime object since Jinja produces strings. The “| to\_datetime” part “wares off” so to speak once the expression is resolved, and you end up with a string. Behold:

```auto
utoddl@tango:~/ansible$ **cat test.yml**
---
# test.yml
- name: Date games
  hosts: localhost
  gather_facts: false
  vars:
    old_date: "{{ lookup('ansible.builtin.pipe', 'date -d \"now - 14 days\" +%Y-%m-%dT%H:%M:%S') }}"
    start_trigger: "{{ (now(utc=false,fmt='%Y-%m-%dT%H:%M:%S') | to_datetime('%Y-%m-%dT%H:%M:%S')).strftime('%s') }}"
  tasks:
    - name: Set 2 weeks ago date
      ansible.builtin.set_fact:
        new_date: "{{ lookup('ansible.builtin.pipe', 'date -d \"now - 14 days\" +%Y-%m-%d\" \"%H:%M:%S') | to_datetime }}"
    - name: Check types
      ansible.builtin.debug:
        msg:
         - '{{ old_date }}, {{ old_date | type_debug }}'
         - '{{ new_date }}, {{ new_date | type_debug }}'
utoddl@tango:~/ansible$ **ansible-playbook test.yml**

PLAY [Date games] *********************************************************************************************************

TASK [Set 2 weeks ago date] ***********************************************************************************************
ok: [localhost]

TASK [Check types] ********************************************************************************************************
ok: [localhost] => 
  msg:
  - 2024-08-21T23:56:21, AnsibleUnsafeText
  - 2024-08-21 23:56:21, AnsibleUnsafeText

```

Since the output of `date` is a string, if you could get `create_time` to be a string also, you could compare them as strings rather than datetime objects. And you probably only need the “+%Y-%m-%d” part anyway.
