Ansible fetch does not overwrite target

I have created a task to do a backup of my Mikrotik switches every morning, and copy the backup file to a shared folder on my Ansible server (Debian 12).

Ansible are installed from apt:
ansible: 7.7.0+dfsg-3+deb12u1
ansible-core: 2.14.18-0+deb12u2

My playbook:

- name: Backup all RouterOS switches
  hosts: routeros
  gather_facts: no
  remote_user: ansible

  tasks:

  - name: Run backup and check to see if output says its saved
    community.routeros.command:
      commands: /system/backup/save dont-encrypt=yes name=/auto
      retries: 1
      wait_for: result[0] contains Configuration backup saved

  - name: Retrieve the backup file from RouterOS
    ansible.builtin.fetch:
      src: auto.backup
      dest: /mnt/backup/routeros/{{ inventory_hostname }}.backup
      flat: true

The backup file are created successfully on the Mikrotik device.
But if the file already exists in the backup dir on the Ansible server, it is not replaced.
The fetch job works 100% if I delete the target files before I run the playbook.
There is no overwrite option in the fetch module, so is there another module I could try?

I do not want to delete the backup file before I know that it will be replaced.
As a developer I could fix this with a few lines in a bash script, but I would like for it to work in a playbook.

@bobbysock Welcome to the Ansible Forum.

What you are doing looks correct, I wonder if there is a bug with fetch on systems that might be able to generate the checksum

Can you run with ansible-playbook -vvv and reply with the verbose output for the Retrieve the backup file from RouterOS step.

Or you could try using ansible.netcommon.net_get module

Based on the description of fetch I’d expect this to work.

The version of ansible-core you’re running is past its EOL date, and likely doesn’t have up-to-date versions of the modules.

I’d suggest (at least in a virtual environment) installing an up-to-date version following the Installing Ansible with pip (or Installing Ansbile with pipx if you prefer) guide and see if the issue persist.

1 Like

@Lyle_McKarns Good spot, I totally missed the very old versions.

@bobbysock In case you need them, here are the porting guides:

1 Like

I’m trying to install PIP (which I do not like), and will see if I can upgrade the module to something that works.
As a 60 year old programmer based on Pascal, C+ and other common languages, I will say I do hate containers. And by hate, I mean HATE! “apt purge docker*”
So I hope there is some way for me to use this without those technologies.
I can live with pip, it’s not that bad, just.. well.. bad.

What did pip ever do to you?

i suspect it is not just pip, but ‘language based package managers’ … some of us prefer the system package managers when it comes to host installations, for many reasons, specially if we are/were sysadmins.

2 Likes

Maybe. Things can get complicated when you need something that isn’t packaged up by the OS’s package manager though. Alas, not everything can be as elegant as “C+”.

As @Lyle_McKarns suggested above, pipx is nicer than pip for installing Ansible.

ansible -m flame_war -a 'state=started msg=And there is also uv' all

2 Likes