Launch task with date in var

11 views
Skip to first unread message

Gabz Modz

unread,
Jul 12, 2022, 3:09:15 AM7/12/22
to Ansible Project
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 (free version)

Vladimir Botka

unread,
Jul 12, 2022, 5:29:30 AM7/12/22
to Gabz Modz, ansible...@googlegroups.com
On Tue, 12 Jul 2022 00:09:15 -0700 (PDT)
Gabz Modz <gabz...@gmail.com> wrote:

> 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

shell> 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

shell> cat pb.yml
- hosts: localhost
vars:
date_format: "%Y-%m-%d"
today: "{{ date_format|strftime }}"
tasks:
- community.general.read_csv:
path: "{{ playbook_dir }}/users.csv"
fieldnames: start,end,user
register: users
- debug:
msg: "Launch task for user {{ item.user }} end date."
loop: "{{ users.list }}"
when: item.end == today
- debug:
msg: "Launch task for user {{ item.user }} start date."
loop: "{{ users.list }}"
when: item.start == today

gives (abridged)

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

--
Vladimir Botka

Vladimir Botka

unread,
Jul 12, 2022, 5:38:37 AM7/12/22
to Gabz Modz, ansible...@googlegroups.com
On Tue, 12 Jul 2022 11:29:09 +0200
Vladimir Botka <vbo...@gmail.com> wrote:

> - debug:
> msg: "Launch task for user {{ item.user }} end date."
> loop: "{{ users.list }}"
> when: item.end == today
> - debug:
> msg: "Launch task for user {{ item.user }} start date."
> loop: "{{ users.list }}"
> when: item.start == today

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

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

--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages