String 无法在字符串匹配条件下终止Ansible playbook失败
当String 无法在字符串匹配条件下终止Ansible playbook失败,string,ansible,terminate,String,Ansible,Terminate,当Number=TKT334 下面是我的剧本: --- - 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:16\tTKT334\tKIRAN\tDeployed\tPRODUCTI
Number=TKT334
下面是我的剧本:
---
- 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:16\tTKT334\tKIRAN\tDeployed\tPRODUCTION'
- 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."
)
}}
但是,当我运行playbook时,我没有失败,并继续打印调试:
我原以为它会失败,但它显示ansible运行成功,并且不会终止playbook执行
请参见下面的输出:
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
您能提出建议吗?
环境是一个特殊选项,用于为任务设置环境变量,例如:
- name: Do something
shell: echo "whatever"
environment:
http_proxy: http://my.server.com:8080
no_proxy: my.domain.com
在您的情况下,set\u fact
之后,环境始终为空,environment==“PRODUCTION”
始终为false
将变量重命名为其他变量,不要使用环境(请参阅),例如
[…]
-名称:设置字符串变量。
设定事实:
部署_环境:“生产”
-区块:
-名称:“搜索以避免重复CR编号用户:{{ansible_user_id}”
shell:“echo successsss”
失败时:(inlinevar为搜索(编号)和部署\u环境==“生产”)
[...]