Ansible Variable not working in AAP v4.5.6

Hi All,

I am running AAP v4.5.6 for ILMT task automation purposes. for a requirement I have created a constructed Inventory to filter the VMs which is created last 7 days from Azure subscription. I used this module in variable “ansible_date_time.date” and It was worked before my AAP v4.5.4.

Recently we have seen our AAP version upgraded to 4.5.6 and observed this constructed Inventory filter not working for last 7 days created VMs.

So I am requesting here are they any module support for 4.5.6 to use “ansible_data_time.date” variable to get the VM filter in my constructed Invetory?

Thanks,
Nagarajan.

Two things.

  1. Even in AAP v4.5.4, you would need to cache facts about all hosts, but especially your new hosts, to filter your constructed inventory by ansible_date_time.date.
  2. This value would be the current date at the time this fact was gathered. So, anytime your cache expires and this fact is gathered again, your constructed inventory would now contain recently updated hosts, not recently created hosts.

I think what you want, assuming you’re using the azure.azcollection.azure_rm inventory plugin, is the creation_time which is a host variable that is always available (according to the example).

Hi Denney,

Thanks for your reply and I am trying to filter the VMs from Azure subscription based on VM created date should be last 7 days.

For Example 10 servers running with Azure cloud in “subscription A”, the 5 VMs has been created 5/2/2024, and another set of 5 VMs has been created 04/03/2024 in same Azure subscription. Also I could see all the 10 VMs in AAP.

I want to filter out the VMs which is created last 7 days from current date. so that I am using “ansible_date_time.date” varibale in below syntax to filter out the last 7 days created VMs.

It is not filtering with current version of AAP 4.5.6. but the same time It was worked in AAP v4.5.4.

Thanks,
Nagarajan.

Thank you for your example.

ansible_date_time is an ansible_fact that gets gathered when you run ansible against your hosts. It is not a magic variable or function that is evaluated the moment you reference it.

You have gather_facts and gather_subset specified, but these don’t mean anything to ansible.builtin.constructed inventories. This won’t help your inventory to gather these facts if they’re missing. In order for AAP to know what this variable is and not be null, it must be a cached fact in your inventory from previously run jobs with fact caching enabled in the job template. If the fact is not cached, then the conditional will technically error, but since you have strict: false, errors resolve to “false” and no failed host would be added to the intel_hosts group.

If the facts are cached and the cached date was 05/2/2024 on your first 5 hosts, and 04/03/2024 on the other 5, just like the origination dates for the respective hosts, then all 10 hosts would pass your filter as being less than 7 days difference.

This is an example of one of my hosts from today. The time now (as of me writing this post) is ~11:09:00, but the fact is already stale by over an hour. It could be a few months stale before this fact is cleared/expired and refreshed.
image

What you’re looking for is the now() function.

For e.g.

plugin: ansible.builtin.constructed
strict: false
groups:
  intel_hosts: (now() - (tags.originationdate | to_datetime('%Y-%m-%d'))).days <= 7
  ilmt: computer_name == 'foobar'

or, using the azure.azcollection.azure_rm creation_time var:

plugin: ansible.builtin.constructed
strict: false
groups:
  intel_hosts: (now() - (creation_time | to_datetime('%Y-%m-%d'))).days <= 7
  ilmt: computer_name == 'foobar'

Thanks Denney, It is working with your updates.

1 Like