# renaming a folder on the remote ... issue with defaults/main.yml

**URL:** <https://forum.ansible.com/t/renaming-a-folder-on-the-remote-issue-with-defaults-main-yml/36920>\
**Category:** Ansible Project\
**Created:** [July 29, 2022, 9:09am UTC](https://forum.ansible.com/t/renaming-a-folder-on-the-remote-issue-with-defaults-main-yml/36920 "2022-07-29T09:09:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [July 29, 2022, 9:09am UTC](https://forum.ansible.com/t/renaming-a-folder-on-the-remote-issue-with-defaults-main-yml/36920/1 "2022-07-29T09:09:36Z")

</div>

I am trying to rename a folder on a remote if it exists. this is being done via a role.

====== my two tasks ===============================================================  
- name: move legacy postgres - stat postgres\_data  
&nbsp;&nbsp;stat: path="{{ postgres\_data }}"  
&nbsp;&nbsp;register: postgres\_data

- name: move legacy postgres - rename $PGDATA to /opt/db/data/postgres/data\_legacy  
&nbsp;&nbsp;command: mv "{{ postgres\_data }}" "{{ postgres\_data\_legacy }}"  
&nbsp;&nbsp;when: postgres\_data.stat.exists

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [July 29, 2022, 9:24am UTC](https://forum.ansible.com/t/renaming-a-folder-on-the-remote-issue-with-defaults-main-yml/36920/2 "2022-07-29T09:24:25Z")

</div>

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mv: cannot stat '{'\\''changed'\\'': False, '\\''stat'\\'': {'\\''exists'\\'': True, '\\''path'\\'': '\\''/opt/db/data/postgres/data'\\'', '\\''mode'\\'': '\\''0750'\\'', '\\''isdir'\\'': True, '\\''ischr'\\'': False, '\\''isblk'\\'': False, '\\''isreg'\\'': False, '\\''isfifo'\\'': False, '\\''islnk'\\'': False, '\\''issock'\\'': False, '\\''uid'\\'': 1001, '\\''gid'\\'': 1001, '\\''size'\\'': 4096, '\\''inode'\\'': 65664, '\\''dev'\\'': 64779, '\\''nlink'\\'': 20, '\\''atime'\\'': 1659080065.949395, '\\''mtime'\\'': 1659017614.6222425, '\\''ctime'\\'': 1659080076.9242337, '\\''wusr'\\'': True, '\\''rusr'\\'': True, '\\''xusr'\\'': True, '\\''wgrp'\\'': False, '\\''rgrp'\\'': True, '\\''xgrp'\\'': True, '\\''woth'\\'': False, '\\''roth'\\'': False, '\\''xoth'\\'': False, '\\''isuid'\\'': False, '\\''isgid'\\'': False, '\\''blocks'\\'': 8, '\\''block\_size'\\'': 4096, '\\''device\_type'\\'': 0, '\\''readable'\\'': False, '\\''writeable'\\'': False, '\\''executable'\\'': False,  
> &nbsp;&nbsp;'\\''pw\_name'\\'': '\\''postgres'\\'', '\\''gr\_name'\\'': '\\''postgres'\\'', '\\''mimetype'\\'': '\\''inode/directory'\\'', '\\''charset'\\'': '\\''binary'\\'', '\\''version'\\'': None, '\\''attributes'\\'': , '\\''attr\_flags'\\'': '\\'''\\''}, '\\''failed'\\'': False}': No such file or directory"

