Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 同一循环中的Ansible循环使用with_项_Loops_Ansible_Nested Loops - Fatal编程技术网

Loops 同一循环中的Ansible循环使用with_项

Loops 同一循环中的Ansible循环使用with_项,loops,ansible,nested-loops,Loops,Ansible,Nested Loops,正在寻求帮助,尝试在多个池中循环,而某些池可能有多个主机。 我尝试使用_嵌套元素和_子元素,但这两个元素似乎都不能满足我的需要 - name: Pool members bigip_pool_member: state: present pool: "{{ item.pool }}" partition: Common host: "{{ item.host }}" port: "{{ item.port }}" provider:

正在寻求帮助,尝试在多个池中循环,而某些池可能有多个主机。 我尝试使用_嵌套元素和_子元素,但这两个元素似乎都不能满足我的需要

- name: Pool members
  bigip_pool_member:
    state: present
    pool: "{{ item.pool }}"
    partition: Common
    host: "{{ item.host }}"
    port: "{{ item.port }}"
    provider:
      server: bigip.domain.com
      user: admin
      password: admin
      validate_certs: no
  delegate_to: localhost
  with_items:
    - { pool: pool, host: [10.10.10.10, 10.10.10.11], port: 80 }
    - { pool: pool2, host: 10.10.10.10, port: 80 }
我相信主机字段一次只能获取一个值,因此我需要循环一个池的主机。输出将是类似于

  bigip_pool_member:
    state: present
    pool: pool
    partition: Common
    host: 10.10.10.10
    port: 80

  bigip_pool_member:
    state: present
    pool: pool
    partition: Common
    host: 10.10.10.11
    port: 80

  bigip_pool_member:
    state: present
    pool: pool2
    partition: Common
    host: 10.10.10.10
    port: 80
循环就是你要找的。例如,请参见下面的手册“如何引用所需的属性”

- hosts: localhost
  vars:
    my_pools:
      - {pool: pool, host: [10.10.10.10, 10.10.10.11], port: 80}
      - {pool: pool2, host: [10.10.10.10], port: 80}
  tasks:
    - debug:
        msg: "pool:{{ item.0.pool }} port:{{ item.0.port }} host:{{ item.1 }}"
      with_subelements:
        - "{{ my_pools }}"
        - host
给予

注释

只需将对属性的引用放入代码中即可。 保持一致。即使列表中只有一项,属性主机也必须是列表。 使用循环而不是与_子元素一起使用 下面是一个任务,它只需进行最小的更改就可以正常工作

- name: Pool members
  bigip_pool_member:
    state: present
    pool: "{{ item.0.pool }}"
    partition: Common
    host: "{{ item.1 }}"
    port: "{{ item.0.port }}"
    provider:
      server: bigip.domain.com
      user: admin
      password: admin
      validate_certs: no
  delegate_to: localhost
  with_subelements:
    - - {pool: pool, host: [10.10.10.10, 10.10.10.11], port: 80}
      - {pool: pool2, host: [10.10.10.10], port: 80}
    - host
未试验过

请澄清您到底想循环什么,给出您的代码片段,以便获得一些合适的答案。另外,请添加一些句子,说明您在前两次尝试中预期的行为,以及您观察到的为什么您的尝试不适合完成您期望的任务。
      loop: "{{lookup('subelements', my_pools, 'host', {'skip_missing': True})}}"
- name: Pool members
  bigip_pool_member:
    state: present
    pool: "{{ item.0.pool }}"
    partition: Common
    host: "{{ item.1 }}"
    port: "{{ item.0.port }}"
    provider:
      server: bigip.domain.com
      user: admin
      password: admin
      validate_certs: no
  delegate_to: localhost
  with_subelements:
    - - {pool: pool, host: [10.10.10.10, 10.10.10.11], port: 80}
      - {pool: pool2, host: [10.10.10.10], port: 80}
    - host