Ansible failed_when条件不能阻止任务失败

时间:2020-01-24 14:02:51

标签: ansible

我正在运行以下ansible任务:

    - name: Remove Jenkins
      register: jenkins_delete
      shell: "helm delete --purge --tls jenkins-{{ env_name }}"
      retries: 2
      delay: 20
      until: jenkins_delete.rc == 0
      failed_when: "'jenkins-{{ env_name }} not found' not in jenkins_delete.stderr"

如果我要查找的版本不存在,则失败,如下所示:

fatal: [localhost]: FAILED! => {"attempts": 5, "changed": true, "cmd": "helm delete --purge --tls jenkins-inception", "delta": "0:00:00.212773", "end": "2020-01-24 13:58:24.993134", "failed_when_result": true, "msg": "non-zero return code", "rc": 1, "start": "2020-01-24 13:58:24.780361", "stderr": "Error: release: \"jenkins-inception\" not found", "stderr_lines": ["Error: release: \"jenkins-inception\" not found"], "stdout": "", "stdout_lines": []}

我要使用的failed_when条件不是要防止这种情况吗?

编辑:我也尝试了此方法,但没有成功:

    - name: Remove Jenkins
      register: jenkins_delete
      shell: "helm delete --purge --tls jenkins-{{ env_name }}"
      retries: 2
      delay: 10
      until: jenkins_delete.rc == 0 or jenkins_delete.stderr is regex("release:.+not found")

这很奇怪,因为似乎使用的regex应该match stderr中的字符串

1 个答案:

答案 0 :(得分:0)

据我所知,jenkins-inceptionstderr周围有引号:

"Error: release: \"jenkins-inception\" not found"

此正则表达式应匹配:

"jenkins-{{ env_name }}" not found