You are trying to move a directory called '{'\\''changed'\\'': False,  
'\\''stat'\\'': {'\\''exists'\\'': True, '\\''path'\\'':  
'\\''/opt/db/data/postgres/data'\\'', '\\''mode'\\'': '\\''0750'\\'',  
'\\''isdir'\\'': True, '\\''ischr'\\'': False, '\\''isblk'\\'': False,  
'\\''isreg'\\'': False, '\\''isfifo'\\'': False, '\\''islnk'\\'':  
False, '\\''issock'\\'': False, '\\''uid'\\'': 1001, '\\''gid'\\'':  
1001, '\\''size'\\'': 4096, '\\''inode'\\'': 65664, '\\''dev'\\'':  
64779, '\\''nlink'\\'': 20, '\\''atime'\\'': 1659080065.949395,  
'\\''mtime'\\'': 1659017614.6222425, '\\''ctime'\\'':  
1659080076.9242337, '\\''wusr'\\'': True, '\\''rusr'\\'': True,  
'\\''xusr'\\'': True, '\\''wgrp'\\'': False, '\\''rgrp'\\'': True,  
'\\''xgrp'\\'': True, '\\''woth'\\'': False, '\\''roth'\\'': False,  
'\\''xoth'\\'': False, '\\''isuid'\\'': False, '\\''isgid'\\'': False,  
'\\''blocks'\\'': 8, '\\''block\_size'\\'': 4096,  
'\\''device\_type'\\'': 0, '\\''readable'\\'': False,  
'\\''writeable'\\'': False, '\\''executable'\\'': False,

> &nbsp;&nbsp;'\\''pw\_name'\\'': '\\''postgres'\\'', '\\''gr\_name'\\'': '\\''postgres'\\'', '\\''mimetype'\\'': '\\''inode/directory'\\'', '\\''charset'\\'': '\\''binary'\\'', '\\''version'\\'': None, '\\''attributes'\\'': , '\\''attr\_flags'\\'': '\\'''\\''}, '\\''failed'\\'': False}', which does not exist.

The variable you register with stat is not just a string, but a dict.  
You should use the correct key from that dict (i.e. 'postgres\_data.path')

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [July 29, 2022, 9:25am UTC](https://forum.ansible.com/t/renaming-a-folder-on-the-remote-issue-with-defaults-main-yml/36920/3 "2022-07-29T09:25:28Z")

</div>

probably 'postgres\_data.stat.path'

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [July 29, 2022, 10:02am UTC](https://forum.ansible.com/t/renaming-a-folder-on-the-remote-issue-with-defaults-main-yml/36920/4 "2022-07-29T10:02:03Z")

</div>

it seems neither

´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´  
- name: move legacy postgres - stat postgres\_data  
&nbsp;&nbsp;stat: path="{{ postgres\_data }}"  
&nbsp;&nbsp;register: postgres\_data

- name: move legacy postgres - rename $PGDATA to /opt/db/data/postgres/data\_legacy  
&nbsp;&nbsp;command: mv "{{ postgres\_data }}" "{{ postgres\_data\_legacy }}" #  
&nbsp;&nbsp;when: postgres\_data.stat.path  
&nbsp;&nbsp;become: true  
´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´  
nor

´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´  
- name: move legacy postgres - stat postgres\_data  
&nbsp;&nbsp;stat: path="{{ postgres\_data }}"  
&nbsp;&nbsp;register: postgres\_data

- name: move legacy postgres - rename $PGDATA to /opt/db/data/postgres/data\_legacy  
&nbsp;&nbsp;command: mv "{{ postgres\_data }}" "{{ postgres\_data\_legacy }}" #  
&nbsp;&nbsp;when: postgres\_data.path  
&nbsp;&nbsp;become: true  
´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´

seems to make any difference. When I put harcoded locations the dir is renamed, when I use the variables from the defaults/main.yml it isn't

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [July 29, 2022, 10:24am UTC](https://forum.ansible.com/t/renaming-a-folder-on-the-remote-issue-with-defaults-main-yml/36920/5 "2022-07-29T10:24:17Z")

</div>

Correct.  
I assumed it was obvious that my suggestion was for the variable in  
the command - because that is what the error is about.  
You used it in the 'when' clause - which does not make sense as that  
was OK to start with.  
But now I see another issue with your code. You use 'postgres\_data' as  
a variable in the stat task, which you then overwrite with the results  
of that task.  
That is very confusing and the source of your error.  
Rather than describing things and then running the risk of  
misinterpretation again, perhaps it's better to just spell it out  
entirely:

- name: move legacy postgres - stat postgres\_data  
&nbsp;&nbsp;stat: path="{{ postgres\_data }}"  
&nbsp;&nbsp;register: pgdatastat

- name: move legacy postgres - rename $PGDATA to  
/opt/db/data/postgres/data\_legacy  
&nbsp;&nbsp;command: mv "{{ pgdatastat.stat.path }}" "{{ postgres\_data\_legacy }}"  
&nbsp;&nbsp;when: pgdatastat.stat.exists

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [July 29, 2022, 3:05pm UTC](https://forum.ansible.com/t/renaming-a-folder-on-the-remote-issue-with-defaults-main-yml/36920/6 "2022-07-29T15:05:40Z")

</div>

that works now, thx.

Apparently I still will have to study what 'stat' acutally does under the hood before I really get what actually happens here and why.
