On Mon, 12 Dec 2022 15:30:07 +0100 (CET)
dulhaver via Ansible Project <[email protected]> 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

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20221212173718.597130f3%40gmail.com.

Attachment: pgpzoGGh0CP7_.pgp
Description: OpenPGP digital signature

Reply via email to