Find knowledge base article(s) by searching for keywords in the title e.g. type linux in the search box below
Find knowledge base article(s) by browsing the subject categories of articles
Technology quick references, cheatsheets, user manuals etc.
Shop Online through ShopifyLite
Tutorials on various IT applications.
Search Title    (UL:0 |SS:f)

Software >> Automation >> Ansible >> Modules >> file >> Examples

 

file


## DOCUMENTATION

online help: https://docs.ansible.com/ansible-core/2.11/collections/ansible/builtin/file_module.html
offline help: ansible-doc file


## EXAMPLE 1
## PLAYBOOK

---
- name: file module example 1
  hosts: all
  become: True
  gather_facts: no
  tasks:
  - name: (1) Change file ownership and permission using numeric modes
    file:
       path: /home/ansible/hello.txt
       owner: root
       group: root
       mode: '0644'
  - name: Verify changes for (1)
    shell: ls -ldh /home/ansible/hello.txt
    register: results1
  - debug: var=results1.stdout_lines
  #
  - name: (2) Change permission using symbolic modes
    file:
       path: /home/ansible/hello.txt
       mode: u=rw,g=r,o=r
  - name: Verify changes for (2)
    shell: ls -ldh /home/ansible/hello.txt
    register: results2
  - debug: var=results2.stdout_lines
  #
  - name: (3) Change permission by adding, removing modes
    file:
       path: /home/ansible/hello.txt
       mode: u+rw,g+w,o-r
  - name: Verify changes for (3)
    shell: ls -ldh /home/ansible/hello*.txt
    register: results3
  - debug: var=results3.stdout_lines
   #
  - name: (4) Create symbolic link
    file:
       src: /home/ansible/hello.txt
       dest: /home/ansible/hello_symlink.txt
       owner: ansible
       group: ansible
       state: link
  - name: Verify changes for (4)
    shell: ls -ldh /home/ansible/hello*.txt
    register: results4
  - debug: var=results4.stdout_lines
   #
  - name: (5) Create directory if does not exist
    file:
       path: /home/ansible/welcome
       state: directory
       mode: '0755'
  - name: Verify changes for (5)
    shell: ls -ldh /home/ansible/welcome
    register: results5
  - debug: var=results5.stdout_lines
  #
  - name: (6) Change directory ownership recursively
    file:
       path: /home/ansible/welcome
       state: directory
       recurse: yes
       group: ansible
       owner: ansible
  - name: Verify changes for (6)
    shell: ls -laRh /home/ansible/welcome
    register: results6
  - debug: var=results6.stdout_lines
  #
  - name: (7) Change file modification time and access time
    file:
       path: /home/ansible/hello.txt
       state: file
       #modification_time: '{{ "%Y%m%d%H%M.%S" | strftime(stat_var.stat.atime) }}'
       modification_time: '202110262149.00'
       access_time: now
  - name: Verify changes for (7)
    shell: ls -lah /home/ansible/hello.txt;ls -luh /home/ansible/hello.txt
    register: results7
  - debug: var=results7.stdout_lines
  #
  - name: (8) Delete a directory recursively
    file:
       path: /home/ansible/welcome
       state: absent
  - name: Verify changes for (8)
    stat:
       path: /home/ansible/welcome
    register: result8_dir
  - debug: var=result8_dir.stat.exists
  #
  - name: (9) Delete a file
    file:
       path: /home/ansible/hello.txt
       state: absent
  - name: Verify changes for (9)
    stat:
       path: /home/ansible/hello.txt
    register: result9_file
  - debug: var=result9_file.stat.exists


## OUTPUT

PLAY [file module example 1] ****************************************************************************************************************

TASK [(1) Change file ownership and permission using numeric modes] *************************************************************************
changed: [debian11_int]
changed: [ubuntu20_int]

TASK [Verify changes for (1)] ***************************************************************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [debug] ********************************************************************************************************************************
ok: [ubuntu20_int] => {
    "results1.stdout_lines": [
        "-rw-r--r-- 1 root root 6 Oct 26 22:50 /home/ansible/hello.txt"
    ]
}
ok: [debian11_int] => {
    "results1.stdout_lines": [
        "-rw-r--r-- 1 root root 6 Oct 26 22:50 /home/ansible/hello.txt"
    ]
}

TASK [(2) Change permission using symbolic modes] *******************************************************************************************
ok: [ubuntu20_int]
ok: [debian11_int]

TASK [Verify changes for (2)] ***************************************************************************************************************
changed: [debian11_int]
changed: [ubuntu20_int]

TASK [debug] ********************************************************************************************************************************
ok: [ubuntu20_int] => {
    "results2.stdout_lines": [
        "-rw-r--r-- 1 root root 6 Oct 26 22:50 /home/ansible/hello.txt"
    ]
}
ok: [debian11_int] => {
    "results2.stdout_lines": [
        "-rw-r--r-- 1 root root 6 Oct 26 22:50 /home/ansible/hello.txt"
    ]
}

TASK [(3) Change permission by adding, removing modes] **************************************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [Verify changes for (3)] ***************************************************************************************************************
changed: [debian11_int]
changed: [ubuntu20_int]

