Score:2

Ansible fetch from various hosts various files

va flag

I am newbie to ansible. My goal is to fetch files from various servers. Each server has different path where a file is stored. Destination path is always the same.

I have the following:

- name: fetch files
  hosts: hosts
  tasks:
    - name: fetch files
      fetch:
       src: /home/ubuntu/test1/testing1.txt
       dest: /home/ubuntu/
       flat: yes
       when: inventory_hostname == "ansible1"

    - name: fetch files2
      fetch:
       src: /home/ubuntu/test2/testing2.txt
       dest: /home/ubuntu/
       flat: yes
       when: inventory_hostname == "ansible2"

My inventory file is:

[hosts]
ansible1
ansible2

When I execute:

ansible-playbook fetch.yml -i inventory.txt

The output is following:

PLAY [fetch files] ****************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [ansible2]
ok: [ansible1]

TASK [fetch files] ****************************************************************************************************************************************************
ok: [ansible1]
fatal: [ansible2]: FAILED! => {"changed": false, "msg": "file not found: /home/ubuntu/test1/testing1.txt"}

TASK [fetch files2] ***************************************************************************************************************************************************
fatal: [ansible1]: FAILED! => {"changed": false, "msg": "file not found: /home/ubuntu/test2/testing2.txt"}

PLAY RECAP ************************************************************************************************************************************************************
ansible1                   : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
ansible2                   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Why it tried to run it for other hosts when the condition is specific for one? Second question - why for the second task "fetch file2" it actually doesnt fetch it at all, although the file is present on the remote machine?

I am using for the test and development multipass and ubuntu. SSH keys are imported on the target machines.

Thanks in advance for help :)

Score:0
in flag

Your indentation is wrong. when needs to be at the same level as fetch, not below it.

    - name: fetch files
      fetch:
        src: /home/ubuntu/test1/testing1.txt
        dest: /home/ubuntu/
        flat: yes
      when: inventory_hostname == "ansible1"

The missing indendation results in both tasks being run on both hosts. Since the first task fails on the host ansible2 because the src path doesn't exist there, Ansible ends the playbook for this host and no further tasks are executed for it. The second task then fails on the remaining host, again, because the src path doesn't exist there.


But you could make this much easier by saving the src path as a host variable and reduce it to a single task

host_vars/ansible1.yml

src_path: /home/ubuntu/test1/testing1.txt

host_vars/ansible2.yml

src_path: /home/ubuntu/test2/testing2.txt

playbook.yml

- name: fetch files
  hosts: hosts
  tasks:
    - name: fetch files
      fetch:
        src: "{{ src_path }}"
        dest: /home/ubuntu/
        flat: yes
strom avatar
va flag
Same here, thanks a lot for help.
Score:0
br flag

Create a dictionary in vars. For example,

shel> cat group_vars/all/testing.yml
testing_paths:
  ansible1: /home/ubuntu/test1/testing1.txt
  ansible2: /home/ubuntu/test2/testing2.txt

and use it for the task

    - fetch:
        src: "{{ testing_paths[inventory_hostname] }}"
        dest: /home/ubuntu/
        flat: yes
strom avatar
va flag
Thanks a lot for showing me the direction :) it works
strom avatar
va flag
Hi, I added more hosts to the list testing_paths group_vars with different sources. Some of these hosts are in the list twice with different paths. But ansible complains about it WARNING]: While constructing a mapping from /home/ubuntu/ansible/group_vars/all/testing.yml, line 2, column 3, found a duplicate dict key (ansible3). Using last defined value only. And others is it possible to have same hosts multiple times with different sources in one group_vars list?
br flag
Which one would you like to use? (*"Some of these hosts are in the list twice with different paths."*).
strom avatar
va flag
Lets say, ansible2 contains two paths of files I need to fetch: `testing_paths: ansible1: /home/ubuntu/test1/testing1.txt ansible2: /home/ubuntu/test2/testing2.txt ansible2: /home/somewhereelse/testing_of_fetch/testing2.txt`
br flag
You can have lists of the paths `testing_paths: ansible1: [path1] ansible2: [path1, path2]`. Open a new question.
strom avatar
va flag
Awesome, thanks!!!!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.