【问题标题】:Ansible Task failing with when conditionAnsible 任务因条件失败而失败
【发布时间】:2020-06-16 15:57:20
【问题描述】:

我的 Ansible 任务在条件为假时失败(仅当“当”条件为真时,任务才会失败)

我的剧本

    - name: 'Check if any task is existing'
      command: aws ecs list-tasks --cluster mycluster --query length(taskArns[*])
      register: ecs_tasks

    - name: 'Fail playbook if some task is already existing in cluster'
      fail:
        msg: "There is already an existing task in mycluster"
      when: ecs_tasks.stdout != 0

    - name: 'Create Ranger task'
      command: create ECS task
      register: ecs_task

输出

    "stderr": "", 
    "stderr_lines": [], 
    "stdout": "0", 
    "stdout_lines": [
        "0"
    ]
    }

    TASK [Fail playbook if some task is already existing in cluster] ***************
    task path: /home/somepath/Task.yml:35
    fatal: [127.0.0.1]: FAILED! => {
    "changed": false, 
    "msg": "There is already an existing task in mycluster"
    }

    PLAY RECAP *********************************************************************
09:09:38  127.0.0.1                  : ok=5    changed=4    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0    

我的 when Condition 格式是否存在问题,因为我尝试了各种条件,例如 >0 和 >=1,但没有运气,因为它仍然失败(我的 ECS 集群中不存在任何任务)以及 AWS CLI 命令返回

aws ecs list-tasks --cluster mycluster --query length(taskArns[*])
0

【问题讨论】:

    标签: ansible jinja2 amazon-ecs


    【解决方案1】:

    问题是您将string(在您的注册输出中)与int(在您的 when 子句中)进行比较。您应该将 when 条件更改为:

    when: ecs_tasks.stdout != "0"
    

    此外,您不需要第二个任务来验证失败,因为您可以轻松地在第一个任务上使用failed_when 条件:

        - name: 'Check if any task is existing'
          command: aws ecs list-tasks --cluster mycluster --query length(taskArns[*])
          register: ecs_tasks
          failed_when: ecs_tasks.stdout != "0"
    

    旁注

    • 如果命令返回码仍然有意义,通常最好还是检查它,这样您就不会不必要地解析可能被误解的输出。在您的情况下,我想您可以轻松地将您的条件更改为(注意 rcint):

      failed_when: ecs_tasks.rc != 0 or ecs_tasks.stdout != "0"
      

      如果您的命令有多个可以视为成功的返回码(例如02),您可以更改为

      failed_when: ecs_tasks.rc not in [0,2] or ecs_tasks.stdout != "0"
      
    • 您可能很想将输出比较转换为int,例如:

      ecs_tasks.stdout | int != 0
      

      这应该可行,但请注意,sdtout 中任何不可解析为 int 的字符串值都将导致 O,例如:

      $ ansible localhost -m debug -a msg="{{ 'whatever' | int }}"
      localhost | SUCCESS => {
      "msg": "0"
      }
      

    【讨论】:

      【解决方案2】:

      以下任何一个都应该工作:

      - name: 'Fail playbook if some task is already existing in cluster'
        fail:
          msg: "There is already an existing task in mycluster"
        when: ecs_tasks.stderr | length > 0
      

      或者

      - name: 'Fail playbook if some task is already existing in cluster'
        fail:
          msg: "There is already an existing task in mycluster"
        when: ecs_tasks.stderr != ""
      

      在此处参考更多信息:How to test that a registered variable is not empty?

      【讨论】:

      • OP 不想检查已注册的 var 是否为空但不包含“0”。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      • 2017-01-08
      • 1970-01-01
      相关资源
      最近更新 更多