TASK [debug] ********************************************************************************************************************************
ok: [debian11_int] => {
    "results3.stdout_lines": [
        "lrwxrwxrwx 1 root root 23 Oct 26 21:30 /home/ansible/hello_symlink.txt -> /home/ansible/hello.txt",
        "-rw-rw---- 1 root root  6 Oct 26 22:50 /home/ansible/hello.txt"
    ]
}
ok: [ubuntu20_int] => {
    "results3.stdout_lines": [
        "lrwxrwxrwx 1 root root 23 Oct 26 21:30 /home/ansible/hello_symlink.txt -> /home/ansible/hello.txt",
        "-rw-rw---- 1 root root  6 Oct 26 22:50 /home/ansible/hello.txt"
    ]
}

TASK [(4) Create symbolic link] *************************************************************************************************************
changed: [debian11_int]
changed: [ubuntu20_int]

TASK [Verify changes for (4)] ***************************************************************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [debug] ********************************************************************************************************************************
ok: [ubuntu20_int] => {
    "results4.stdout_lines": [
        "lrwxrwxrwx 1 root    root    23 Oct 26 21:30 /home/ansible/hello_symlink.txt -> /home/ansible/hello.txt",
        "-rw-rw---- 1 ansible ansible  6 Oct 26 22:50 /home/ansible/hello.txt"
    ]
}
ok: [debian11_int] => {
    "results4.stdout_lines": [
        "lrwxrwxrwx 1 root    root    23 Oct 26 21:30 /home/ansible/hello_symlink.txt -> /home/ansible/hello.txt",
        "-rw-rw---- 1 ansible ansible  6 Oct 26 22:50 /home/ansible/hello.txt"
    ]
}

TASK [(5) Create directory if does not exist] ***********************************************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [Verify changes for (5)] ***************************************************************************************************************
changed: [debian11_int]
changed: [ubuntu20_int]

TASK [debug] ********************************************************************************************************************************
ok: [debian11_int] => {
    "results5.stdout_lines": [
        "drwxr-xr-x 2 root root 4.0K Oct 26 22:51 /home/ansible/welcome"
    ]
}
ok: [ubuntu20_int] => {
    "results5.stdout_lines": [
        "drwxr-xr-x 2 root root 4.0K Oct 26 22:51 /home/ansible/welcome"
    ]
}

TASK [(6) Change directory ownership recursively] *******************************************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [Verify changes for (6)] ***************************************************************************************************************
changed: [debian11_int]
changed: [ubuntu20_int]

TASK [debug] ********************************************************************************************************************************
ok: [debian11_int] => {
    "results6.stdout_lines": [
        "/home/ansible/welcome:",
        "total 8.0K",
        "drwxr-xr-x 2 ansible ansible 4.0K Oct 26 22:51 .",
        "drwxr-xr-x 8 ansible ansible 4.0K Oct 26 22:51 .."
    ]
}
ok: [ubuntu20_int] => {
    "results6.stdout_lines": [
        "/home/ansible/welcome:",
        "total 8.0K",
        "drwxr-xr-x 2 ansible ansible 4.0K Oct 26 22:51 .",
        "drwxr-xr-x 8 ansible ansible 4.0K Oct 26 22:51 .."
    ]
}

TASK [(7) Change file modification time and access time] ************************************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [Verify changes for (7)] ***************************************************************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [debug] ********************************************************************************************************************************
ok: [debian11_int] => {
    "results7.stdout_lines": [
        "-rw-rw---- 1 ansible ansible 6 Oct 26 21:49 /home/ansible/hello.txt",
        "-rw-rw---- 1 ansible ansible 6 Oct 26 22:51 /home/ansible/hello.txt"
    ]
}
ok: [ubuntu20_int] => {
    "results7.stdout_lines": [
        "-rw-rw---- 1 ansible ansible 6 Oct 26 21:49 /home/ansible/hello.txt",
        "-rw-rw---- 1 ansible ansible 6 Oct 26 22:51 /home/ansible/hello.txt"
    ]
}

TASK [(8) Delete a directory recursively] ***************************************************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [Verify changes for (8)] ***************************************************************************************************************
ok: [ubuntu20_int]
ok: [debian11_int]

TASK [debug] ********************************************************************************************************************************
ok: [ubuntu20_int] => {
    "result8_dir.stat.exists": false
}
ok: [debian11_int] => {
    "result8_dir.stat.exists": false
}

TASK [(9) Delete a file] ********************************************************************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [Verify changes for (9)] ***************************************************************************************************************
ok: [ubuntu20_int]
ok: [debian11_int]

TASK [debug] ********************************************************************************************************************************
ok: [ubuntu20_int] => {
    "result9_file.stat.exists": false
}
ok: [debian11_int] => {
    "result9_file.stat.exists": false
}

PLAY RECAP **********************************************************************************************************************************
debian11_int               : ok=27   changed=15   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ubuntu20_int               : ok=27   changed=15   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

[ © 2008-2021 myfaqbase.com - A property of WPDC Consulting ]