Ansible File Module

Ansible File Module:

ansible file module is used for creating files and deleting the files in the remote server. With ansible file module We can also create multiple files and we can also delete multiple files in the remote server. we can create files with content.  we can create directories we can delete directories and we can change the permissions of the files. We can create symlinks or soft links and we can create hard links and we can delete those links. With the ansible file module, we can set the permissions to the files.

creating a file in remote server-ansible file module

- name: create file in a remote server
  file:
   path: /path/to/file/in/remote/server/devops.txt
   state: touch

in ansible file module we have different parameters. In this case, we are using path and state parameters. in every file module path is a must. In the path parameter, we will mention the path of the file in remote server. At this path only the file will be created.

at path:  mention the path of the file in remote server.

at state:  mention touch,   touch will create file exact like Linux command.

it will create a new empty file with name devops.txt. so mention filename in the path.

so in the state: we will mention touch to create the file.

Deleting a file in remote server-ansible file module

- name:  delete file in a remote server
  file:
   path: /etc/abcd.conf
   state: absent

here we are deleting abcd.conf in the remote server. So at path parameter mention path of your file which you want to delete.

at path:  mention the path of the file in remote server.

at state:  mention absent to delete the file.

so at state parameter, we will use touch to create the file, absent to delete the e file.

creating a file with permissions-ansible file module

 tasks:  
  - name: Ansible file module to create new file with permissions.    
    file:      
    path: /path/to/cretae/file/devops.txt    
    state: touch      
    mode: 0421      
    owner: devops

 

by using this module we can create the e file with permissions

at mode parameter we have total 4 digits. will mention starting will be always zero ramaining digits will be your file permissions.

at owner parameter, you can mention the  owner of the file

this permissions will be set to that newly created file

file:    
 path: /path/to/cretae/file/devops.txt   
 state: touch    
 mode: "u=rw,g=w,o=e"    
 owner: devops

both the above codes work like same but here we are using symolic mode (equivalent to 0421).

Creating multiple new files-ansible file module

tasks:  
- name: Ansible file module to create multiple files    
  file:       
   path: "{{ item }}"      
   state: touch     
   mode: 0421    
  with_items:    
  - devops1.txt    
  - devops2.txt    
  - devops3.txt   

At path paremater we are using “{{ item }}” , by using this we can crete loop to create multiple files.

At with_items parameter, we have to mentions  file names which you want to create.

So by using “{{ item }}”  , with_items parameters we can crete loop and we can create multiple files.

Deleting multiple files-ansible file module

- name: Ansible file module to delete multiple files 
  file:                  
   path: "{{ item }}"    
   state: absent  
  with_items:    
  - devops1.txt    
  - devops2.txt    
  - devops3.txt

To create multiple files and to delete multiple files the code will be the same but small change in state parameter.

Touch to create files, absent to delete files.

you can refer in official ansible documentation https://docs.ansible.com/ansible/2.5/modules/file_module.html

2 Responses

  1. Anonymous says:

    bro formatting.

     

  2. vijay says:

    thanks for the great information

Leave a Reply

Your email address will not be published. Required fields are marked *