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

Unable to fail terminate Ansible playbook upon string match condition

我希望在 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: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."
              )
            }}

然而,当我 运行 剧本时,我没有失败并继续打印调试:

我原以为它会失败,但它显示 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" 始终为 false。

将您的变量重命名为其他名称,不要使用环境(参见 ),例如

    [...]
    - 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")
    [...]