ansible - 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) }}" }
这可能吗?
解决方案
是的。这是可能的。使用子元素。首先准备好数据的结构。例如
- 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
推荐阅读
- git - 无法从 Git 克隆存储库
- reactjs - 从 React 中的节点获取文本内容
- python - 如何在通过求解另一个 Piecewise 函数获得的 Piecewise 函数中对 ExprCondPair 进行排序?
- python - 从 Python 中带有偏差的列表中选择一个随机样本
- javascript - 快速排序算法javascript
- python - TypeError:强制转换为 Unicode:需要字符串或缓冲区,找到 sqlite3.Cursor
- php - PHP 连接到 Redis
- python - 如何在不知道我在 Python 中查找的对象名称的情况下解析 JSON 文件?
- c - 不带括号的 C 函数调用
- angularjs - 为什么模板没有在测试中编译?