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

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

问题描述

我目前正在获取两台主机,并将它们动态添加到一个组中,然后执行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/ 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]

从本质上讲,通过将主机添加为列表,仅当当前主机(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屋!

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