# moving a folder on a remote whether it exists

**URL:** <https://forum.ansible.com/t/moving-a-folder-on-a-remote-whether-it-exists/36910>\
**Category:** Ansible Project\
**Created:** [July 27, 2022, 8:33am UTC](https://forum.ansible.com/t/moving-a-folder-on-a-remote-whether-it-exists/36910 "2022-07-27T08:33:54Z")\
**Posts on this page:** 4\
**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 27, 2022, 8:33am UTC](https://forum.ansible.com/t/moving-a-folder-on-a-remote-whether-it-exists/36910/1 "2022-07-27T08:33:54Z")

</div>

I am trying to rename/move a folder on a remote host and try to do this based on whether it exists

2 TASKS should be doing this

---

<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:** [July 27, 2022, 9:09am UTC](https://forum.ansible.com/t/moving-a-folder-on-a-remote-whether-it-exists/36910/2 "2022-07-27T09:09:18Z")

</div>

> I am trying to rename/move a folder on a remote host and try to do this based on whether it exists

That doesn't look like a proper path: "path": " /opt/db/backup/postgres/backups ".

Also it doesn't coincide with your playbook.

Regards

```
     Racke

```

---

<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 27, 2022, 11:11am UTC](https://forum.ansible.com/t/moving-a-folder-on-a-remote-whether-it-exists/36910/3 "2022-07-27T11:11:33Z")

</div>

sorry, my bad. I have several identical actions and mixed up the TASK of one with the return of another. So in reality they match up. The installation path /opt/db/postgres is what we do here in my context (despite the standard /var/lib/pgsql/...).

#### my TASK ###########################################################################################  
&nbsp;&nbsp;  
- name: move legacy postgres - stat postgres\_base  
&nbsp;&nbsp;stat: path=/opt/db/postgres  
&nbsp;&nbsp;register: postgres\_base\_stat

- name: move legacy postgres - move postgres\_base to postgres\_base\_legacy  
&nbsp;&nbsp;command: mv /opt/db/postgres /opt/db/postgres/legacy"  
&nbsp;&nbsp;when: postgres\_base\_stat.stat.exists

#### feedback #########################################################################################

TASK [postgres\_disable : move legacy postgres - stat postgres\_base] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/gwagner/repos/ansible/open\_source/postgres\_upgrade/roles/postgres\_disable/tasks/main.yml:16  
\<rhel8-vm.local\> ESTABLISH SSH CONNECTION FOR USER: None  
\<rhel8-vm.local\> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/gwagner/.ansible/cp/8ca9f05e1f"' rhel8-vm.local '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''  
\<rhel8-vm.local\> (0, b'/home/gwagner\n', b'')  
\<rhel8-vm.local\> ESTABLISH SSH CONNECTION FOR USER: None  
\<rhel8-vm.local\> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/gwagner/.ansible/cp/8ca9f05e1f"' rhel8-vm.local '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /home/gwagner/.ansible/tmp `"&& mkdir "` echo /home/gwagner/.ansible/tmp/ansible-tmp-1658919873.2246733-63503-154421168863718 `" && echo ansible-tmp-1658919873.2246733-63503-154421168863718="` echo /home/gwagner/.ansible/tmp/ansible-tmp-1658919873.2246733-63503-154421168863718 `" ) && sleep 0'"'"''  
\<rhel8-vm.local\> (0, b'ansible-tmp-1658919873.2246733-63503-154421168863718=/home/gwagner/.ansible/tmp/ansible-tmp-1658919873.2246733-63503-154421168863718\n', b'')  
Using module file /usr/lib/python3.8/site-packages/ansible/modules/stat.py  
\<rhel8-vm.local\> PUT /home/gwagner/.ansible/tmp/ansible-local-63470u1tpvd24/tmp5yf\_h4ad TO /home/gwagner/.ansible/tmp/ansible-tmp-1658919873.2246733-63503-154421168863718/AnsiballZ\_stat.py  
\<rhel8-vm.local\> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/gwagner/.ansible/cp/8ca9f05e1f"' '[rhel8-vm.local]'  
\<rhel8-vm.local\> (0, b'sftp\> put /home/gwagner/.ansible/tmp/ansible-local-63470u1tpvd24/tmp5yf\_h4ad /home/gwagner/.ansible/tmp/ansible-tmp-1658919873.2246733-63503-154421168863718/AnsiballZ\_stat.py\n', b'')  
\<rhel8-vm.local\> ESTABLISH SSH CONNECTION FOR USER: None  
\<rhel8-vm.local\> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/gwagner/.ansible/cp/8ca9f05e1f"' rhel8-vm.local '/bin/sh -c '"'"'chmod u+x /home/gwagner/.ansible/tmp/ansible-tmp-1658919873.2246733-63503-154421168863718/ /home/gwagner/.ansible/tmp/ansible-tmp-1658919873.2246733-63503-154421168863718/AnsiballZ\_stat.py && sleep 0'"'"''  
\<rhel8-vm.local\> (0, b'', b'')  
\<rhel8-vm.local\> ESTABLISH SSH CONNECTION FOR USER: None  
\<rhel8-vm.local\> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/gwagner/.ansible/cp/8ca9f05e1f"' -tt rhel8-vm.local '/bin/sh -c '"'"'/usr/libexec/platform-python /home/gwagner/.ansible/tmp/ansible-tmp-1658919873.2246733-63503-154421168863718/AnsiballZ\_stat.py && sleep 0'"'"''  
\<rhel8-vm.local\> (0, b'\r\n{"changed": false, "stat": {"exists": false}, "invocation": {"module\_args": {"path": " /opt/db/postgres ", "follow": false, "get\_md5": false, "get\_checksum": true, "get\_mime": true, "get\_attributes": true, "checksum\_algorithm": "sha1"}}}\r\n', b'Shared connection to rhel8-vm.local closed.\r\n')  
\<rhel8-vm.local\> ESTABLISH SSH CONNECTION FOR USER: None  
\<rhel8-vm.local\> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/home/gwagner/.ansible/cp/8ca9f05e1f"' rhel8-vm.local '/bin/sh -c '"'"'rm -f -r /home/gwagner/.ansible/tmp/ansible-tmp-1658919873.2246733-63503-154421168863718/ \> /dev/null 2\>&1 && sleep 0'"'"''  
\<rhel8-vm.local\> (0, b'', b'')  
ok: [rhel8-vm.local] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"changed": false,  
&nbsp;&nbsp;&nbsp;&nbsp;"invocation": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"module\_args": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"checksum\_algorithm": "sha1",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"follow": false,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"get\_attributes": true,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"get\_checksum": true,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"get\_md5": false,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"get\_mime": true,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"path": " /opt/db/postgres "  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;},  
&nbsp;&nbsp;&nbsp;&nbsp;"stat": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"exists": false  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

