Ansible - 以 with_together 方式针对主机运行任务 [英] Ansible - Run Task Against Hosts in a with_together Fashion

查看:30
本文介绍了Ansible - 以 with_together 方式针对主机运行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用两个主机并将它们动态添加到一个组中,然后是一个 synchronize 任务,使用 with_together 并行使用 2 个元素的 3 个列表来复制两个远程服务器之间的指定文件.

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.

我需要它像这样同步:

  • /tmp/remote/source/one/remoteSrc1 复制到 上的 /tmp/remote/dest/one/remoteDest1
  • /tmp/remote/source/two/remoteSrc2 复制到 上的 /tmp/remote/dest/two/remoteDest2
  • copy /tmp/remote/source/one/ from remoteSrc1 to /tmp/remote/dest/one/ on remoteDest1
  • copy /tmp/remote/source/two/ from remoteSrc2 to /tmp/remote/dest/two/ on remoteDest2

这意味着它是 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]

本质上,通过将主机添加为列表,它们可以在 when 语句中使用,仅当当前主机 (inventory_hostname) 匹配主机时才执行任务当前在列表中被索引.

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 - 以 with_together 方式针对主机运行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