如何在Ansible中重命名文件

问题描述

如果存在任何文件,我需要将hosts_example或hosts_Example重命名为hosts_real

- name: Playbook to Standardize  Hosts
  hosts: test
  vars:
    destpath: /etc/hosts_real
    filename: [ /etc/hosts_example,/etc/hosts_Example ]

  tasks:
    - name: Check if file exists
      stat:
        path: "{{ item }}"
      with_items:
        - "{{ filename }}"
      register: check_file_name

    - debug:
        msg: "{{check_file_name}}"

    - name: Rename file
      command: mv "{{ item }}"{{destpath}}"
      with_items:
        - "{{ check_file_name.results }}"
      when: item.stat.exists == true

我尝试这会出错并且无法达到预期的结果

解决方法

名称:替换文件

shell:mv /local/oracle/12.2/oldora.ora /local/oracle/12.2/tnsnames.ora

成为:appuser

为我们工作。在shell命令中更新您的路径。我认为文件模块没有重命名命令。如果您的shell行中包含变量,则将整个行用引号引起来,例如:

shell:“ MV {{path_1}} {{path_2}}”