Rename a File with Native Ansible Functions

I need to identify a set of files in a directory and rename all those with a .repo extension. Is it possible to do it with native Ansible functions? If it's not possible I'll use a pure and hard shell command.

1 Answer

As to this answer ansible does not support move / rename functionality:

If you're careful with fileglob you can workaround not using shell like this:

---
- name: copy files to new names and remove old ones
  hosts: all
  become: yes

  tasks:
    - name: copy all .repo files in a spec. directory to new names
      copy:
        src: "{{ item }}"
        dst: whatever_you_want_me_to_be_{{ item }}.reponot
      with_fileglob: "/tmp/repofiles/*repo"

    - name: remove all .repo files in a spec. directory
      file:
        path: "{{ item }}"
        state: absent
      with_fileglob: "/tmp/repofiles/*repo"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest