首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当前一个条件为真时跳过最后一个任务

当前一个条件为真时跳过最后一个任务
EN

Stack Overflow用户
提问于 2022-07-06 17:49:13
回答 1查看 220关注 0票数 0

我想跳过最后一个任务“名称:如果不是在办公时间失败”“如果我以前的任务"-名称:获得办公时间”条件为真。

正在发生的事情是,当我的“上班时间”条件为真时,最后一项任务不是跳过。

我将在AWX中导入这个剧本,并安排一个作业链。我有两个选择。要么创建最后一个任务的条件(如果不是在办公时间内失败),然后失败,或者如果第二个任务(获得办公时间)成功运行,则应该跳过最后一个任务,以便在第二个任务成功的条件下触发其他作业。

注意:在AWX中,跳过的任务没有正确标识。如果条件不满足,则任务将失败。

我的问题可能令人困惑,因为我正在尝试不同的逻辑来使这个剧本工作。希望你们能理解我的要求。

ansible_date_time

  • name:
  • 名称:
  • :{{ ansible_date_time.hour }}-名称: Set变量set_fact: cur_time:"{{ ansible_date_time.hour }}“-名称:"{{ ansible_date_time.hour }}”-名称:打印当前时间调试: msg:"{{ cur_time }}“-名称:获取工作时间调试: msg:”是的!当前时间是在办公时间之间“时: cur_time \ int >= 7和cur_time \ int <= 19 -名称:如果不在办公时间失败,调试: msg:”否!当前的时间不是在办公时间之间“failed_when: cur_time区int <7和cur_time区int >cur_time
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-06 18:02:21

failed_when在提供的表达式计算true时执行。以17为例,您的剧本将评估如下:

代码语言:javascript
复制
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

如果要保持任务的相同结构,请将最终表达式否定如下:

代码语言:javascript
复制
 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)

结果:

代码语言:javascript
复制
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任务来确保我们处于办公时间,然后我们可以继续使用以下操作:

代码语言:javascript
复制
- 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

这使我们(在工作时间内):

代码语言:javascript
复制
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

在工作时间以外:

代码语言:javascript
复制
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=0
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72887905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档