无法在字符串匹配条件下终止 Ansible 剧本失败



我希望失败并在Number=TKT334时终止我的 ansible 剧本

以下是我的剧本:

---
- name: "Play 1"
hosts: localhost
any_errors_fatal: True
serial: 1
tags: always
tasks:
- name: Setting string variable.
set_fact:
inlinevar: '2020-06-10 20:22:16tTKT334tKIRANtDeployedtPRODUCTION'
- name: Setting string variable.
set_fact:
environment: 'PRODUCTION'

- block:
- name: "Search to avoid duplicate CR Numbers user:{{ ansible_user_id }}"
shell: "echo SUCCESSSSSSS"
failed_when: (inlinevar is search( Number ) and environment == "PRODUCTION")
- debug:
msg: This is FINAL  inlinevar `{{ inlinevar }}`
rescue:
- name: Print custom conditional debug message
fail:
msg: >-
{{
command_result_app.stdout is search( Number ) |
ternary(
"This CR is already Deployed. Hence retry with a Unique CR Number.",
"The Dtabase is not reachable. Unable to connect To the Database."
)
}}

但是,当我运行剧本时,我不会失败并继续打印调试:

我预计它会失败,但它显示 ansible 运行成功并且不会终止剧本执行。

请参阅下面的输出:

ansible-playbook test.yml -e Number=TKT334
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Play 1] *************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [localhost]
TASK [Setting string variable.] ****************************************************************
ok: [localhost]
TASK [Setting string variable.] ****************************************************************
ok: [localhost]
TASK [Search to avoid duplicate CR Numbers user:{{ ansible_user_id }}] ***
changed: [localhost]
TASK [debug] ***********************************************************************************************************************************************************
ok: [localhost] => {
"msg": "This is FINAL  inlinevar `2020-06-10 20:22:16\tTKT334\t KIRAN\tDeployed\tPRODUCTION`"
}
PLAY RECAP *************************************************************************************************************************************************************
localhost                  : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

你能建议一下吗?

environment是一个特殊选项,用于为任务设置环境变量,例如

- name: Do something
shell: echo "whatever"
environment:
http_proxy: http://my.server.com:8080
no_proxy: my.domain.com

在您的情况下,环境在set_fact后始终为空,environment == "PRODUCTION"始终为假。

将您的变量重命名为其他名称,不要使用环境(有关更多讨论,请参阅其他问题(,例如

[...]
- name: Setting string variable.
set_fact:
deploy_environment: 'PRODUCTION'
- block:
- name: "Search to avoid duplicate CR Numbers user:{{ ansible_user_id }}"
shell: "echo SUCCESSSSSSS"
failed_when: (inlinevar is search( Number ) and deploy_environment == "PRODUCTION")
[...]

最新更新