TASK [postgres\_disable : move legacy postgres - rename "postgres\_back" to "postgres\_user\_home/back/legacy"] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/gwagner/repos/ansible/open\_source/postgres\_upgrade/roles/postgres\_disable/tasks/main.yml:38  
skipping: [rhel8-vm.local] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"changed": false,  
&nbsp;&nbsp;&nbsp;&nbsp;"skip\_reason": "Conditional result was False"  
}

---

<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:** [July 27, 2022, 11:17am UTC](https://forum.ansible.com/t/moving-a-folder-on-a-remote-whether-it-exists/36910/4 "2022-07-27T11:17:04Z")

</div>

> > > I am trying to rename/move a folder on a remote host and try to do this based on whether it exists
> > 
> > That doesn't look like a proper path: "path": " /opt/db/backup/postgres/backups ".
> > 
> > Also it doesn't coincide with your playbook.
> 
> sorry, my bad. I have several identical actions and mixed up the TASK of one with the return of another. So in reality they match up. The installation path /opt/db/postgres is what we do here in my context (despite the standard /var/lib/pgsql/...).

You still have spurious white space in your path:

"invocation": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"module\_args": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"checksum\_algorithm": "sha1",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"follow": false,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"get\_attributes": true,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"get\_checksum": true,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"get\_md5": false,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"get\_mime": true,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"path": " /opt/db/postgres "  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},

That's the reason why the next task is skipped.

Regards

```
         Racke

```
