请您帮我解决这个问题: 我有一个包含多个任务的剧本,每个任务都包含用于任务输出的循环和寄存器。最后一个任务将使用 lineinfile 根据之前的寄存器创建 csv 报告。如下所示:
- name: information
module:
xxxx: xxxx
xxxx: xxxxx
loop:
- xxxx
- xxxx
register: task1_info
- name: information
module:
xxxx: xxxx
xxxx: xxxxx
loop:
- xxxx
- xxxx
register: task2_info
- name: information
lineinfile:
path: xxxx
line: "{{ item.tags.Name }}, {{ item.lastName }}"
loop:
- task1_info.results
- task2_info.results
如果我最后只使用一个寄存器,它就可以工作,但不会循环所有寄存器。另一种选择是在每次注册后编写一个任务,我认为这不合理!
最佳答案
我了解您的用例,您喜欢将一个列表附加到另一个列表或合并两个列表。
为此,您可以使用类似的方法
---
- hosts: localhost
become: false
gather_facts: false
vars:
LIST_1:
- 1
- 2
- 3
LIST_2:
- A
- B
- C
tasks:
- name: Info
debug:
msg: "{{ item }}"
loop: "{{ LIST_1 + LIST_2 }}"
loop_control:
extended: true
label: "{{ansible_loop.index0 }}"
导致输出
TASK [Info] ******************
ok: [localhost] => (item=0) =>
msg: 1
ok: [localhost] => (item=1) =>
msg: 2
ok: [localhost] => (item=2) =>
msg: 3
ok: [localhost] => (item=3) =>
msg: A
ok: [localhost] => (item=4) =>
msg: B
ok: [localhost] => (item=5) =>
msg: C
致谢
进一步问答
关于automation - 具有多个寄存器值的 Ansible 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71624029/