2

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 :)

2 Answers 2

0

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
5
  • Thanks a lot for showing me the direction :) it works
    – strom
    Nov 5, 2022 at 7:54
  • 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?
    – strom
    Nov 7, 2022 at 10:59
  • Which one would you like to use? ("Some of these hosts are in the list twice with different paths."). Nov 7, 2022 at 11:01
  • 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
    – strom
    Nov 7, 2022 at 12:14
  • You can have lists of the paths testing_paths: ansible1: [path1] ansible2: [path1, path2]. Open a new question. Nov 7, 2022 at 13:35
0

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
1
  • Same here, thanks a lot for help.
    – strom
    Nov 5, 2022 at 7:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .