ansible-collections / ansible.netcommon

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

Variables inventory_hostname, remote_user don't contain the correct value #641

Open urohit011 opened 6 months ago

urohit011 commented 6 months ago
SUMMARY

When using the netcommon collection for connection purpose, sometimes values of variables such as, inventory_hostname, remote_user and password are somehow not being used correctly which results in the error:

[Errno -3] Temporary failure in name resolution

And I found the following entry in the ansible.log file

<inventory_hostname> ESTABLISH PARAMIKO SSH CONNECTION FOR USER: None on PORT 22 TO inventory_hostname

I think this could be happening after this PR https://github.com/ansible/ansible/pull/79704, was merged in ansible fix a very similar issue. In short the PR has changes in paramiko_ssh.py where references like connection._play_context("remote_addr") were changed to connection.get_option("remote_addr"), which is not retrieving the correct value. I think that is happening because in this following snippet, the variables variable on on line 75, only contains ansible_command_timeout:

https://github.com/ansible-collections/ansible.netcommon/blob/d28c8fcbfb7d652f769b209ce296415567fa3738/plugins/connection/persistent.py#L64-L82

When the I changed variables to the following, I didn't get the error and it worked fine for me.

variables = {
            "ansible_command_timeout": self.get_option(
                "persistent_command_timeout"
            ),
            "remote_addr": self.get_option("remote_addr"),
            "remote_user": self.get_option("remote_user"),
            "password": self.get_option("password"),
            "port": self.get_option("port"),
            "private_key_file": self.get_option("private_key_file")
        }
ISSUE TYPE
COMPONENT NAME

ansible.netcommon

ANSIBLE VERSION
ansible [core 2.15.12] and later
COLLECTION VERSION
ansible.netcommon   v5.1.1
CONFIGURATION
OS / ENVIRONMENT

ubuntu

STEPS TO REPRODUCE

Run the below playbook with ansible version 2.15.12 or later, you do not need a bigip box, you can provide a random IP, username and password

---
- name: Test
  hosts: bigip
  gather_facts: true
  connection: local

  vars:
    provider:
     server: "1.2.3.4"
     user: user
     password: pass
     validate_certs: no
     transport: cli
     server_port: 22
     no_f5_teem: yes

  tasks:
    - name: test
      bigip_command:
        commands:
          - show sys version
        provider: "{{ provider }}"

You'll get the error, [Errno -3] Temporary failure in name resolution and the below entry in ansible.log file <inventory_hostname> ESTABLISH PARAMIKO SSH CONNECTION FOR USER: None on PORT 22 TO inventory_hostname

EXPECTED RESULTS

It should not fail with the error, [Errno -3] Temporary failure in name resolution

ACTUAL RESULTS

It's failing with the error

The full traceback is:
Traceback (most recent call last):
  File "/home/ubuntu/ansible_cfgs/py310/lib/python3.10/site-packages/ansible/executor/task_executor.py", line 165, in run
    res = self._execute()
  File "/home/ubuntu/ansible_cfgs/py310/lib/python3.10/site-packages/ansible/executor/task_executor.py", line 656, in _execute
    result = self._handler.run(task_vars=vars_copy)
  File "/home/ubuntu/f5-ansible/ansible_collections/f5networks/f5_modules/plugins/action/bigip.py", line 96, in run
    out = conn.get_prompt()
  File "/home/ubuntu/ansible_cfgs/py310/lib/python3.10/site-packages/ansible/module_utils/connection.py", line 200, in __rpc__
    raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)
ansible.module_utils.connection.ConnectionError: [Errno -3] Temporary failure in name resolution
fatal: [1.2.3.4]: FAILED! => 
  msg: 'Unexpected failure during module execution: [Errno -3] Temporary failure in name resolution'
  stdout: ''
rohitthakur2590 commented 5 months ago

@urohit011 I have set/provide these variables for the connection in the action plugin itself. For instance, refer to this example: https://github.com/F5Networks/f5-ansible/pull/2413 These are the changes on netcommon side https://github.com/ansible-collections/ansible.netcommon/pull/653/files

I tested setting remote_address, and it resolved correctly on my end. Could you please confirm if this solution works for you as well?

urohit011 commented 5 months ago

Hi @rohitthakur2590, I'll check and confirm once #653 is merged. Thanks

