我將在 15 分鐘內通知用戶有事件發生;然后,我執行耗時少于 15 分鐘的任務,然后我需要等待剩余時間,并在我通知用戶后的 15 分鐘內執行所述事件。
有人可以在ansible中提出這樣的實時計時器嗎? pause不會作業,因為它是靜態的。此外,async它不適用于pause任務,因此我們不能pause異步啟動 a poll: 0,繼續執行其他任務,然后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,這就是我以這種方式格式化它們的原因。
uj5u.com熱心網友回復:
還有更多選擇。
- 同時運行任務。異步運行模塊wait_for 。然后,使用async_status等待剩余的wait_time過去。重試次數是wait_time和pause in 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:
- 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
- 下一個選項是剩余時間的計算
- 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
適合您的系統的偏移量。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/474739.html
