我想跳过最后一个任务“名称:如果不是在办公时间失败”“如果我以前的任务"-名称:获得办公时间”条件为真。
正在发生的事情是,当我的“上班时间”条件为真时,最后一项任务不是跳过。
我将在AWX中导入这个剧本,并安排一个作业链。我有两个选择。要么创建最后一个任务的条件(如果不是在办公时间内失败),然后失败,或者如果第二个任务(获得办公时间)成功运行,则应该跳过最后一个任务,以便在第二个任务成功的条件下触发其他作业。
注意:在AWX中,跳过的任务没有正确标识。如果条件不满足,则任务将失败。
我的问题可能令人困惑,因为我正在尝试不同的逻辑来使这个剧本工作。希望你们能理解我的要求。
ansible_date_time
发布于 2022-07-06 18:02:21
failed_when在提供的表达式计算true时执行。以17为例,您的剧本将评估如下:
cur_time: 17
- name: get working office hours
debug:
msg: "Yes ! Current time is between office hours"
when: 17| int >= 7 and 17| int <= 19 # evaluates true
- name: Fail if not in office hours
debug:
msg: "No ! Current time is not between office hours"
failed_when: 17| int < 7 and 17| int > 18 # evaluates false, does not fail如果要保持任务的相同结构,请将最终表达式否定如下:
name: Fail if not in office hours
debug:
msg: "No ! Current time is not between office hours"
failed_when: not (cur_time| int < 7 and cur_time| int > 18)结果:
PLAY [play] *************************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************************
ok: [localhost]
TASK [Print msg variable] ***********************************************************************************************************************************************
ok: [localhost] => {
"msg": "18"
}
TASK [Set variables] ****************************************************************************************************************************************************
ok: [localhost]
TASK [Print Current time] ***********************************************************************************************************************************************
ok: [localhost] => {
"msg": "18"
}
TASK [get working office hours] *****************************************************************************************************************************************
ok: [localhost] => {
"msg": "Yes ! Current time is between office hours"
}
TASK [Fail if not in office hours] **************************************************************************************************************************************
fatal: [localhost]: FAILED! => {
"msg": "No ! Current time is not between office hours"
}
PLAY RECAP **************************************************************************************************************************************************************
localhost : ok=5 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0但是,如果我是您,我更愿意使用assert任务来确保我们处于办公时间,然后我们可以继续使用以下操作:
- name: Set variables
set_fact:
cur_time: "{{ ansible_date_time.hour }}"
- name: Ensure we are running playbook within working hours
assert:
that: cur_time | int >= 7 and cur_time | int <= 19
success_msg: "Yes ! Current time is between office hours"
fail_msg: "No ! Current time is not between office hours"
# continue with the rest of your play as normal这使我们(在工作时间内):
PLAY [play] *************************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************************
ok: [localhost]
TASK [Print msg variable] ***********************************************************************************************************************************************
ok: [localhost] => {
"msg": "18"
}
TASK [Set variables] ****************************************************************************************************************************************************
ok: [localhost]
TASK [Print Current time] ***********************************************************************************************************************************************
ok: [localhost] => {
"msg": "18"
}
TASK [Ensure we are running playbook within working hours] **************************************************************************************************************
ok: [localhost] => {
"changed": false,
"msg": "Yes ! Current time is between office hours"
}
PLAY RECAP **************************************************************************************************************************************************************
localhost : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0在工作时间以外:
PLAY [play] *************************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************************
ok: [localhost]
TASK [Print msg variable] ***********************************************************************************************************************************************
ok: [localhost] => {
"msg": 1
}
TASK [Set variables] ****************************************************************************************************************************************************
ok: [localhost]
TASK [Print Current time] ***********************************************************************************************************************************************
ok: [localhost] => {
"msg": 1
}
TASK [Ensure we are running playbook within working hours] **************************************************************************************************************
fatal: [localhost]: FAILED! => {
"assertion": "cur_time | int >= 7 and cur_time | int <= 19",
"changed": false,
"evaluated_to": false,
"msg": "No ! Current time is not between office hours"
}
PLAY RECAP **************************************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0https://stackoverflow.com/questions/72887905
复制相似问题