FreeKB - Ansible file module (change owner group mode permissions chown chgrp chmod)
Ansible - file module (change owner group mode permissions chown chgrp chmod)

The file module can be used to change the owner, group, and/or mode of a file on a managed node (e.g. target system). Or, the local_action module can be used to invoke this module on the control node (that's your Ansible server).

The ansible-doc file command can be used to show the Ansible documention on the file module.

In this example, the owner/group/mode of /tmp/foo.txt will be updated.

- name: "update foo.txt owner group mode"
  file:
    path: /tmp/foo.txt
    owner: "root"
    group: "root"
    mode: "0664"

 

The task should ok if the owner/group/mode were not updated.

TASK [update foo.txt owner group mode]
ok: [server1.example.com]

 

The task should have changed if the owner/group/mode were updated.

TASK [update foo.txt owner group mode]
changed: [server1.example.com]

 


Recursive directory

The state: directory and recurse: yes options can be used to update the owner of every file and directory below a certain directory. In this example, every file and directory below the /tmp directory will be updated to be owned by root.

- name: "recursively update /tmp directory owner"
  file:
    path: "/tmp"
    state: "directory"
    recurse: "yes"
    owner: "root"

 

Or like this, to give "group" the "write" permission.

- name: "recursively update /tmp directory"
  file:
    path: "/tmp"
    state: "directory"
    recurse: "yes"
    mode: "g+w"

 


Optional Parameters

Refer to the following articles for more details on the owner, group, and mode parameters.

  • owner (e.g. owner: "root")
  • group (e.g. group: "wheel")
  • mode (e.g. mode: "2775")


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 3cfec in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |