ansible-collections / ansible.posix

Ansible Collection for Posix
Other
157 stars 152 forks source link

synchronize treats ports on localhost as local connections #535

Open Jackenmen opened 7 months ago

Jackenmen commented 7 months ago
SUMMARY

When port forwarding a remote SSH server to a local port and using that local port in Ansible, synchronize module wrongfully treats it as a local connection and does not perform the transfer over SSH connection.

ISSUE TYPE
COMPONENT NAME

synchronize

ANSIBLE VERSION
ansible [core 2.15.9]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/jack/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/jack/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/usr/bin/python3)
  jinja version = 3.0.3
  libyaml = True
COLLECTION VERSION
# /usr/lib/python3/dist-packages/ansible_collections
Collection    Version
------------- -------
ansible.posix 1.5.4 
CONFIGURATION
CONFIG_FILE() = /etc/ansible/ansible.cfg
EDITOR(env: EDITOR) = subl -w
OS / ENVIRONMENT

Running Ubuntu 22.04 on both the controller node and the remote.

STEPS TO REPRODUCE

In real environment, I forward the SSH port of a different machine in the network that the machine I ssh into is but this example should highlight the issue as well and is simple to create a reproduction for.

  1. Create a file playbook.yaml with the following contents:
    ---
    - hosts: all
    tasks:
    - ansible.posix.synchronize:
        src: folder
        dest: /tmp/folder
  2. Create a file inventory.yaml with the following contents (replace <user> with the user to connect with):
    all:
    hosts:
    127.0.0.1:
      ansible_connection: ssh
      ansible_port: 1234
      ansible_user: <user>
  3. Create a folder named folder.
  4. Create a file named file in the folder folder.
  5. Run ssh <user>@<host> -L 127.0.0.1:1234:127.0.0.1:22 in another terminal window.
  6. Run ansible-playbook playbook.yaml -i inventory.yaml.
  7. Validate that the rsync only worked locally by checking that /tmp/folder was created on the controller node rather than the remote node.

For comparison to how it should actually work, you can change the inventory.yaml file to:

all:
  hosts:
    <host>:
      ansible_connection: ssh
      ansible_port: 22
      ansible_user: <user>
EXPECTED RESULTS

I expected the local folder to get properly synced to the remote folder.

ACTUAL RESULTS

The local folder gets synced to the local folder.

Here's the actual output: https://gist.github.com/Jackenmen/20de4a647cfb607f5a7d17232a01a998/raw/c57f02ec315fcf678db67a7034c5b7a9b751deb2/actual_result_1234.log

and for comparison, here's the expected output that I get when using <user>@<host>:22 directly: https://gist.github.com/Jackenmen/20de4a647cfb607f5a7d17232a01a998/raw/c57f02ec315fcf678db67a7034c5b7a9b751deb2/expected_result.log

Tharre commented 4 weeks ago

Interestingly you can work around this issue by using 127.0.0.2 in the inventory instead, even though of course both are loopback addresses.