首页 > 解决方案 > 无法解决阻塞和救援中的错误

问题描述

我已经用块和救援方法编写了一个用于错误处理的代码。我已经给出了块中的条件,所以当它满足时它会导入我在块中编写的剧本。

并且对于 when 条件下的值,我在它下面给出了 with_items。对齐,一切都很好。但我在 with_items 上遇到了错误。下面是我写的任务部分

任务:

  - name: including the user_list
    include_vars: Users.yml
    no_log: 'yes'
  - name: user validating using block
    block:
     - import_playbook: CIname.yml
    when: '"{{ CI_name }}" == item.ci_name and "{{ username }}" == item.username'
    with_items: "{{ user_list }}"
    rescue:
        - name: Update the work notes of the incident when block fails

    always:
      - name: Post the status back to ServiceNow

我刚开始的错误是:

ERROR! 'with_items' is not a valid attribute for a Play be elsewhere in the file depending on the exact syntax problem.

标签: pythonansible

解决方案


在 Ansible 中循环块是不可能的。我认为您要实现的是import_playbook模块上的循环,应该可以这样:

  - name: user validating using block
    block:
     - import_playbook: CIname.yml
       when: CI_name == item.ci_name and username == item.username
       with_items: "{{ user_list }}"
    rescue:
      - ....
    always:
      - ....

推荐阅读