# Ansible Variable not working in AAP v4.5.6

**URL:** <https://forum.ansible.com/t/ansible-variable-not-working-in-aap-v4-5-6/5246>\
**Category:** Get Help\
**Tags:** azure, aap\
**Created:** [May 1, 2024, 8:02pm UTC](https://forum.ansible.com/t/ansible-variable-not-working-in-aap-v4-5-6/5246 "2024-05-01T20:02:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![naga](https://avatars.discourse-cdn.com/v4/letter/n/7ab992/32.png) [@naga](https://forum.ansible.com/u/naga)\
**Post date:** [May 1, 2024, 8:02pm UTC](https://forum.ansible.com/t/ansible-variable-not-working-in-aap-v4-5-6/5246/1 "2024-05-01T20:02:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [May 2, 2024, 7:51pm UTC](https://forum.ansible.com/t/ansible-variable-not-working-in-aap-v4-5-6/5246/2 "2024-05-02T19:51:31Z")

</div>

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](https://docs.ansible.com/ansible/latest/collections/azure/azcollection/azure_rm_inventory.html#examples) inventory plugin, is the `creation_time` which is a host variable that is always available (according to the example).

---

<div class="post-metadata">

**Author:** ![naga](https://avatars.discourse-cdn.com/v4/letter/n/7ab992/32.png) [@naga](https://forum.ansible.com/u/naga)\
**Post date:** [May 3, 2024, 11:09am UTC](https://forum.ansible.com/t/ansible-variable-not-working-in-aap-v4-5-6/5246/3 "2024-05-03T11:09:41Z")

</div>

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.

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

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.

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [May 3, 2024, 4:09pm UTC](https://forum.ansible.com/t/ansible-variable-not-working-in-aap-v4-5-6/5246/4 "2024-05-03T16:09:33Z")

</div>

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](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html) 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](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/constructed_inventory.html) 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](https://us1.discourse-cdn.com/ansible/original/2X/7/71975304128251242ec6d6df00d5fd7cf02a39e9.png)

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

For e.g.

```yaml
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:

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

```

---

<div class="post-metadata">

**Author:** ![naga](https://avatars.discourse-cdn.com/v4/letter/n/7ab992/32.png) [@naga](https://forum.ansible.com/u/naga)\
**Post date:** [May 3, 2024, 5:29pm UTC](https://forum.ansible.com/t/ansible-variable-not-working-in-aap-v4-5-6/5246/5 "2024-05-03T17:29:01Z")

</div>

Thanks Denney, It is working with your updates.

---

<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:** [June 2, 2024, 5:29pm UTC](https://forum.ansible.com/t/ansible-variable-not-working-in-aap-v4-5-6/5246/6 "2024-06-02T17:29:31Z")

</div>

Closed on June 2, 2024, 5:29pm UTC

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