60292

Ansible - Run Task Against Hosts in a with_together Fashion

Question:

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] }}"

Currently, it does both operations for both servers, resulting in 4 operations.

I need it to synchronize like so:

<ul><li>copy /tmp/remote/source/one/ from remoteSrc1 to /tmp/remote/dest/one/ on remoteDest1 </li> <li>copy /tmp/remote/source/two/ from remoteSrc2 to /tmp/remote/dest/two/ on remoteDest2</li> </ul>

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.

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.

Answer1:

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]

To get:

- 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]

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.

Recommend

  • Finding a string within an stdout_lines array
  • Using register variable to store values for multi host play
  • Ansible: apply when to complete loop
  • How to have a condition for with-items as a whole, not for the individual items
  • Assertion error on mininet-test experiment
  • Using Ansible, how can I install PythonBrew system-wide?
  • Issue with running .bash_profile by ansible
  • How to resolve the dependency and compile the Junit test classes using Gradle Bulid Tool
  • Image loaded from Isolated storage are not binding at all
  • 'Could not find us.bpsm:edn-java:0.4.3' error with Gradle for Clojure (Clojuresque)
  • How do you disable Android Library Update?
  • Task behaviour differs from Task behaviour when OperationCanceledException thrown
  • Nested Tree View in Odoo v8
  • Typical tasks/problems to demonstrate differences between programming languages
  • How to link a work load item with a commited change?
  • MSBuild to run unit tests
  • How to fix: The return type of an async method must be void, Task or Task [AppName]
  • multiprocessing freeze computer
  • Is it really posible to close a PhoneGap App?
  • How can you Call a method from a diffrent Project, both in C++?
  • Making mono cross platform support for Task/Intent
  • Error:Execution failed for task ':app:dexDebug'. when importing ParseLoginUI as a module
  • Ansible: setting user on dynamic ec2
  • How to issue a command that produces infinite output and return immediately
  • Regex for incomplete lines within known start and end strings
  • Attemping to change attributes of $(this) inside success function
  • Autohotkey script running program with command line arguments
  • get all processes in parallel
  • Accessing Rows In A LINQ Result Without A Foreach Loop?
  • mave 3.2 not able to access local nexus instance return 502 code
  • iText RadioGroup/RadioButtons across multiple PdfPCells
  • Tomcat memory Leak
  • How to add a focus style to an editable ComboBox in WPF
  • How do I superscript characters in a UIButton?
  • Cannot resolve symbol 'MyApi'
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Can Jackson SerializationFeature be overridden per field or class?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How to push additional view controllers onto NavigationController but keep the TabBar?