ansible 循环遍历列表

ansible loop over list

我有库存(可以更改):

index:
  - indexName: "AAA"
    homePath: "$SPLUNK_DB/AAA/db"
    coldPath: "$SPLUNK_DB/AAA/colddb"
    thawedPath: "$SPLUNK_DB/AAA/thaweddb"
    repFactor: "auto"
  - indexName: "BBB"
    homePath: "$SPLUNK_DB/BBB/db"
    coldPath: "$SPLUNK_DB/BBB/colddb"
    thawedPath: "$SPLUNK_DB/BBB/thaweddb"
    repFactor: "auto"

我想遍历索引,但也想使用键值。像这样:

- name: Write paths for the index
  ini_file:
    dest: "{{ splunk.home }}/etc/master-apps/_cluster/local/indexes.conf"
    section: "{{ index.indexName }}"
    option: "{{ item.key }}"
    value: "{{ item.value }}"
  with_items:
    - { key: "homePath", value: "{{ index.homePath | default('', true) }}" }
    - { key: "thawedPath", value: "{{ index.thawedPath | default('', true) }}" }
    - { key: "coldPath", value: "{{ index.coldPath | default('', true) }}" }
    - { key: "repFactor", value: "{{ index.repFactor | default('', true) }}" }

这可能吗?

是的。这是可能的。使用 subelements。首先准备数据的结构。例如

    - set_fact:
        index2: "{{ index2|default([]) +
                    [{'indexName': item.indexName, 'conf': conf}] }}"
      loop: "{{ index }}"
      vars:
        conf: "{{ item|dict2items|
                  rejectattr('key', '==', 'indexName')|list }}"
    - debug:
        var: index2

给予

  index2:
  - conf:
    - key: homePath
      value: $SPLUNK_DB/AAA/db
    - key: coldPath
      value: $SPLUNK_DB/AAA/colddb
    - key: thawedPath
      value: $SPLUNK_DB/AAA/thaweddb
    - key: repFactor
      value: auto
    indexName: AAA
  - conf:
    - key: homePath
      value: $SPLUNK_DB/BBB/db
    - key: coldPath
      value: $SPLUNK_DB/BBB/colddb
    - key: thawedPath
      value: $SPLUNK_DB/BBB/thaweddb
    - key: repFactor
      value: auto
    indexName: BBB

然后使用列表 index2 并查看项目

    - name: Write paths for the index
      # ini_file:
      debug:
        msg:
          - "dest: {{ splunk.home }}/etc/master-apps/_cluster/local/indexes.conf"
          - "section: {{ item.0.indexName }}"
          - "option: {{ item.1.key }}"
          - "value: {{ item.1.value }}"
      loop: "{{ lookup('subelements', index2, 'conf') }}"

给予


  msg:
  - 'dest: splunk_home/etc/master-apps/_cluster/local/indexes.conf'
  - 'section: AAA'
  - 'option: homePath'
  - 'value: $SPLUNK_DB/AAA/db'

  msg:
  - 'dest: splunk_home/etc/master-apps/_cluster/local/indexes.conf'
  - 'section: AAA'
  - 'option: coldPath'
  - 'value: $SPLUNK_DB/AAA/colddb'

  msg:
  - 'dest: splunk_home/etc/master-apps/_cluster/local/indexes.conf'
  - 'section: AAA'
  - 'option: thawedPath'
  - 'value: $SPLUNK_DB/AAA/thaweddb'

  msg:
  - 'dest: splunk_home/etc/master-apps/_cluster/local/indexes.conf'
  - 'section: AAA'
  - 'option: repFactor'
  - 'value: auto'

  msg:
  - 'dest: splunk_home/etc/master-apps/_cluster/local/indexes.conf'
  - 'section: BBB'
  - 'option: homePath'
  - 'value: $SPLUNK_DB/BBB/db'

  msg:
  - 'dest: splunk_home/etc/master-apps/_cluster/local/indexes.conf'
  - 'section: BBB'
  - 'option: coldPath'
  - 'value: $SPLUNK_DB/BBB/colddb'

  msg:
  - 'dest: splunk_home/etc/master-apps/_cluster/local/indexes.conf'
  - 'section: BBB'
  - 'option: thawedPath'
  - 'value: $SPLUNK_DB/BBB/thaweddb'

  msg:
  - 'dest: splunk_home/etc/master-apps/_cluster/local/indexes.conf'
  - 'section: BBB'
  - 'option: repFactor'
  - 'value: auto

如果这是您想要的,请写入文件。例如

    - name: Write paths for the index
      ini_file:
        dest: indexes.conf
        section: "{{ item.0.indexName }}"
        option: "{{ item.1.key }}"
        value: "{{ item.1.value }}"
      loop: "{{ lookup('subelements', index2, 'conf') }}"

给予

shell> cat indexes.conf 

[AAA]
homePath = $SPLUNK_DB/AAA/db
coldPath = $SPLUNK_DB/AAA/colddb
thawedPath = $SPLUNK_DB/AAA/thaweddb
repFactor = auto
[BBB]
homePath = $SPLUNK_DB/BBB/db
coldPath = $SPLUNK_DB/BBB/colddb
thawedPath = $SPLUNK_DB/BBB/thaweddb
repFactor = auto

使用实际的数据结构,您可以通过以下方式实现您的目标。我们在一侧有 index 列表,在另一侧有我们要引用的键列表。 product filter 创建一个全局列表,我们可以循环其他列表以从两个列表中获取每个可能的组合:

- name: Write paths for the index
  ini_file:
    dest: "{{ splunk.home }}/etc/master-apps/_cluster/local/indexes.conf"
    section: "{{ item.0.indexName }}"
    option: "{{ item.1 }}"
    value: "{{ item.0[item.1] | default('', true) }}"
  with_items: {{ index | product(['homePath', 'coldPath', 'thawedPath', 'repFactor']) | list }}

同时,这仍然需要您对需要循环的键列表进行硬编码。此外,如果您需要将某些键添加到其他索引不可用的索引中,这将很难维护。

既然你提到你最终可能会修改你的数据,我会建议以下结构,这将更容易与 subelements lookup 一起完成。这是一个想法,您可以根据自己的具体需要进行修改。

index:
  - indexName: "AAA"
    options:
      - name: homePath
        value: "$SPLUNK_DB/AAA/db"
      - name: coldPath
        value: "$SPLUNK_DB/AAA/colddb"
      - name: thawedPath
        value: "$SPLUNK_DB/AAA/thaweddb"
      - name: repFactor
        value: "auto"
  - indexName: "BBB"
    options:
      - name: homePath
        value: "$SPLUNK_DB/BBB/db"
      - name: coldPath
        value: "$SPLUNK_DB/BBB/colddb"
      - name: thawedPath
        value: "$SPLUNK_DB/BBB/thaweddb"
      - name: repFactor
        value: "auto"

然后你的任务看起来像:

- name: Write paths for the index
  ini_file:
    dest: "{{ splunk.home }}/etc/master-apps/_cluster/local/indexes.conf"
    section: "{{ item.0.indexName }}"
    option: "{{ item.1.name }}"
    value: "{{ item.1.value }}"
  loop: "{{ lookup('subelements', index, 'options') }}"