【问题标题】:Unable to fail terminate Ansible playbook upon string match condition在字符串匹配条件下无法终止 Ansible playbook
【发布时间】:2020-06-15 18:14:31
【问题描述】:

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

下面是我的剧本:

---
- 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 run 成功并且不会终止 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

你能推荐一下吗?

【问题讨论】:

    标签: string ansible terminate


    【解决方案1】:

    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。

    将变量重命名为其他名称,不要使用环境(参见this other question for more discussion),例如

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

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      相关资源
      最近更新 更多