rohitthakur2590 commented 4 months ago

@urohit011 Thanks, meanwhile could you please give detailed debug logs as well? also could you please tell us the specific versions of Netcommon and ansible core for which it was working fine before?

rohitthakur2590 commented 4 months ago

@urohit011 I believe the better and correct approach would be implementing the changes in PR #2413. Could you please verify if this works for you? As a result, no modifications are needed in the netcommon collection, so I am closing PR #653.

urohit011 commented 4 months ago

Hi @rohitthakur2590, apologies for late reply, changes in PR #2413 do not help unless the changes made in PR #653 re merged I had checked it when I had opened this issue.

urohit011 commented 3 months ago

Here's tha ansible logs

2024-07-30 16:00:30,799 p=36606 u=user n=ansible | ansible-playbook [core 2.17.2]
  config file = /Users/user/dev/ansible_cfgs/V1/ansible.cfg
  configured module search path = ['/Users/user/dev/f5-ansible/ansible_collections/f5networks/f5_modules/plugins/modules']
  ansible python module location = /Users/user/dev/py310/lib/python3.10/site-packages/ansible
  ansible collection location = /Users/user/dev/f5-ansible
  executable location = /Users/user/dev/py310/bin/ansible-playbook
  python version = 3.10.14 (main, Mar 19 2024, 21:46:16) [Clang 15.0.0 (clang-1500.1.0.2.5)] (/Users/user/dev/py310/bin/python)
  jinja version = 3.1.3
  libyaml = True
