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
On Wed, Sep 4, 2024 at 2:30 PM Todd Lewis <uto...@gmail.com> wrote:

> If you don't have now(), you can probably use this:
>
> old_date: "{{ lookup('ansible.builtin.pipe', 'date -d \"now - 14 days\" 
> +%Y-%m-%dT%H:%M:%S') }}"
>
>
>
> On 9/4/24 4:05 PM, Alex Wanderley wrote:
>
> 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
>
> On Wed, Sep 4, 2024 at 1:11 AM Stephen Maher <mahes...@gmail.com> wrote:
>
>> 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
>>
>>
>> On 4 Sep 2024, at 03:37, Alex Wanderley <alex.wander...@edmonton.ca>
>> wrote:
>>
>> 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: "<filter>"
>>                match_type: includes
>>        register: old_snapshots
>>
>> Thanks...
>>
>> Alex
>>
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ansible Project" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to ansible-project+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/CADp8UUS10CbPWP45TRpfXB%2BYRWev5KaSRFjchAi7KQO7Dt%2BzHQ%40mail.gmail.com
>> <https://groups.google.com/d/msgid/ansible-project/CADp8UUS10CbPWP45TRpfXB%2BYRWev5KaSRFjchAi7KQO7Dt%2BzHQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ansible Project" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to ansible-project+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/6F27360B-C585-4887-A1DF-230B8C3020A0%40gmail.com
>> <https://groups.google.com/d/msgid/ansible-project/6F27360B-C585-4887-A1DF-230B8C3020A0%40gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>
>
> *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.* --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CADp8UUTFhKPwN3DT41e7SxwOQs4jSOWZvjeQk2F%3DuCq8B%3Dv5Cg%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CADp8UUTFhKPwN3DT41e7SxwOQs4jSOWZvjeQk2F%3DuCq8B%3Dv5Cg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
>
> --
> Todd
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/622a5121-efa6-495b-b836-e8f583d8184d%40gmail.com
> <https://groups.google.com/d/msgid/ansible-project/622a5121-efa6-495b-b836-e8f583d8184d%40gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
*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.*

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CADp8UUR0h7cN8k9E7mPL1Wxnqgdzx7_DxpyT4oZhe-iSaXwWSw%40mail.gmail.com.

Reply via email to