moving a folder on a remote whether it exists

20 views
Skip to first unread message

dulh...@mailbox.org

unread,
Jul 27, 2022, 4:34:11 AM7/27/22
to ansible...@googlegroups.com
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
 
################################################################################
- name: move legacy postgres - stat postgres_base
  stat: path=/opt/db/postgres
  register: postgres_base_stat
 
- name: move legacy postgres - move postgres_base to postgres_base_legacy
    command: mv /opt/db/postgres /opt/db/postgres/legacy"
    when: postgres_base_stat.stat.exists
################################################################################
 
The TASKS run through, but do not move the folder as intended, apparently because the stat TASK does not return the right value ("skip_reason": "Conditional result was False")
 
Do I have to reverse the when clause?
 
below the detailed output
 
TASK [postgres_disable : move legacy postgres - stat "postgres_back"] *******************************************************
task path: /home/gwagner/repos/ansible/open_source/postgres_upgrade/roles/postgres_disable/tasks/main.yml:32
<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-1658907975.2889578-62723-197073820787608 `" && echo ansible-tmp-1658907975.2889578-62723-197073820787608="` echo /home/gwagner/.ansible/tmp/ansible-tmp-1658907975.2889578-62723-197073820787608 `" ) && sleep 0'"'"''
<rhel8-vm.local> (0, b'ansible-tmp-1658907975.2889578-62723-197073820787608=/home/gwagner/.ansible/tmp/ansible-tmp-1658907975.2889578-62723-197073820787608\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-626726_zsgrbg/tmpxz0n4329 TO /home/gwagner/.ansible/tmp/ansible-tmp-1658907975.2889578-62723-197073820787608/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-626726_zsgrbg/tmpxz0n4329 /home/gwagner/.ansible/tmp/ansible-tmp-1658907975.2889578-62723-197073820787608/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-1658907975.2889578-62723-197073820787608/ /home/gwagner/.ansible/tmp/ansible-tmp-1658907975.2889578-62723-197073820787608/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-1658907975.2889578-62723-197073820787608/AnsiballZ_stat.py && sleep 0'"'"''
<rhel8-vm.local> (0, b'\r\n{"changed": false, "stat": {"exists": false}, "invocation": {"module_args": {"path": " /opt/db/backup/postgres/backups ", "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-1658907975.2889578-62723-197073820787608/ > /dev/null 2>&1 && sleep 0'"'"''
<rhel8-vm.local> (0, b'', b'')
ok: [rhel8-vm.local] => {
"changed": false,
"invocation": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_attributes": true,
"get_checksum": true,
"get_md5": false,
"get_mime": true,
"path": " /opt/db/backup/postgres/backups "
}
},
"stat": {
"exists": false
}
}
Perform task: TASK: postgres_disable : move legacy postgres - rename "postgres_back" to "postgre_back/legacy" (N)o/(y)es/(c)ontinue: y
Perform task: TASK: postgres_disable : move legacy postgres - rename "postgres_back" to "postgre_back/legacy" (N)o/(y)es/(c)ontinue: ****************************
 
TASK [postgres_disable : move legacy postgres - rename "postgres_back" to "postgre_back/legacy"] ****************************
task path: /home/gwagner/repos/ansible/open_source/postgres_upgrade/roles/postgres_disable/tasks/main.yml:35
skipping: [rhel8-vm.local] => {
"changed": false,
"skip_reason": "Conditional result was False"
 
 
 
 

Stefan Hornburg (Racke)

unread,
Jul 27, 2022, 5:09:46 AM7/27/22
to ansible...@googlegroups.com
On 27/07/2022 10:33, dulhaver via Ansible Project wrote:
> 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
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1083094168.383085.1658910834565%40office.mailbox.org <https://groups.google.com/d/msgid/ansible-project/1083094168.383085.1658910834565%40office.mailbox.org?utm_medium=email&utm_source=footer>.


--
Automation expert - Ansible and friends
Linux administrator & Debian maintainer
Perl Dancer & conference hopper

dulh...@mailbox.org

unread,
Jul 27, 2022, 7:11:56 AM7/27/22
to ansible...@googlegroups.com

> On 07/27/2022 11:09 AM CEST Stefan Hornburg (Racke) <ra...@linuxia.de> wrote:
>
>
> On 27/07/2022 10:33, dulhaver via Ansible Project wrote:
> > 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/...).

#### my TASK ###########################################################################################

- name: move legacy postgres - stat postgres_base
stat: path=/opt/db/postgres
register: postgres_base_stat

- name: move legacy postgres - move postgres_base to postgres_base_legacy
command: mv /opt/db/postgres /opt/db/postgres/legacy"
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] => {
"changed": false,
"invocation": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_attributes": true,
"get_checksum": true,
"get_md5": false,
"get_mime": true,
"path": " /opt/db/postgres "
}
},
"stat": {
"exists": false
}
}


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] => {
"changed": false,
"skip_reason": "Conditional result was False"
}

########################################################################################################

Stefan Hornburg (Racke)

unread,
Jul 27, 2022, 7:17:27 AM7/27/22
to ansible...@googlegroups.com
On 27/07/2022 13:11, dulhaver via Ansible Project wrote:
>> On 07/27/2022 11:09 AM CEST Stefan Hornburg (Racke) <ra...@linuxia.de> wrote:
>>
>>
>> On 27/07/2022 10:33, dulhaver via Ansible Project wrote:
>>> 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": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_attributes": true,
"get_checksum": true,
"get_md5": false,
"get_mime": true,
"path": " /opt/db/postgres "
}
},

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

Regards

            Racke
Reply all
Reply to author
Forward
0 new messages