英文:
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 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.
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?
英文:
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.
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?
答案1
得分: 0
我用一个非本地的ansible解决方案解决了这个问题:
- hosts: machine_lv2 # 所有二级机器的组
gather_facts: no
vars_files:
- vars/main.yml
tasks:
- name: 从machine_lv2同步CSV到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
其中 <machine_lv1_aliasname>
是每个 machine_lv2 中 /etc/hosts 文件中映射的相关 machine_lv1 的 IP 地址。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论