ansible

Is there a way to copy a file from a host to another one, being the machine on which the playbooks run none of the two?


I have an infrastructure of machines in which there is a central machine on which I have my ansible playbooks, then I have an ensemble of machines of "level 1" and another ensemble of machines of "level 2".

The central machine is linked to many machines of level 1, a machine of level 1 is linked to many machines of level 2.

enter image description here

the hostname of machine of level 1 is like:

machine-010    (one host)

the hostname of the group of all its related machines of level 2 is like:

machine-010_lv2    (many hosts)

I would need to copy a file from an ensemble of machines of level 2 to their related machine of level 1.

The file has name according to the ensemble of machines on which it lies:

file_machinelv1_{{ id_machine_level1 }}_machinelv2_{{ id_machine_level2 }}.csv

e.g.

file_machinelv1_10_machinelv2_3.csv

Is it possible to do it with an ansible task without passing by the machine on which the ansible playbook lies?

e.g. the machine id=10 of level1 should get a file from each machine of level2 linked to it, so it should get files like

Update

By reading this thread, I found out that I can use the ansible.posix.synchronize module to copy files from one node to another, being the machine on which lies the running Ansible script none of the two.

By following the example given in the thread, I have written this task:

-hosts: machine-010

  gather_facts: no

  vars_files:
    - vars/main.yml

  tasks:
  
    - name: synchronize CSV from machine-010_lv2 to machine-010
      ansible.posix.synchronize:
        src: "{{ log_base_path }}/file_machinelv1_{{ id_machine_level1 }}_machinelv2_{{ id_machine_level2 }}.csv"
        dest: "{{ log_base_path }}/"
      delegate_to: machine-010_lv2

But as I run my playbook, I get this error:

[WARNING]: Unhandled error in Python interpreter discovery for host machine-010:
Failed to connect to the host via ssh: ssh: Could not resolve hostname
machine-010_lv2: Temporary failure in name resolution
  machine-010 unreachable | msg: Data could not be sent to remote host "machine-010_lv2". Make sure this host can be reached over ssh: ssh: Could not resolve hostname machine-010_lv2: Temporary failure in name resolution

however, If I run from the terminal of my central machine

ansible machine-010:machine-010_lv2 -o -b -f50 -m shell -a 'echo "Hello. I am ready!"' | sort

I get response from all the machines.

So what is the problem here, and how can I solve it?


Solution

  • I solved with a non-native ansible workaround:

    - hosts: machine_lv2  # group of all the machines of level 2
    
      gather_facts: no
    
      vars_files:
        - vars/main.yml
    
      tasks:
    
        - name: rsync CSV from machine_lv2 to machine_lv1
          ansible.builtin.shell:
            cmd: |
              rsync {{ log_base_path }}/file_machinelv1_{{ id_machine_level1 }}_machinelv2_*.csv <machine_lv1_aliasname>:{{ log_base_path }}
          ignore_errors: true
    

    where <machine_lv1_aliasname> is the ip of the related machine_lv1 mapped in the /etc/hosts file of every machine_lv2