首页 > 解决方案 > ansible循环列表

问题描述

我有一个库存(可以更改):

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) }}" }

这可能吗?

标签: ansiblesplunk

解决方案


是的。这是可能的。使用子元素。首先准备好数据的结构。例如

    - 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

推荐阅读