使用loop_var Ansible循环多个变量

时间:2018-12-05 11:09:33

标签: ansible uri

我需要在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或与将来不再使用的其他模块结合使用来实现此目的?

1 个答案:

答案 0 :(得分: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.