我有以下多个文件,并已使用set_fact注册到变量。
Topic test-topic-15 Partition 0 Leader 2 Replicas 2,0,1 Isr 2,0
Topic test-topic-15 Partition 1 Leader 0 Replicas 0,1,2 Isr 2,0
Topic test-topic-15 Partition 2 Leader 1 Replicas 1,2,0 Isr 1
Topic test-topic-15 Partition 3 Leader 2 Replicas 2,1,0 Isr 2,0
Topic test-topic-15 Partition 4 Leader 0 Replicas 0,2,1 Isr 2,0
Topic test-topic-15 Partition 5 Leader 1 Replicas 1,0,2 Isr 1
Topic test-topic-16 Partition 0 Leader 2 Replicas 2,0,1 Isr 2,0
Topic test-topic-16 Partition 1 Leader 0 Replicas 0,1,2 Isr 2,0
Topic test-topic-16 Partition 2 Leader 1 Replicas 1,2,0 Isr 1
Topic test-topic-16 Partition 3 Leader 2 Replicas 2,1,0 Isr 2,0
file_3 ..........
示例代码
- name: register to a 2 variable
set_fact:
contents_2: "{{ lookup('file','/tmp/all_{{ item }}') }}"
with_items:
- "{{ available_topics.stdout.split('\n') }}"
register: contents_3
- name: to a file 4
lineinfile:
line: '"topic_name": "{{ item.split()[1] }}", "partition_no": "{{ item.split()[3] }}", "leader": "{{ item.split()[5] }}"", "replicas": "{{ item.split()[7] }}"", "isr": "{{ item.split()[9] }}"'
path: "/tmp/some_file_name_{{ item.split()[1] }}"
create: yes
with_items: "{{contents_3.results|map(attribute='ansible_facts.contents_2') | list}}"
使用上述代码,我得到的输出如下,其中仅包含每个输入文件的第一行数据。
第一个文件输出
"topic_name": "test-topic-15", "partition_no": "0", "leader": "2", "replicas": "2,0,1", "isr": "2,0"
第二个文件输出
"topic_name": "test-topic-16", "partition_no": "0", "leader": "2", "replicas": "2,0,1", "isr": "2,0"
但是我希望输出如下。当我刚接触Ansible时,能否请您帮助如下所示的输出?
第一个文件输出
"topic_name": "test-topic-15", "partition_no": "0", "leader": "2", "replicas": "2,0,1", "isr": "2,0"
"topic_name": "test-topic-15", "partition_no": "1", "leader": "0", "replicas": "0,1,2", "isr": "2,0"
"topic_name": "test-topic-15", "partition_no": "2", "leader": "1", "replicas": "1,2,0", "isr": "1"
......
第二个文件输出
"topic_name": "test-topic-16", "partition_no": "0", "leader": "2", "replicas": "2,0,1", "isr": "2,0"
"topic_name": "test-topic-16", "partition_no": "1", "leader": "0", "replicas": "0,1,2", "isr": "2,0"
.....