What does the fetch module do, and where does a fetched file end up?
fetch is the reverse of copy: it pulls a file from the managed host to the control node. By default it saves it under dest/<hostname>/<full remote path>, so files from many hosts don't overwrite each other.
- name: Transfer the access.log file
ansible.builtin.fetch:
src: /var/log/apache2/access.log
dest: /tmp/access.log
* fetch builds one subtree per host, so logs from many servers never collide. *
The surprise is the path. With dest: /tmp/access.log, the result is not a file /tmp/access.log but:
/tmp/access.log/debian-001.example/var/log/apache2/access.log
Ansible treats dest as a directory, adds the inventory hostname, then the full source path. That is deliberate: run against 20 web servers, you get 20 separate logs instead of one file overwritten 19 times. If you fetch from a single host and want an exact file name, set flat: yes (or end dest with / to keep only the basename).
Typical uses: collecting logs, configs or reports from many hosts for review on one machine.
Go deeper:
fetch module (official docs) โ
destlayout,flat,fail_on_missingand examples.