at - specific time & date

43 views
Skip to first unread message

dulh...@mailbox.org

unread,
Dec 12, 2022, 9:30:32 AM12/12/22
to ansible...@googlegroups.com
I want to automate a list of command exections with at.

Looking at the at modules documentation I see only unit & count for specify the time and date. That looks a litte unprecise to me.

What is the concepts here to specify something like '16:00 2023-01-19'?

Andrew Latham

unread,
Dec 12, 2022, 9:59:56 AM12/12/22
to ansible...@googlegroups.com
So you are using ansible.posix.at?


So in the code it uses the ```now +``` method. This may be a situation where a command/shell call would be the solution.





--
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-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1330471356.973684.1670855407841%40office.mailbox.org.


--
- Andrew "lathama" Latham -

Vladimir Botka

unread,
Dec 12, 2022, 11:37:41 AM12/12/22
to dulhaver via Ansible Project
On Mon, 12 Dec 2022 15:30:07 +0100 (CET)
dulhaver via Ansible Project <ansible...@googlegroups.com> wrote:

> I want to automate a list of command exections with at.
> What is the concepts here to specify something like '16:00 2023-01-19'?

For example, to schedule a command at "2022-12-12 17:30:00" declare
the variables

now_datetime: "{{ '%Y-%m-%d %H:%M:%S'|strftime }}"
at_datetime: "2022-12-12 17:30:00"
at_seconds: "{{ ((at_datetime|to_datetime) -
(now_datetime|
to_datetime('%Y-%m-%d %H:%M:%S'))).seconds }}"
at_minutes: "{{ (at_seconds|int / 60)|int + 1 }}"

gives

now_datetime: 2022-12-12 17:26:23
at_datetime: 2022-12-12 17:30:00
at_seconds: 217
at_minutes: 4

Use the variable *at_minutes*

- ansible.posix.at:
command: date > /tmp/test_at
count: "{{ at_minutes }}"
units: minutes
- command: at -l
register: out
- debug:
var: out.stdout

will display the queue

out.stdout: "5\tMon Dec 12 17:30:00 2022 a admin"

The command executed as expected

shell> cat /tmp/test_at
Mon 12 Dec 2022 05:30:00 PM CET

Example of a complete playbook for testing

- hosts: localhost

vars:

now_datetime: "{{ '%Y-%m-%d %H:%M:%S'|strftime }}"
at_datetime: "2022-12-12 17:30:00"
at_seconds: "{{ ((at_datetime|to_datetime) -
(now_datetime|
to_datetime('%Y-%m-%d %H:%M:%S'))).seconds }}"
at_minutes: "{{ (at_seconds|int /
60)|int + 1 }}"

tasks:

- debug:
msg: |
now_datetime: {{ now_datetime }}
at_datetime: {{ at_datetime }}
at_seconds: {{ at_seconds }}
at_minutes: {{ at_minutes }}

- ansible.posix.at:
command: date > /tmp/test_at
count: "{{ at_minutes }}"
units: minutes

- command: at -l
register: out

- debug:
var: out.stdout

--
Vladimir Botka

dulh...@mailbox.org

unread,
Dec 13, 2022, 3:24:11 AM12/13/22
to ansible...@googlegroups.com
thx that really works. It is not really user-friendly though.

Apparently I am not the only one thinking this should be easier, so there is an issue for adding a more intuitive way to specify time and date https://github.com/ansible-collections/ansible.posix/issues/326
> --
> 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-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20221212173718.597130f3%40gmail.com.

Vladimir Botka

unread,
Dec 13, 2022, 5:03:25 AM12/13/22
to dulhaver via Ansible Project
On Tue, 13 Dec 2022 09:23:40 +0100 (CET)
dulhaver via Ansible Project <ansible...@googlegroups.com> wrote:

