ansible-collections / ansible.netcommon

Ansible Network Collection for Common Code
GNU General Public License v3.0
144 stars 104 forks source link

httpapi using incorrect port #253

Open scottlangendyk opened 3 years ago

scottlangendyk commented 3 years ago
SUMMARY

When declaring multiple hosts that use the httpapi connection with the same IP address but different ports, outgoing requests will use the same port for all hosts.

ISSUE TYPE
COMPONENT NAME

httpapi

ANSIBLE VERSION
ansible 2.10.7
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/scottlangendyk/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/scottlangendyk/.local/lib/python3.8/site-packages/ansible
  executable location = /home/scottlangendyk/.local/bin/ansible
  python version = 3.8.5 (default, Jan 27 2021, 15:41:15) [GCC 9.3.0]
CONFIGURATION
OS / ENVIRONMENT

Distributor ID: Ubuntu Description: Ubuntu 20.04.2 LTS Release: 20.04 Codename: focal

STEPS TO REPRODUCE

I've created a github repo demonstrating the issue. Requires docker and docker-compose to spin up 3 web servers that echo the http request headers. docker-compose up is run automatically during playbook execution

https://github.com/scottlangendyk/ansible-httpapi-issue

EXPECTED RESULTS

The echod HTTP Host header should match the port specified for ansible_httpapi_port.

ACTUAL RESULTS
PLAY [localhost] *****************************************************************************************************************************************************************************************

TASK [command] *******************************************************************************************************************************************************************************************
changed: [localhost]

TASK [add_host] ******************************************************************************************************************************************************************************************
changed: [localhost]

TASK [add_host] ******************************************************************************************************************************************************************************************
changed: [localhost]

TASK [add_host] ******************************************************************************************************************************************************************************************
changed: [localhost]

PLAY [all] ***********************************************************************************************************************************************************************************************

TASK [debug] *********************************************************************************************************************************************************************************************
ok: [web3] => {
    "msg": 8003
}
ok: [web1] => {
    "msg": 8001
}
ok: [web2] => {
    "msg": 8002
}

TASK [my_module] *****************************************************************************************************************************************************************************************
changed: [web1]
changed: [web2]
changed: [web3]

TASK [debug] *********************************************************************************************************************************************************************************************
ok: [web1] => {
    "msg": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python3"
        },
        "changed": true,
        "failed": false,
        "http_result": "b'GET / HTTP/1.1\\r\\nAccept-Encoding: identity\\r\\nContent-Type: application/x-www-form-urlencoded\\r\\nContent-Length: 0\\r\\nHost: 127.0.0.1:8003\\r\\nUser-Agent: Python-urllib/3.8\\r\\nConnection: close\\r\\n\\r\\n\\r\\n\\n9b84c91d4b25\\n'"
    }
}
ok: [web2] => {
    "msg": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python3"
        },
        "changed": true,
        "failed": false,
        "http_result": "b'GET / HTTP/1.1\\r\\nAccept-Encoding: identity\\r\\nContent-Type: application/x-www-form-urlencoded\\r\\nContent-Length: 0\\r\\nHost: 127.0.0.1:8003\\r\\nUser-Agent: Python-urllib/3.8\\r\\nConnection: close\\r\\n\\r\\n\\r\\n\\n9b84c91d4b25\\n'"
    }
}
ok: [web3] => {
    "msg": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python3"
        },
        "changed": true,
        "failed": false,
        "http_result": "b'GET / HTTP/1.1\\r\\nAccept-Encoding: identity\\r\\nContent-Type: application/x-www-form-urlencoded\\r\\nContent-Length: 0\\r\\nHost: 127.0.0.1:8003\\r\\nUser-Agent: Python-urllib/3.8\\r\\nConnection: close\\r\\n\\r\\n\\r\\n\\n9b84c91d4b25\\n'"
    }
}

PLAY RECAP ***********************************************************************************************************************************************************************************************
localhost                  : ok=4    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
web1                       : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
web2                       : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
web3                       : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
scottlangendyk commented 3 years ago

Upon closer inspection seems this is caused by the way the connection hash is generated for the socket path.

https://github.com/ansible/ansible/blob/09c8faca9f8e803549a44fc79dec98c42b2917f3/lib/ansible/plugins/connection/__init__.py#L368

It's using the value from ansible_port. Setting that resolves the issue. Perhaps the httpapi connection can set ansible_port based on the value of ansible_httpapi_port.

NilashishC commented 6 months ago

@scottlangendyk Apologies for the super late response here. Just for my understanding, are you saying that it works when you set ansible_port but not ansible_httpapi_port?