# question about mode=preserve with file module

**URL:** <https://forum.ansible.com/t/question-about-mode-preserve-with-file-module/33649>\
**Category:** Ansible Project\
**Created:** [August 20, 2020, 7:34pm UTC](https://forum.ansible.com/t/question-about-mode-preserve-with-file-module/33649 "2020-08-20T19:34:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martey\_Dodoo](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@Martey\_Dodoo](https://forum.ansible.com/u/Martey_Dodoo)\
**Post date:** [August 20, 2020, 7:34pm UTC](https://forum.ansible.com/t/question-about-mode-preserve-with-file-module/33649/1 "2020-08-20T19:34:20Z")

</div>

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) 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

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [August 20, 2020, 7:52pm UTC](https://forum.ansible.com/t/question-about-mode-preserve-with-file-module/33649/2 "2020-08-20T19:52:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Martey\_Dodoo](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@Martey\_Dodoo](https://forum.ansible.com/u/Martey_Dodoo)\
**Post date:** [August 20, 2020, 8:17pm UTC](https://forum.ansible.com/t/question-about-mode-preserve-with-file-module/33649/3 "2020-08-20T20:17:26Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [August 21, 2020, 3:12am UTC](https://forum.ansible.com/t/question-about-mode-preserve-with-file-module/33649/4 "2020-08-21T03:12:29Z")

</div>

ansible --version?

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [August 21, 2020, 5:36am UTC](https://forum.ansible.com/t/question-about-mode-preserve-with-file-module/33649/5 "2020-08-21T05:36:57Z")

</div>

> 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](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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Markus\_Falb](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Markus\_Falb](https://forum.ansible.com/u/Markus_Falb)\
**Post date:** [August 21, 2020, 2:54pm UTC](https://forum.ansible.com/t/question-about-mode-preserve-with-file-module/33649/6 "2020-08-21T14:54:28Z")

</div>

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

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

kind regards, markus

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 21, 2020, 9:14pm UTC](https://forum.ansible.com/t/question-about-mode-preserve-with-file-module/33649/7 "2020-08-21T21:14:37Z")

</div>

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  
> &nbsp;&nbsp;file:  
> &nbsp;&nbsp;&nbsp;&nbsp;path=/tmp/test  
> &nbsp;&nbsp;&nbsp;&nbsp;owner=www-data  
> &nbsp;&nbsp;&nbsp;&nbsp;mode=preserve  
> &nbsp;&nbsp;&nbsp;&nbsp;state=directory  
> &nbsp;&nbsp;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

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