【问题标题】:How to rename a file in ansible如何在ansible中重命名文件
【发布时间】:2020-09-11 11:16:46
【问题描述】:

如果存在任何文件,我需要将 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

我试过了,但出现错误,无法达到预期的结果

【问题讨论】:

  • 嗨!请在问题中包含错误消息。
  • 看起来您可能需要在 mv 命令的两个参数之间留一个空格。我认为你也有引用问题。

标签: linux ansible hostname ansible-template


【解决方案1】:

名称:替换文件

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

成为:appuser

为我们工作。在 shell 命令中更新您的路径。我不认为文件模块有重命名命令。如果您的 shell 行中有变量,请将整行放在引号中,例如:

外壳:“mv {{ path_1 }} {{ path_2 }}”

【讨论】:

    猜你喜欢
    • 2021-10-16
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2011-07-28
    • 2013-10-09
    • 2015-04-26
    • 2013-12-28
    相关资源
    最近更新 更多