确保在可视剧本中两个任务之间已经经过了一定的时间,并且是实时的



我将通知用户一个事件将在15分钟内发生;然后,我执行的任务需要可变的时间量,少于15分钟,然后我需要等待剩余的时间,并在通知用户的15分钟后执行上述事件。

有人能在可行的情况下提出这样的实时计时器吗?pause不起作用,因为它是静态的。此外,async不能在pause任务上工作,所以我们不能与poll: 0异步启动pause,继续移动到其他任务,然后回来并确保它在我们等待的任务之前成功地使用async_status

这是我最好的尝试,但until条件似乎没有得到更新与实际的当前时间,因为它永远不会终止:

- name: Ensure a certain amount of time has elapsed between two tasks
hosts: localhost
gather_facts: no
vars:
wait_time: 10
timer_delay_interval: 1
tasks:
- name: Debug start time
debug:
var: ansible_date_time
- name: Set current time
set_fact:
start_time: "{{ ansible_date_time.date }} {{ ansible_date_time.time }}"

- name: Other task
pause:
seconds: 2
- name: Timer
set_fact:
current_time: "{{ ansible_date_time.date }} {{ ansible_date_time.time }}"
until: ((current_time | to_datetime) - (start_time | to_datetime)).total_seconds() >= wait_time
retries: 1000000
delay: "{{ timer_delay_interval }}"
register: timer_task
- name: Waited for task
debug:
msg: |
The timer has completed with {{ timer_task.attempts }} attempts,
for a total of {{ timer_task.attempts*timer_delay_interval | int }} seconds.
The original wait time was {{ wait_time }}, which means that intervening
tasks took {{ wait_time - timer_task.attempts*timer_delay_interval | int }} seconds.

注意:to_datetime过滤器要求日期时间像%Y-%m-%d %H:%M:%S一样格式化,这就是为什么我这样格式化它们的原因。

有更多的选项

  1. 并发运行任务。异步运行模块wait_for。然后,使用async_status等待剩余的wait_time结束。重试次数wait_timepause在1秒内的差值delay,以确保模块将覆盖剩余的时间。当然,在实践中,重试的次数会更少。参见下面关于offset
  2. 的注释
- name: Ensure a certain amount of time has elapsed between two tasks
hosts: localhost
gather_facts: false
vars:
wait_time: 10
pause: 5
offset: 2
tasks:
- debug:
msg: "start_time: {{ '%Y-%m-%d %H:%M:%S'|strftime }}"
- wait_for:
timeout: "{{ wait_time|int - offset|int }}"
async: 20
poll: 0
register: async_result
- pause:
seconds: "{{ pause }}"
- async_status:
jid: "{{ async_result.ansible_job_id }}"
register: job_result
until: job_result.finished
retries: "{{ wait_time|int - pause|int }}"
delay: 1
- debug:
msg: "Something happened at {{ '%Y-%m-%d %H:%M:%S'|strftime }}"

给(简略)

TASK [debug] *********************************************************************************
ok: [localhost] => 
msg: 'start_time: 2022-05-12 09:43:11'
TASK [wait_for] ******************************************************************************
changed: [localhost]
TASK [pause] *********************************************************************************
Pausing for 5 seconds
(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)
ok: [localhost]
TASK [async_status] **************************************************************************
FAILED - RETRYING: [localhost]: async_status (5 retries left).
FAILED - RETRYING: [localhost]: async_status (4 retries left).
ok: [localhost]
TASK [debug] *********************************************************************************
ok: [localhost] => 
msg: Something happened at 2022-05-12 09:43:21

  1. 下一个选项是剩余时间的计算
- name: Ensure a certain amount of time has elapsed between two tasks
hosts: localhost
gather_facts: false
vars:
wait_time: 10
pause: 5
offset: 2
tasks:
- set_fact:
start_time: "{{ '%Y-%m-%d %H:%M:%S'|strftime }}"
start_time_sec: "{{ '%s'|strftime }}"
- set_fact:
stop_time: "{{ '%Y-%m-%d %H:%M:%S'|strftime(start_time_sec|int + wait_time|int) }}"
stop_time_sec: "{{ start_time_sec|int + wait_time|int }}"
- debug:
msg: "start_time: {{ start_time }}"
- pause:
seconds: "{{ pause }}"
- set_fact:
wait_time: "{{ stop_time_sec|int - '%s'|strftime|int - offset|int }}"
- debug:
msg: |-
wait_time: {{ wait_time }}
when: debug|d(false)|bool
- wait_for:
timeout: "{{ wait_time|int }}"
- debug:
msg: "Something happened at {{ '%Y-%m-%d %H:%M:%S'|strftime }}"

给(简略)

TASK [debug] *********************************************************************************
ok: [localhost] => 
msg: 'start_time: 2022-05-12 09:55:08'
TASK [pause] *********************************************************************************
Pausing for 5 seconds
(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)
ok: [localhost]
TASK [set_fact] ******************************************************************************
ok: [localhost]
TASK [debug] *********************************************************************************
skipping: [localhost]
TASK [wait_for] ******************************************************************************
ok: [localhost]
TASK [debug] *********************************************************************************
ok: [localhost] => 
msg: Something happened at 2022-05-12 09:55:18

适合偏移到您的系统。

相关内容

  • 没有找到相关文章

最新更新