# Fetch - not working correctly

**URL:** <https://forum.ansible.com/t/fetch-not-working-correctly/43180>\
**Category:** Get Help\
**Created:** [June 5, 2025, 1:21pm UTC](https://forum.ansible.com/t/fetch-not-working-correctly/43180 "2025-06-05T13:21:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![trt757](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@trt757](https://forum.ansible.com/u/trt757)\
**Post date:** [June 5, 2025, 1:21pm UTC](https://forum.ansible.com/t/fetch-not-working-correctly/43180/1 "2025-06-05T13:21:55Z")

</div>

I’ve written a playbook with some roles and I can’t seem to get fetch working correctly. Below is the part of the play in question:

- hosts: Server1 (which I consider the controller and where I’m running this play)  
gather\_facts: false  
become: true  
roles:
  - 03-Fetch\_files

In roles: 03-Fetch\_files

- name: Fetch key file  
ansible.builtin.fetch:  
src: /etc/certificate-authority/host1.fqdn.key  
dest: /tmp  
flat: no

Here’s what’s happening. Host 1 is just a host I’m using to test. Ultimately there will be over 500 hosts I need to fetch key (and csr) files from.  
If I run this play with an entry for Server1 in hosts file, and run the play on Server 1, it’s pulling the key file from host1, but it’s creating a directory, called Server1/etc/certificate-authority/host1.fqdn.key in /tmp (which is just for testing right now). That’s no good. Again, since ultimately I’m going to need to fetch over 1000 files (key and csr) and need them in directories named for the host.

If I run this play from host1, with the entry in “hosts” changed to host1, then it creates a directory called “Server1/etc/certificate-authority/host1.fqdn.key”.

I’m trying to fetch keys to the controller, Server1, from host1, and want the dir structure to look like: host1/host1.fqdn.key.

I also can’t figure out why it’s picking up the “/etc/certificate-authority” and placing that name as part of the dir structure.

Can anyone help untangle my brain?

---

<div class="post-metadata">

**Author:** ![trt757](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@trt757](https://forum.ansible.com/u/trt757)\
**Post date:** [June 5, 2025, 1:33pm UTC](https://forum.ansible.com/t/fetch-not-working-correctly/43180/2 "2025-06-05T13:33:59Z")

</div>

I’ve also tried changing src in the role to:

src: /etc/certificate-authority/“{{ ws\_hostname }}.fqdn.key” but then I get "file doesn’t exist errors, although the key is there.

ws\_hostname is defined in vars as inventory\_hostname\_short

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [June 6, 2025, 1:58am UTC](https://forum.ansible.com/t/fetch-not-working-correctly/43180/3 "2025-06-06T01:58:39Z")

</div>

> [@trt757](#):
>
> If I run this play with an entry for Server1 in hosts file, and run the play on Server 1, it’s pulling the key file from host1,

I don’t think it is. As written above, no `host1` is involved. It’s running with `Server1` as both the Ansible controller and the target host. It’s fetching from the controller to the controller.

> [@trt757](#):
>
> it’s creating a directory, called Server1/etc/certificate-authority/host1.fqdn.key in /tmp

That’s correct, because it’s fetching the file from `Server1`.

> [@trt757](#):
>
> I also can’t figure out why it’s picking up the “/etc/certificate-authority” and placing that name as part of the dir structure.

That’s because you have `flat: no`. Change that to `flat: true` to eliminate the intermediate directory levels in the fetch destination.

> [@trt757](#):
>
> I’m trying to fetch keys to the controller, Server1, from host1, and want the dir structure to look like: host1/host1.fqdn.key.

Then you want something like this:

```yaml
- name: Fun with ansible.builtin.fetch
  hosts: host1,host2,host3 # Hosts and/or host groups to fetch _from_, NOT your controller (necessarily)
  gather_facts: false
  become: true
  tasks: # This task could be in a role. Trying to keep it simple.
    - name: Fetch key files from target hosts
      ansible.builtin.fetch:
        src: /etc/certificate-authority/{{ inventory_hostname }}.fqdn.key
        dest: /tmp/{{ inventory_hostname }}/ # This will create host directories
        flat: true # This avoids creating the intermediate directories

```

(BTW, see [this post in its _raw_ format](https://forum.ansible.com/raw/43180/3) to see how to get your `yaml` to look good in your posts.)

---

<div class="post-metadata">

**Author:** ![trt757](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@trt757](https://forum.ansible.com/u/trt757)\
**Post date:** [June 6, 2025, 12:00pm UTC](https://forum.ansible.com/t/fetch-not-working-correctly/43180/4 "2025-06-06T12:00:30Z")

</div>

Thank you. This works just the way I need it to, and even better, I understand Fetch now.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 6, 2025, 12:00pm UTC](https://forum.ansible.com/t/fetch-not-working-correctly/43180/5 "2025-07-06T12:00:35Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
