Loops 如何在遍历with_项调试列表时,基于传递when条件执行Ansible任务?

Loops 如何在遍历with_项调试列表时,基于传递when条件执行Ansible任务?,loops,debugging,ansible,conditional-statements,Loops,Debugging,Ansible,Conditional Statements,我正试图在with_用户项列表上基于when条件执行API POST。从API GET收集when条件,然后将其存储为调试变量 问题:调试变量似乎没有正确迭代,API POST任务只遵循第一个结果。这会导致尝试对with_items列表中的所有项目执行POST,或将其全部跳过 我不明白是否每个“request\u ad\u user”都在创建一个新变量,以及它们是如何在“when”条件下迭代的。我错过了什么 这是我的密码: - name: Add a users to univention AD

我正试图在with_用户项列表上基于when条件执行API POST。从API GET收集when条件,然后将其存储为调试变量

问题:调试变量似乎没有正确迭代,API POST任务只遵循第一个结果。这会导致尝试对with_items列表中的所有项目执行POST,或将其全部跳过

我不明白是否每个“request\u ad\u user”都在创建一个新变量,以及它们是如何在“when”条件下迭代的。我错过了什么

这是我的密码:

- name: Add a users to univention AD server.
  hosts: localhost

  tasks:
      - name: Include user to add as variable
        include_vars:
            file: users.yaml
            name: users

      - name: Check if AD users exist (object DN)
        uri:
          url: https://10.10.10.10/univention/udm/users/user/uid%3D{{item.username}}%2Ccn%3Dusers%2Cdc%3Dcybertax%2Cdc%3Dcso%2Cdc%3Dcom
          user: admin
          password: "{{users.adminpw}}"
          validate_certs: no
          return_content: yes
          status_code: 200,404
          method: GET
          timeout: 10
        with_items:
          - "{{users.user}}"
        register: request_ad_user

      - name: debug univention user object DN request
        debug:
          var: request_ad_user

      - name: Add AD user accounts
        uri:
          url: https://10.10.10.10/univention/udm/users/user/
          user: admindh
          password: "{{users.vcenterPassword}}"
          validate_certs: no
          return_content: yes
          status_code: 201
          method: POST
          body: "{\"uuid\": \"string\", \"uri\": \"https://10.104.8.110/univention/udm/users/user/uid={{item.username}},dc=cybertax,dc=cso,dc=com\", \"options\": {\"pki\": false}, \"policies\": {\"pol
          body_format: json
        when:
          - request_ad_user.results[0].status == 404
        with_items:
          - "{{users.user}}"

尝试添加循环索引,并在
when
子句中使用该索引:

  - name: Add AD user accounts
    uri:
      url: https://10.10.10.10/univention/udm/users/user/
      user: admindh
      password: "{{users.vcenterPassword}}"
      validate_certs: no
      return_content: yes
      status_code: 201
      method: POST
      body: "{\"uuid\": \"string\", \"uri\": \"https://10.104.8.110/univention/udm/users/user/uid={{item.username}},dc=cybertax,dc=cso,dc=com\", \"options\": {\"pki\": false}, \"policies\": {\"pol
      body_format: json
    when:
      - request_ad_user.results[ndx].status == 404
    with_items:
      - "{{users.user}}"
    loop_control:
      index_var: ndx

尝试添加循环索引,并在
when
子句中使用该索引:

  - name: Add AD user accounts
    uri:
      url: https://10.10.10.10/univention/udm/users/user/
      user: admindh
      password: "{{users.vcenterPassword}}"
      validate_certs: no
      return_content: yes
      status_code: 201
      method: POST
      body: "{\"uuid\": \"string\", \"uri\": \"https://10.104.8.110/univention/udm/users/user/uid={{item.username}},dc=cybertax,dc=cso,dc=com\", \"options\": {\"pki\": false}, \"policies\": {\"pol
      body_format: json
    when:
      - request_ad_user.results[ndx].status == 404
    with_items:
      - "{{users.user}}"
    loop_control:
      index_var: ndx

这个很好用,非常感谢!这个很好用,非常感谢!