question about mode=preserve with file module

Hi everyone,

I’m having some issues using the “mode” parameter on the file module to change ownership of an existing directory without modifying its permissions. For example:

  • name: use preserve mode

file:
path=/tmp/test
owner=www-data
mode=preserve
state=directory
become: true

Based on the documentation, I would assume that this would “preserve” the directory’s existing permissions while changing its owner. Instead, it fails with “mode must be in octal or symbolic form” / “bad symbolic permission for mode: preserve”.

I’ve managed to get the behavior I need by using an empty string for the mode parameter, but I am unsure why “preserve” doesn’t work. Is this a bug in the file module? Am I reading the documentation wrong?

Thanks,
Martey

Not sure it makes any difference but can you try to use the colon yaml syntax for the parameters

Not sure it makes any difference but can you try to use the colon yaml syntax for the parameters

Using standard YAML syntax instead of key=value displays the same error message.

ansible --version?

Hi everyone,

I'm having some issues using the "mode" parameter on the file module
<https://docs.ansible.com/ansible/latest/modules/file_module.html#parameter-mode&gt; to change ownership of an existing
directory without modifying its permissions. For example:

- name: use preserve mode
file:
path=/tmp/test
owner=www-data
mode=preserve
state=directory
become: true

Based on the documentation, I would assume that this would "preserve" the directory's existing permissions while
changing its owner. Instead, it fails with "mode must be in octal or symbolic form" / "bad symbolic permission for mode:
preserve".

I've managed to get the behavior I need by using an empty string for the mode parameter, but I am unsure why "preserve"
doesn't work. Is this a bug in the file module? Am I reading the documentation wrong?

Hello Martey,

my reading of the documentation is that "preserve" applies only when you are using the "src" parameter. For example when
you are creating a symlink.

Regards
          Racke

have a look at this comment
https://github.com/ansible/ansible/issues/71377#issuecomment-677892112

according to that comment, it is not intended to work with file.

kind regards, markus

Martey,

... the "mode" parameter on the file module to change ownership of
an existing directory without modifying its permissions. For
example:

- name: use preserve mode
  file:
    path=/tmp/test
    owner=www-data
    mode=preserve
    state=directory
  become: true

When "state=directory" the parameter mode says "The permissions the
resulting file or directory should have". There is no reason to use
"mode=preserve" if you don't want to change the mode. Simply remove
the line

  - name: use preserve mode
    file:
      path=/tmp/test
      owner=www-data
      state=directory
    become: true