> thx that really works. It is not really user-friendly though.
>
> Apparently I am not the only one thinking this should be easier, so there is an issue for adding a more intuitive way to specify time and date https://github.com/ansible-collections/ansible.posix/issues/326

> > now_datetime: "{{ '%Y-%m-%d %H:%M:%S'|strftime }}"
> > at_datetime: "2022-12-12 17:30:00"
> > at_seconds: "{{ ((at_datetime|to_datetime) -
> > (now_datetime|to_datetime)).seconds }}"
> > at_minutes: "{{ (at_seconds|int / 60)|int + 1 }}"
> >
> > - ansible.posix.at:
> > command: date > /tmp/test_at
> > count: "{{ at_minutes }}"
> > units: minutes

Yes, it's rather awkward. I simplified the declaration of
*at_seconds*. It might be a good idea to have a conversion function
for this, e.g.


- ansible.posix.at:
command: date > /tmp/test_at
count: "{{ at_datetime|at_minutes }}"
units: minutes
vars:
at_datetime: "2022-12-12 17:30:00"


Try

shell> cat plugins/filter/at_minutes.py
from datetime import datetime


def at_minutes(at_datetime):
timesince = datetime.fromisoformat(at_datetime) - datetime.now()
return int(timesince.total_seconds() / 60 + 1)


class FilterModule(object):

def filters(self):
return {
'at_minutes': at_minutes,
}


--
Vladimir Botka

dulh...@mailbox.org

unread,
Dec 13, 2022, 6:49:39 AM12/13/22
to ansible...@googlegroups.com
That works! Even with looping

##################################################################
- hosts: localhost

tasks:
- ansible.posix.at:
command: date > /tmp/test_at
count: "{{ at_datetime|at_minutes }}"
units: minutes
vars:
at_datetime: "{{ item }}"
loop:
- "2022-12-13 12:09"
- "2022-12-13 12:10"
- "2022-12-13 12:13"
##################################################################

would it be worth a PR on the issue or is this too quick'n'dirty?

It seems to be a different approach then the one suggested in the PR though (I can not judge on any of that due to missing python skills).
> --
> 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-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20221213110300.52f9ca3e%40gmail.com.

Vladimir Botka

unread,
Dec 13, 2022, 7:20:18 AM12/13/22
to dulhaver via Ansible Project
> count: "{{ at_datetime|at_minutes }}"
> ...
> would it be worth a PR on the issue or is this too quick'n'dirty?

It's quick'n'clean I'd say :)

Yes, it's worth PR. I'll write tests for this. The question is which
collection?

--
Vladimir Botka

dulh...@mailbox.org

unread,
Dec 13, 2022, 9:15:36 AM12/13/22
to ansible...@googlegroups.com
> Yes, it's worth PR. I'll write tests for this. The question is which
> collection?


honestly, I am still not really 100% sure what the term 'collection' actually means in the Ansible context and how things are organized. The issue, however, lives in ansible-collection/ansible.posix. Isn't that hint enough?
> --
> 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-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20221213132000.59eb4000%40gmail.com.

Vladimir Botka

unread,
Dec 13, 2022, 9:57:04 AM12/13/22
to dulhaver via Ansible Project
On Tue, 13 Dec 2022 15:15:06 +0100 (CET)
dulhaver via Ansible Project <ansible...@googlegroups.com> wrote:

> > Yes, it's worth PR. I'll write tests for this. The question is which
> > collection?
>
>
> honestly, I am still not really 100% sure what the term
> 'collection' actually means in the Ansible context and how things
> are organized. The issue, however, lives in
> ansible-collection/ansible.posix. Isn't that hint enough?

I'm not sure if this kind of datetime conversion belongs to POSIX.

To learn about collections see https://github.com/ansible-collections

For example, Ansible.Posix, where the module ansible.posix.at comes
from, is "Ansible Collection targeting POSIX and POSIX-ish platforms."
https://docs.ansible.com/ansible/latest/collections/ansible/posix/index.html


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