Ansible:从每个主机的寄存器减少到变量
Ansible: reduce to the variable from registers of each host
---
- hosts: all
tasks:
- name: Read the file
command: cat the_file
register: the_file
- name: Reduce them
set_fact:
result: "{{ the_file.stdout }}"
reduce: yes # magic option
当the_file在每个主机中分别写成"a","b","c"时,是否可以得到像["a", "b", "c"]
或"abc"
这样的变量?
主机之间不是通过 ssh 连接的,而是仅来自 playbook 运行 一个。
extract 来自 hostvars
的变量
- set_fact:
result: "{{ ansible_play_hosts_all|
map('extract', hostvars, 'the_file')|list }}"
run_once: true
给予
result: [a, b, c]
如果你愿意,你可以加入项目
- set_fact:
result: "{{ ansible_play_hosts_all|
map('extract', hostvars, 'the_file')|join }}"
run_once: true
给予
result: abc
请参阅 Special Variables 并根据您的需要调整主机列表。
---
- hosts: all
tasks:
- name: Read the file
command: cat the_file
register: the_file
- name: Reduce them
set_fact:
result: "{{ the_file.stdout }}"
reduce: yes # magic option
当the_file在每个主机中分别写成"a","b","c"时,是否可以得到像["a", "b", "c"]
或"abc"
这样的变量?
主机之间不是通过 ssh 连接的,而是仅来自 playbook 运行 一个。
extract 来自 hostvars
的变量 - set_fact:
result: "{{ ansible_play_hosts_all|
map('extract', hostvars, 'the_file')|list }}"
run_once: true
给予
result: [a, b, c]
如果你愿意,你可以加入项目
- set_fact:
result: "{{ ansible_play_hosts_all|
map('extract', hostvars, 'the_file')|join }}"
run_once: true
给予
result: abc
请参阅 Special Variables 并根据您的需要调整主机列表。