使用loop_var Ansible循环多个变量

问题描述 投票:-1回答:1

我需要在URI模块中有一个循环,它可以同时运行一个任务。我尝试过类似下面的示例,但它不起作用,因为只考虑最后一个值:

- uri:
      method: PATCH
      url: https://10.10.10.10/api/xyz
      status_code: 200, 201
      force_basic_auth: yes
      user: user
      password: pass
      return_content: yes
      validate_certs: no
      body_format: "json"
      body:
       replace:
         data:
              - enabled: "{{ x.enabled }}"
                if_name: "{{ x.name }}"
              - enabled: "{{ y.enabled }}"
                if_name: "{{ y.name }}"
  loop: "{{ vm.params | default([]) | flatten(levels=1) }}"
  loop_control:
       loop_var: x
       loop_var: y

有没有办法用loop,loop_control和loop_var实现这一点,或者将它与将来不会被弃用的其他模块一起使用?

ansible uri
1个回答
0
投票
site.yml -
---
- hosts: 
  tasks:
    - include_tasks: main.yml
      with_items: 
        - "{{  vm.params | default([]) | flatten(levels=1)  }}"
      loop_control:
        loop_var: x

main.yml -
---
- uri:
      method: PATCH
      url: https://10.10.10.10/api/xyz
      status_code: 200, 201
      force_basic_auth: yes
      user: user
      password: pass
      return_content: yes
      validate_certs: no
      body_format: "json"
      body:
       replace:
         data:
              - enabled: "{{ x.enabled }}"
                if_name: "{{ x.name }}"

I'm not using the 'y' variable in here as that is not possible based on the code you put on.
© www.soinside.com 2019 - 2024. All rights reserved.