0

I have an infrastructure of machines in which there is a central machine on which I have my ansible playbooks, then I have an ensamble of machines of "level 1" and another ensable 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 ensamble of machines of level 2 to their related machine of level 1.

The file has name according to the ensamble 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

  • file_machinelv1_10_machinelv2_1.csv
  • file_machinelv1_10_machinelv2_2.csv
  • file_machinelv1_10_machinelv2_3.csv ...

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?

Tms91
  • 3,456
  • 6
  • 40
  • 74
  • 1
    Why not just set a group-level variable `parent_id` or `level1_id` or something? – Richard Huxton Jun 06 '23 at 09:32
  • "_what is the problem here_", according the new error message `Failed to connect to the host via ssh: ssh: Could not resolve hostname machine-010_lv2: Temporary failure in name resolution` it seems to quite clear to be name resolution (DNS). There shouldn't be an underline within the hostname. See [Valid characters of a hostname](https://stackoverflow.com/questions/3523028/). – U880D Jun 09 '23 at 06:01
  • @U880D that is not the problem. As I wrote, 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 – Tms91 Jun 09 '23 at 07:00
  • Note: by following the trhead indicated as duplicate of this answer, I could not solve the problem. I have updated the question. – Tms91 Jun 09 '23 at 09:58

1 Answers1

0

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

Tms91
  • 3,456
  • 6
  • 40
  • 74
  • Yet I do not consider this answer to a valid one to this thread. Any other ansible-native answer is welcomed and likely to be flagged as the correct one :) – Tms91 Jun 16 '23 at 14:24