Ansible-以共同的方式针对主机运行任务 [英] Ansible - Run Task Against Hosts in a with_together Fashion
问题描述
我目前正在获取两台主机,并将它们动态添加到一个组中,然后执行synchronize
任务,使用with_together
并行使用3个2个元素的列表在两个远程服务器之间复制指定的文件.
I am currently taking two hosts and dynamically adding them to a group, followed by a synchronize
task using with_together
to use 3 lists of 2 elements in parallel to copy the specified files between two remote servers.
这是一个基于此思想的示例:
Here's an example based on the idea:
---
- name: Configure Hosts for Copying
hosts: localhost
gather_facts: no
tasks:
- name: Adding given hosts to new group...
add_host:
name: "{{ item }}"
groups: copy_group
with_items:
- ["remoteDest1", "remoteDest2"]
- name: Copy Files between servers
hosts: copy_group
gather_facts: no
tasks:
- name: Copying files...
synchronize:
src: "{{ item[1] }}"
dest: "{{ item[2] }}"
with_together:
- ["remoteSrc1", "remoteSrc2"]
- ["/tmp/remote/source/one/", "/tmp/remote/source/two/"]
- ["/tmp/remote/dest/one/", "/tmp/remote/dest/two/"]
delegate_to: "{{ item[0] }}"
当前,它对两台服务器都执行这两项操作,导致进行了4次操作.
Currently, it does both operations for both servers, resulting in 4 operations.
我需要它像这样进行同步:
I need it to synchronize like so:
- 将
/tmp/remote/source/one/
从remoteSrc1
复制到remoteDest1
上的/tmp/remote/dest/one/
- 将
/tmp/remote/source/two/
从remoteSrc2
复制到remoteDest2
上的/tmp/remote/dest/two/
- copy
/tmp/remote/source/one/
fromremoteSrc1
to/tmp/remote/dest/one/
onremoteDest1
- copy
/tmp/remote/source/two/
fromremoteSrc2
to/tmp/remote/dest/two/
onremoteDest2
这将意味着这是1:1的比率;基本上以与with_together
处理列表相同的方式作用于主机.
Which would mean it's a 1:1 ratio; essentially acting on the hosts in the same manner as with_together
does for the lists.
主机是动态获得的,所以我不能只为每台主机做不同的游戏.
The hosts are obtained dynamically, so I can't just make a different play for each host.
由于synchronize
本质上是rsync
的简化版本,所以如果有直接使用rsync
的简单解决方案,那么将不胜感激.
Since synchronize
is essentially simplified version of rsync
, then if there's a simple solution for this using rsync
directly, then it would be much appreciated.
推荐答案
没有本机功能,因此这是我解决的方法:
There isn't native functionality for this, so this is how I solved it:
鉴于原始任务,请添加以下两行:
Given the original task, add the following two lines:
- "{{ groups['copy_group'] }}"
when: inventory_hostname == item[3]
获取:
- name: Copying files...
synchronize:
src: "{{ item[1] }}"
dest: "{{ item[2] }}"
with_together:
- ["remoteSrc1", "remoteSrc2"]
- ["/tmp/remote/source/one/", "/tmp/remote/source/two/"]
- ["/tmp/remote/dest/one/", "/tmp/remote/dest/two/"]
- "{{ groups['copy_group'] }}"
delegate_to: "{{ item[0] }}"
when: inventory_hostname == item[3]
从本质上讲,通过将主机添加为列表,仅当当前主机(inventory_hostname
)与列表中当前正在索引的主机匹配时,它们才能在when
语句中用于执行任务.
Essentially, by adding the hosts as a list, they can be used in the when
statement to execute the task only when the current host (inventory_hostname
) matches the host currently being indexed in the list.
结果是,该播放只与每个具有相同索引的其他列表项以串行方式针对每个主机运行一次.
The result is that the play only runs against each host once in a serial manner with the other list items that have the same index.
这篇关于Ansible-以共同的方式针对主机运行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!