2024-07-30 16:00:30,799 p=36606 u=user n=ansible | Using /Users/user/dev/ansible_cfgs/V1/ansible.cfg as config file
2024-07-30 16:00:30,800 p=36606 u=user n=ansible | setting up inventory plugins
2024-07-30 16:00:30,943 p=36606 u=user n=ansible | Loading collection ansible.builtin from 
2024-07-30 16:00:30,952 p=36606 u=user n=ansible | Parsed /Users/user/dev/ansible_cfgs/hosts inventory source with ini plugin
2024-07-30 16:00:31,074 p=36606 u=user n=ansible | Loading collection community.general from /Users/user/dev/py310/lib/python3.10/site-packages/ansible_collections/community/general
2024-07-30 16:00:31,078 p=36606 u=user n=ansible | Loading callback plugin community.general.yaml of type stdout, v2.0 from /Users/user/dev/py310/lib/python3.10/site-packages/ansible_collections/community/general/plugins/callback/yaml.py
2024-07-30 16:00:31,095 p=36606 u=user n=ansible | Skipping callback 'default', as we already have a stdout callback.
2024-07-30 16:00:31,095 p=36606 u=user n=ansible | Skipping callback 'minimal', as we already have a stdout callback.
2024-07-30 16:00:31,095 p=36606 u=user n=ansible | Skipping callback 'oneline', as we already have a stdout callback.
2024-07-30 16:00:31,095 p=36606 u=user n=ansible | PLAYBOOK: bigip_cmd.yaml *********************************************************************************************************************************************************
2024-07-30 16:00:31,095 p=36606 u=user n=ansible | 1 plays in bigip_cmd.yaml
2024-07-30 16:00:31,096 p=36606 u=user n=ansible | PLAY [Test] **********************************************************************************************************************************************************************
2024-07-30 16:00:31,111 p=36606 u=user n=ansible | TASK [test] **********************************************************************************************************************************************************************
2024-07-30 16:00:31,131 p=36606 u=user n=ansible | redirecting (type: action) ansible.builtin.bigip to f5networks.f5_modules.bigip
2024-07-30 16:00:31,133 p=36606 u=user n=ansible | Loading collection f5networks.f5_modules from /Users/user/dev/f5-ansible/ansible_collections/f5networks/f5_modules
2024-07-30 16:00:31,134 p=36606 u=user n=ansible | <XX.XX.XX.XX> Using network group action bigip for bigip_command
2024-07-30 16:00:31,134 p=36606 u=user n=ansible | redirecting (type: action) ansible.builtin.bigip to f5networks.f5_modules.bigip
2024-07-30 16:00:31,137 p=36606 u=user n=ansible | Loading collection ansible.netcommon from /Users/user/dev/f5-ansible/ansible_collections/ansible/netcommon
2024-07-30 16:00:31,159 p=36606 u=user n=ansible | <XX.XX.XX.XX> connection transport is cli
2024-07-30 16:00:31,159 p=36606 u=user n=ansible | <XX.XX.XX.XX> using connection plugin network_cli
2024-07-30 16:00:31,160 p=36606 u=user n=ansible | redirecting (type: connection) ansible.builtin.persistent to ansible.netcommon.persistent
2024-07-30 16:00:31,163 p=36606 u=user n=ansible | <XX.XX.XX.XX> starting connection from persistent connection plugin
2024-07-30 16:00:31,163 p=36606 u=user n=ansible | Found ansible-connection at path /Users/user/dev/py310/bin/ansible-connection
2024-07-30 16:00:31,606 p=36611 u=user n=ansible | Loading collection ansible.builtin from 
2024-07-30 16:00:31,694 p=36614 u=user n=ansible | redirecting (type: connection) ansible.builtin.network_cli to ansible.netcommon.network_cli
2024-07-30 16:00:31,697 p=36614 u=user n=ansible | Loading collection ansible.netcommon from /Users/user/dev/f5-ansible/ansible_collections/ansible/netcommon
2024-07-30 16:00:31,711 p=36614 u=user n=ansible | Loading collection ansible.utils from /Users/user/dev/py310/lib/python3.10/site-packages/ansible_collections/ansible/utils
2024-07-30 16:00:31,728 p=36614 u=user n=ansible | redirecting (type: terminal) ansible.builtin.bigip to f5networks.f5_modules.bigip
2024-07-30 16:00:31,729 p=36614 u=user n=ansible | Loading collection f5networks.f5_modules from /Users/user/dev/f5-ansible/ansible_collections/f5networks/f5_modules
2024-07-30 16:00:32,416 p=36606 u=user n=ansible | <XX.XX.XX.XX> local domain socket does not exist, starting it
2024-07-30 16:00:32,417 p=36606 u=user n=ansible | <XX.XX.XX.XX> control socket path is /Users/user/.ansible/pc/9d01ffc9c1
2024-07-30 16:00:32,417 p=36606 u=user n=ansible | <XX.XX.XX.XX> Loading collection ansible.builtin from 
2024-07-30 16:00:32,417 p=36606 u=user n=ansible | <XX.XX.XX.XX> redirecting (type: connection) ansible.builtin.network_cli to ansible.netcommon.network_cli
2024-07-30 16:00:32,418 p=36606 u=user n=ansible | <XX.XX.XX.XX> Loading collection ansible.netcommon from /Users/user/dev/f5-ansible/ansible_collections/ansible/netcommon
2024-07-30 16:00:32,418 p=36606 u=user n=ansible | <XX.XX.XX.XX> Loading collection ansible.utils from /Users/user/dev/py310/lib/python3.10/site-packages/ansible_collections/ansible/utils
2024-07-30 16:00:32,418 p=36606 u=user n=ansible | <XX.XX.XX.XX> redirecting (type: terminal) ansible.builtin.bigip to f5networks.f5_modules.bigip
2024-07-30 16:00:32,418 p=36606 u=user n=ansible | <XX.XX.XX.XX> Loading collection f5networks.f5_modules from /Users/user/dev/f5-ansible/ansible_collections/f5networks/f5_modules
2024-07-30 16:00:32,418 p=36606 u=user n=ansible | <XX.XX.XX.XX> local domain socket listeners started successfully
2024-07-30 16:00:32,418 p=36606 u=user n=ansible | <XX.XX.XX.XX> unable to load cliconf for network_os bigip. Falling back to default
2024-07-30 16:00:32,418 p=36606 u=user n=ansible | <XX.XX.XX.XX> loaded cliconf plugin ansible_collections.ansible.netcommon.plugins.cliconf.default from path /Users/user/dev/f5-ansible/ansible_collections/ansible/netcommon/plugins/cliconf/default.py for network_os bigip
2024-07-30 16:00:32,419 p=36606 u=user n=ansible | network_os is set to bigip
2024-07-30 16:00:32,419 p=36606 u=user n=ansible | <XX.XX.XX.XX> ssh type is set to paramiko
2024-07-30 16:00:32,419 p=36606 u=user n=ansible | <XX.XX.XX.XX> Loading collection ansible.builtin from 

