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