# Launch task with date in var

**URL:** <https://forum.ansible.com/t/launch-task-with-date-in-var/36832>\
**Category:** Ansible Project\
**Tags:** awx\
**Created:** [July 12, 2022, 7:09am UTC](https://forum.ansible.com/t/launch-task-with-date-in-var/36832 "2022-07-12T07:09:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gabz\_Modz](https://avatars.discourse-cdn.com/v4/letter/g/6bbea6/32.png) [@Gabz\_Modz](https://forum.ansible.com/u/Gabz_Modz)\
**Post date:** [July 12, 2022, 7:09am UTC](https://forum.ansible.com/t/launch-task-with-date-in-var/36832/1 "2022-07-12T07:09:15Z")

</div>

Hello,

My playbook allows to create/disable users in the Active Directory,

In my playbook, I’m going to look for my role which will allow me to generate my variables according to a CSV.  
In this CSV, I have a start date and an end date of an employee.

I would like to know if it is possible to process the start date and end date variable in order to launch a task at that moment?

I use AWX in parallel to launch my playbook once a day.

Do you have any ideas?

Thank you!

Translated with [www.DeepL.com/Translator](http://www.DeepL.com/Translator) (free version)

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 12, 2022, 9:29am UTC](https://forum.ansible.com/t/launch-task-with-date-in-var/36832/2 "2022-07-12T09:29:09Z")

</div>

> In this CSV, I have a start date and an end date of an employee.  
> I would like to know if it is possible to process the start date  
> and end date variable in order to launch a task at that moment?

For example, given the file

> cat users.csv

2022-01-01,2023-01-01,admin  
2022-01-01,2022-07-12,alice  
2022-07-12,2023-01-01,bob

The playbook below

> cat pb.yml

- hosts: localhost  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;date\_format: "%Y-%m-%d"  
&nbsp;&nbsp;&nbsp;&nbsp;today: "{{ date\_format|strftime }}"  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- community.general.read\_csv:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ playbook\_dir }}/users.csv"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fieldnames: start,end,user  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: users  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "Launch task for user {{ item.user }} end date."  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ users.list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item.end == today  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "Launch task for user {{ item.user }} start date."  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ users.list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item.start == today

gives (abridged)

&nbsp;&nbsp;msg: Launch task for user alice end date.  
&nbsp;&nbsp;msg: Launch task for user bob start date.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 12, 2022, 9:38am UTC](https://forum.ansible.com/t/launch-task-with-date-in-var/36832/3 "2022-07-12T09:38:22Z")

</div>

Instead of the condition, use more efficient \*selectattr\*. The tasks  
below give the same result

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "Launch task for user {{ item.user }} end date."  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ users.list|selectattr('end', '==', today) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "Launch task for user {{ item.user }} start date."  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ users.list|selectattr('start', '==', today) }}"