2024-07-30 16:00:32,419 p=36606 u=user n=ansible | <XX.XX.XX.XX> local domain socket path is /Users/user/.ansible/pc/9d01ffc9c1
2024-07-30 16:00:32,419 p=36606 u=user n=ansible | <XX.XX.XX.XX> socket_path: /Users/user/.ansible/pc/9d01ffc9c1
2024-07-30 16:00:32,419 p=36614 u=user n=ansible | <inventory_hostname> ESTABLISH PARAMIKO SSH CONNECTION FOR USER: None on PORT 22 TO inventory_hostname
2024-07-30 16:00:32,432 p=36614 u=user n=ansible | Traceback (most recent call last):
  File "/Users/user/dev/py310/lib/python3.10/site-packages/ansible/plugins/connection/paramiko_ssh.py", line 511, in _connect_uncached
    ssh.connect(
  File "/Users/user/dev/py310/lib/python3.10/site-packages/paramiko/client.py", line 377, in connect
    to_try = list(self._families_and_addresses(hostname, port))
  File "/Users/user/dev/py310/lib/python3.10/site-packages/paramiko/client.py", line 202, in _families_and_addresses
    addrinfos = socket.getaddrinfo(
  File "/usr/local/Cellar/python@3.10/3.10.14/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socket.py", line 955, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/user/dev/py310/lib/python3.10/site-packages/ansible/utils/jsonrpc.py", line 45, in handle_request
    result = rpc_method(*args, **kwargs)
  File "/Users/user/dev/f5-ansible/ansible_collections/ansible/netcommon/plugins/connection/network_cli.py", line 343, in wrapped
    self._connect()
  File "/Users/user/dev/f5-ansible/ansible_collections/ansible/netcommon/plugins/connection/network_cli.py", line 625, in _connect
    ssh = self.ssh_type_conn._connect()
  File "/Users/user/dev/py310/lib/python3.10/site-packages/ansible/plugins/connection/paramiko_ssh.py", line 392, in _connect
    self.ssh = SSH_CONNECTION_CACHE[cache_key] = self._connect_uncached()
  File "/Users/user/dev/py310/lib/python3.10/site-packages/ansible/plugins/connection/paramiko_ssh.py", line 537, in _connect_uncached
    raise AnsibleConnectionFailure(msg)
ansible.errors.AnsibleConnectionFailure: [Errno 8] nodename nor servname provided, or not known

2024-07-30 16:00:32,443 p=36606 u=user n=ansible | The full traceback is:
Traceback (most recent call last):
  File "/Users/user/dev/py310/lib/python3.10/site-packages/ansible/executor/task_executor.py", line 164, in run
    res = self._execute()
  File "/Users/user/dev/py310/lib/python3.10/site-packages/ansible/executor/task_executor.py", line 636, in _execute
    result = self._handler.run(task_vars=vars_copy)
  File "/Users/user/dev/f5-ansible/ansible_collections/f5networks/f5_modules/plugins/action/bigip.py", line 90, in run
    out = conn.get_prompt()
  File "/Users/user/dev/py310/lib/python3.10/site-packages/ansible/module_utils/connection.py", line 199, in __rpc__
    raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)
ansible.module_utils.connection.ConnectionError: [Errno 8] nodename nor servname provided, or not known

2024-07-30 16:00:32,443 p=36606 u=user n=ansible | fatal: [XX.XX.XX.XX]: FAILED! => 
  msg: 'Unexpected failure during module execution: [Errno 8] nodename nor servname provided, or not known'
  stdout: ''
2024-07-30 16:00:32,444 p=36606 u=user n=ansible | PLAY RECAP ***********************************************************************************************************************************************************************
2024-07-30 16:00:32,444 p=36606 u=user n=ansible | XX.XX.XX.XX             : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
2024-07-30 16:00:45,646 p=36541 u=user n=ansible | persistent connection idle timeout triggered, timeout value is 30 secs.
See the timeout setting options in the Network Debug and Troubleshooting Guide.
2024-07-30 16:00:45,752 p=36541 u=user n=ansible | shutdown complete
2024-07-30 16:01:02,434 p=36614 u=user n=ansible | persistent connection idle timeout triggered, timeout value is 30 secs.
See the timeout setting options in the Network Debug and Troubleshooting Guide.
2024-07-30 16:01:02,538 p=36614 u=user n=ansible | shutdown complete