Followers

Ansible file Module

  file module in ansible deals with file/directory-related operations. Example 1:-  Change file permission and ownership Playbook: fileper.y...

 file module in ansible deals with file/directory-related operations.

Example 1:- 

Change file permission and ownership

Playbook: fileper.yaml

---
 - name: play for file module
   hosts: webservers
   tasks:
      - name: Change file permssion
        file:
           path: /tmp/foo.conf
           owner: vagrant
           group: vagrant
           mode: 0644
Execute:-    ansible-playbook fileper.yaml

Example 2:- 

Create a soft link file

Playbook: fileper.yaml

---
 - name: play for file module
   hosts: webservers
   tasks:
      - name: Create a soft link file
        file:
           src: /tmp/foo.conf
           dest: /tmp/foo.lnk
           state: link

Execute:-    ansible-playbook fileper.yaml

Example 3:- 

Create a soft link file with loop

Playbook: fileper.yaml

---
 - name: play for file module
   hosts: webservers
   tasks:
      - name: Create a soft link file
        file:
           src: '/tmp/{{ item.src }}'
           dest: '{{ item.dest }}'
           state: link
        with_items:
           - { src: 'foo.conf', dest: 'foo.lnk' }
           - { src: 'foo.txt', dest: 'footxt.lnk' }

Execute:-    ansible-playbook fileper.yaml

Example 4:- 

Change the permission of file using symbolic modes

Playbook: fileper.yaml

---
 - name: play for file module
   hosts: webservers
   tasks:
      - name: Create a soft link file
        file:
           path: /tmp/newfile.txt
           state: touch
           mode : u=rw,g=r,o=r
Execute:-    ansible-playbook fileper.yaml


Example 5:- 

Change the permission of file using symbolic modes

Playbook: fileper.yaml

---
 - name: play for file module
   hosts: webservers
   tasks:
      - name: Create a soft link file
        file:
           path: /tmp/newfile.txt
           state: touch
           mode : u+rw,g+r,o-r

Execute:-    ansible-playbook fileper.yaml

Example 6:- 

Preserve modification and access time of a file with state= touch

Playbook: fileper.yaml

---
 - name: play for file module
   hosts: webservers
   tasks:
      - name: Create a soft link file
        file:
          path: /tmp/foo.conf
          state: touch
          modification_time: "preserve"
          access_time: "preserve"

Execute:-    ansible-playbook fileper.yaml

Example 7:- 

Change modification and access time of a file with current date time during state=touch

Playbook: fileper.yaml

---
 - name: play for file module
   hosts: webservers
   tasks:
      - name: Create a soft link file
        file:
          path: /tmp/foo.conf
          state: touch
          modification_time: now
          access_time: now

Execute:-    ansible-playbook fileper.yaml


Example 8:- 

Create a directory

Playbook: fileper.yaml

---
 - name: play for file module
   hosts: webservers
   tasks:
      - name: Create a soft link file
        file:
          path: /tmp/ansibledir
          state: directory

Execute:-    ansible-playbook fileper.yaml

COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
item
DevOpsWorld: Ansible file Module
Ansible file Module
DevOpsWorld
https://www.devopsworld.co.in/2022/02/ansible-file-module.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2022/02/ansible-file-module.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content