Ansible Pilot

Rename file or directory - Ansible module copy and file

How to rename a file or directory "foo", checking the file existence, and performing the verification in a live demo and some simple Ansible code.

September 24, 2021
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

How to rename a file or directory using an Ansible task on a remote system?

I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible rename file/directory

First of all let me demystify that I’d like to propose a solution using only Ansible native modules, so no shell module to invoke the Unix utility mv. Today we’re talking about Ansible two modules copy and file The full names are ansible.builtin.copy and ansible.builtin.file which means are part of the collection of modules “builtin” with ansible and shipped with it. Both are these modules are pretty stable and out for years. The purpose of the copy module is to copy files to remote locations. Once the file is successfully copied we could use the module file to delete the source file.

Parameters

Module copy

Module file

The parameter list is pretty wide but I’ll summarize the most useful. The only required parameter is “dest” which specifies the destination path. The “src” specifies the source file presumed in the controller host. It could be a relative or absolute path. From version 2.0, in the copy module, you can use the “remote_src” parameter. If True it will search the file in the remote/target machine for the src. From version 2.8 copy module remote_src supports recursive copying. The only required is “path”, where you specify the filesystem path of the file you’re going to edit. The state defines the type of object we are modifying, the default is “file” but we could also handle directories, hardlink, symlink, or only update the access time with the “touch” option. For our use case, we are going to use the “absent” option.

Demo

Let’s jump in a real-life playbook to rename files or directories with Ansible

code

---
- name: rename file or directory
  hosts: all
  vars:
    mysrc: "~/foo"
    mydst: "~/bar"
  tasks:
    - name: Check if file exists
      ansible.builtin.stat:
        path: "{{ mysrc }}"
      register: check_file_name
    - name: print debug
      ansible.builtin.debug:
        var: check_file_name
    - name: Copy file with new name
      ansible.builtin.copy:
        remote_src: true
        src: "{{ mysrc }}"
        dest: "{{ mydst }}"
      when: check_file_name.stat.exists
    - name: Remove old file
      ansible.builtin.file:
        path: "{{ mysrc }}"
        state: absent
      when: check_file_name.stat.exists

execution

$ ansible-playbook -i demo/inventory rename/file.yml
PLAY [rename file or directory] *****************************************************************************
TASK [Gathering Facts] **************************************************************************************
ok: [demo.example.com]
TASK [Check if file exists] *********************************************************************************
ok: [demo.example.com]
TASK [print debug] ******************************************************************************************
ok: [demo.example.com] => {
    "check_file_name": {
        "changed": false,
        "failed": false,
        "stat": {
            "exists": false
        }
    }
}
TASK [Copy file with new name] ******************************************************************************
skipping: [demo.example.com]
TASK [Remove old file] **************************************************************************************
skipping: [demo.example.com]
PLAY RECAP **************************************************************************************************
demo.example.com           : ok=3    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0
$ ansible-playbook -i demo/inventory rename/file.yml
PLAY [rename file or directory] *****************************************************************************
TASK [Gathering Facts] **************************************************************************************
ok: [demo.example.com]
TASK [Check if file exists] *********************************************************************************
ok: [demo.example.com]
TASK [print debug] ******************************************************************************************
ok: [demo.example.com] => {
    "check_file_name": {
        "changed": false,
        "failed": false,
        "stat": {
            "atime": 1632319723.592112,
            "attr_flags": "",
            "attributes": [],
            "block_size": 4096,
            "blocks": 0,
            "charset": "binary",
            "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
            "ctime": 1632319723.592112,
            "dev": 64768,
            "device_type": 0,
            "executable": false,
            "exists": true,
            "gid": 10,
            "gr_name": "wheel",
            "inode": 134764246,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mimetype": "inode/x-empty",
            "mode": "0644",
            "mtime": 1632319723.592112,
            "nlink": 1,
            "path": "/home/devops/foo",
            "pw_name": "devops",
            "readable": true,
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 0,
            "uid": 1001,
            "version": "1745015340",
            "wgrp": false,
            "woth": false,
            "writeable": true,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        }
    }
}
TASK [Copy file with new name] ******************************************************************************
changed: [demo.example.com]
TASK [Remove old file] **************************************************************************************
changed: [demo.example.com]
PLAY RECAP **************************************************************************************************
demo.example.com           : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
$ ansible-playbook -i demo/inventory rename/file.yml
PLAY [rename file or directory] *****************************************************************************
TASK [Gathering Facts] **************************************************************************************
ok: [demo.example.com]
TASK [Check if file exists] *********************************************************************************
ok: [demo.example.com]
TASK [print debug] ******************************************************************************************
ok: [demo.example.com] => {
    "check_file_name": {
        "changed": false,
        "failed": false,
        "stat": {
            "atime": 1632319779.5021598,
            "attr_flags": "",
            "attributes": [],
            "block_size": 4096,
            "blocks": 0,
            "charset": "binary",
            "ctime": 1632319779.5021598,
            "dev": 64768,
            "device_type": 0,
            "executable": true,
            "exists": true,
            "gid": 10,
            "gr_name": "wheel",
            "inode": 898820,
            "isblk": false,
            "ischr": false,
            "isdir": true,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": false,
            "issock": false,
            "isuid": false,
            "mimetype": "inode/directory",
            "mode": "0755",
            "mtime": 1632319779.5021598,
            "nlink": 2,
            "path": "/home/devops/foo",
            "pw_name": "devops",
            "readable": true,
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 6,
            "uid": 1001,
            "version": "1317302979",
            "wgrp": false,
            "woth": false,
            "writeable": true,
            "wusr": true,
            "xgrp": true,
            "xoth": true,
            "xusr": true
        }
    }
}
TASK [Copy file with new name] ******************************************************************************
ok: [demo.example.com]
TASK [Remove old file] **************************************************************************************
changed: [demo.example.com]
PLAY RECAP **************************************************************************************************
demo.example.com           : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

code with ❤️ in GitHub

Recap

Now you know how to rename file or directory with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases