具有多个寄存器值的 Ansible 循环
Ansible loop with multiple register value
你能帮我解决这个问题吗:
我有一个包含多个任务的剧本,每个任务都包含循环并注册任务的输出。最后一个任务是使用 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
感谢
进一步问答
你能帮我解决这个问题吗: 我有一个包含多个任务的剧本,每个任务都包含循环并注册任务的输出。最后一个任务是使用 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
感谢
进一步问答