ansible-collections / ansible.posix

Ansible Collection for Posix
Other
157 stars 152 forks source link

Synchronize version from release 1.5.1 does not work anymore #414

Open MalfuncEddie opened 1 year ago

MalfuncEddie commented 1 year ago
SUMMARY

Synchronize version from release 1.5.1 does not work anymore it did work in 1.4.0

- name: Synchronize path1 to path2
  synchronize:
    src: path1 
    dest: path2
  delegate_to: "{{other_server}}"

I do use the following settings in ansible.cfg (playbook is executed on new deployed servers so no host checking is needed

[defaults]
host_key_checking = False
ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'

[ssh_connection]
ssh_args = -o UserKnownHostsFile=/dev/null

in version 1.5.1. ik looks like a prompt (allow yes/no) ssh prompt is given

snipit does the following. delegate to a server that has access to the new server en rsync some file from path1 to path 2.

ISSUE TYPE
COMPONENT NAME

Synchronize

ANSIBLE VERSION
is in an execution environment --> will try to recreate with basic ansible
COLLECTION VERSION
```yaml
---
collections:
  - name: ansible.posix
    version: 1.5.1
    source: https://galaxy.ansible.com
  - name: ansible.netcommon
    source: https://galaxy.ansible.com

##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
```paste below
is in an execution environment --> will try to recreate with basic ansible

in version

OS / ENVIRONMENT

is in an execution environment

STEPS TO REPRODUCE

see above

EXPECTED RESULTS
ACTUAL RESULTS

synchronize hangs since it s waiting for an "allow host ssh prompt"

<10.10.222.222> ESTABLISH SSH CONNECTION FOR USER: adminuser
<10.10.222.222> SSH: EXEC sshpass -d10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o 'User="adminuser"' -o ConnectTimeout=30 10.10.222.222 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /home/users/adminuser/.ansible/tmp `"&& mkdir "` echo /home/users/adminuser/.ansible/tmp/ansible-tmp-1675845096.2621477-1415-114966977828356 `" && echo ansible-tmp-1675845096.2621477-1415-114966977828356="` echo /home/users/adminuser/.ansible/tmp/ansible-tmp-1675845096.2621477-1415-114966977828356 `" ) && sleep 0'"'"''
<10.10.222.222> (0, b'ansible-tmp-1675845096.2621477-1415-114966977828356=/home/users/adminuser/.ansible/tmp/ansible-tmp-1675845096.2621477-1415-114966977828356\n', b"Warning: Permanently added '10.10.222.222' (ECDSA) to the list of known hosts.\r\n")
<192.168.119.233> Attempting python interpreter discovery
<10.10.222.222> ESTABLISH SSH CONNECTION FOR USER: adminuser
<10.10.222.222> SSH: EXEC sshpass -d10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o 'User="adminuser"' -o ConnectTimeout=30 10.10.222.222 '/bin/sh -c '"'"'echo PLATFORM; uname; echo FOUND; command -v '"'"'"'"'"'"'"'"'python3.10'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.9'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.8'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.7'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.6'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.5'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'/usr/bin/python3'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'/usr/libexec/platform-python'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python2.7'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python2.6'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'/usr/bin/python'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python'"'"'"'"'"'"'"'"'; echo ENDFOUND && sleep 0'"'"''
<10.10.222.222> (0, b'PLATFORM\nLinux\nFOUND\n/usr/bin/python3.8\n/usr/bin/python3\n/usr/bin/python2.7\n/usr/bin/python\n/usr/bin/python\nENDFOUND\n', b"Warning: Permanently added '10.10.222.222' (ECDSA) to the list of known hosts.\r\n")
<10.10.222.222> ESTABLISH SSH CONNECTION FOR USER: adminuser
<10.10.222.222> SSH: EXEC sshpass -d10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o 'User="adminuser"' -o ConnectTimeout=30 10.10.222.222 '/bin/sh -c '"'"'/usr/bin/python3.8 && sleep 0'"'"''
<10.10.222.222> (0, b'{"platform_dist_result": [], "osrelease_content": "NAME=\\"Ubuntu\\"\\nVERSION=\\"20.04.5 LTS (Focal Fossa)\\"\\nID=ubuntu\\nID_LIKE=debian\\nPRETTY_NAME=\\"Ubuntu 20.04.5 LTS\\"\\nVERSION_ID=\\"20.04\\"\\nHOME_URL=\\"https://www.ubuntu.com/\\"\\nSUPPORT_URL=\\"https://help.ubuntu.com/\\"\\nBUG_REPORT_URL=\\"https://bugs.launchpad.net/ubuntu/\\"\\nPRIVACY_POLICY_URL=\\"https://www.ubuntu.com/legal/terms-and-policies/privacy-policy\\"\\nVERSION_CODENAME=focal\\nUBUNTU_CODENAME=focal\\n"}\n', b"Warning: Permanently added '10.10.222.222' (ECDSA) to the list of known hosts.\r\n")
Using module file /runner/requirements_collections/ansible_collections/ansible/posix/plugins/modules/synchronize.py
<10.10.222.222> PUT /var/lib/awx/.ansible/tmp/ansible-local-20eg7ntu0n/tmpodu922sc TO /home/users/adminuser/.ansible/tmp/ansible-tmp-1675845096.2621477-1415-114966977828356/AnsiballZ_synchronize.py
<10.10.222.222> SSH: EXEC sshpass -d10 sftp -o BatchMode=no -b - -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o 'User="adminuser"' -o ConnectTimeout=30 '[10.10.222.222]'
<10.10.222.222> (0, b'sftp> put /var/lib/awx/.ansible/tmp/ansible-local-20eg7ntu0n/tmpodu922sc /home/users/adminuser/.ansible/tmp/ansible-tmp-1675845096.2621477-1415-114966977828356/AnsiballZ_synchronize.py\n', b"Warning: Permanently added '10.10.222.222' (ECDSA) to the list of known hosts.\r\n")
<10.10.222.222> ESTABLISH SSH CONNECTION FOR USER: adminuser
<10.10.222.222> SSH: EXEC sshpass -d10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o 'User="adminuser"' -o ConnectTimeout=30 10.10.222.222 '/bin/sh -c '"'"'chmod u+x /home/users/adminuser/.ansible/tmp/ansible-tmp-1675845096.2621477-1415-114966977828356/ /home/users/adminuser/.ansible/tmp/ansible-tmp-1675845096.2621477-1415-114966977828356/AnsiballZ_synchronize.py && sleep 0'"'"''
<10.10.222.222> (0, b'', b"Warning: Permanently added '10.10.222.222' (ECDSA) to the list of known hosts.\r\n")
<10.10.222.222> ESTABLISH SSH CONNECTION FOR USER: adminuser
<10.10.222.222> SSH: EXEC sshpass -d10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o 'User="adminuser"' -o ConnectTimeout=30 -tt 10.10.222.222 '/bin/sh -c '"'"'sudo -H -S -p "[sudo via ansible, key=wgtebnfjqzhsvdjcfdpcudsvoamvaiku] password:" -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-wgtebnfjqzhsvdjcfdpcudsvoamvaiku ; /usr/bin/python3 /home/users/adminuser/.ansible/tmp/ansible-tmp-1675845096.2621477-1415-114966977828356/AnsiballZ_synchronize.py'"'"'"'"'"'"'"'"' && sleep 0'"'"''
Escalation succeeded

I'll try and debug more in the coming days

MalfuncEddie commented 1 year ago

might be related to #395

MalfuncEddie commented 1 year ago

Did some more testing test case playbook

- name: Testcase for synchronize
  become: true
  become_method: sudo
  gather_facts: yes
  hosts: 172.31.222.241
  tasks:
    - name: Synchronize
      ansible.posix.synchronize:
        src: /tmp/testfile
        dest: /tmp/testfile
        use_ssh_args: True
      delegate_to: "172.31.222.101"

ansible.cfg

[defaults]
host_key_checking = False
ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'
timeout = 30

[ssh_connection]
ssh_args = -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null

playbook command

ansible-playbook -i ,172.31.222.241,172.31.222.101 task.yml -k -K
SSH password:
BECOME password[defaults to SSH password]:

this works versions: ansible.posix: 1.4.0 ansible-playbook [core 2.12.5]

This does not work: ansible.posix: 1.5.1 ansible-playbook [core 2.12.5]

upgrade ansible versions

This does not work: ansible.posix: 1.5.1 ansible-playbook [core 2.14.2]

This does not work: ansible.posix: 1.4.0 ansible-playbook [core 2.14.2]

So I am not sure it is a problem with synchronize or with ansible-core. Will try to debug some more. I do not think that ansible-core deprecated some ansible.cfg settings. I also did a test with ansible-core 2.13.0 and 1.4.0 collection but also does not work.

gustavi commented 1 year ago

I have a dirty patch that work in my specific case:

         if delegate_to is None and remote_transport is False:
             dest_is_local = True
         elif delegate_to is not None and delegate_to in dest_host_ids:
             dest_is_local = True
+        elif delegate_to in ("localhost", src_host):
+            dest_is_local = True

         # CHECK FOR NON-DEFAULT SSH PORT

I'm quite sure there are better ways to check if delegate_to is control node.

I think adding control node in dest_host_ids may be the solution but I'm too new to ansible code base to be confidant about this.

EDIT : indent in code bloc

atika commented 4 months ago

ansible version 2.12.10 / ansible.posix version 1.5.4

This also does not work from a playbook, but work from the command line (with localhost as the target).

- hosts: my_external_server
  tasks:
    - name: "Sync files from local computer to localhost"
      synchronize:
        mode: pull
        src: mylocalcomputer:/path/to/dir
        dest: /tmp/destination/
       rsync_path: "sudo rsync"
       rsync_opt:
         - "--rsh='...'"
      delegate_to: localhost

If I run without rsync_opt the specific port of my_external_server is added to the --rsh parameter. And my_external_server is prepended to the source. Instead of connecting to mylocalcomputer from localhost, he try to connect to my_external_server and sync the folder my_external_server/mylocalcomputer:/path/to/dir.

What I expect is that rsync is executed without modyfing the src or dest urls.

I tried different options, take a look at the source code, I am new to ansible and it's the first time I tried to use this task, I ended to use a shell task for this. I found old closed issue with the same problem.