使用原生 Ansible 函数重命名文件

使用原生 Ansible 函数重命名文件

我需要识别目录中的一组文件,并将所有文件重命名为 .repo 扩展名。是否可以使用原生 Ansible 函数来执行此操作?如果不可能,我将使用纯硬 shell 命令。

答案1

至于这个答案,ansible 不支持移动/重命名功能:https://stackoverflow.com/questions/24162996/how-to-move-rename-a-file-using-an-ansible-task-on-a-remote-system

如果你对 fileglob 很谨慎,你可以像这样不使用 shell 来解决问题:

---
- 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"

相关内容