Repository: ansible/ansible

Issue:
Persistent reset_connection meta fails with stacktrace
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report


##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
<!--- Please do not include extra details here, e.g. "vyos_command" not "the network module vyos_command"

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
2.5
```

##### CONFIGURATION
<!---
If using Ansible 2.4 or above, paste the results of "ansible-config dump --only-changed"

Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).

-->

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->

##### SUMMARY
<!--- Explain the problem briefly -->

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->
Add below task in `prepare_junos_tests` integration test
<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
- debug: msg="START prepare_junos_tests/main.yaml"

- name: reset persistent connection
  meta: reset_connection

- name: Ensure netconf is enabled
  junos_netconf:
    state: present
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Should run without errors

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
ERROR! Unexpected Exception, this is probably a bug: socket_path does not exist or cannot be found
the full traceback was:

Traceback (most recent call last):
  File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/bin/ansible-playbook", line 109, in <module>
    exit_code = cli.run()
  File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/lib/ansible/cli/playbook.py", line 130, in run
    results = pbex.run()
  File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/lib/ansible/executor/playbook_executor.py", line 154, in run
    result = self._tqm.run(play=play)
  File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/lib/ansible/executor/task_queue_manager.py", line 290, in run
    play_return = strategy.run(iterator, play_context)
  File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/lib/ansible/plugins/strategy/linear.py", line 235, in run
    results.extend(self._execute_meta(task, play_context, iterator, host))
  File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/lib/ansible/plugins/strategy/__init__.py", line 916, in _execute_meta
    connection.reset()
  File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/lib/ansible/module_utils/connection.py", line 140, in __rpc__
    response = self._exec_jsonrpc(name, *args, **kwargs)
  File "/var/lib/nodepool/ansible_testing/ganeshrn/ansible/ansible/lib/ansible/module_utils/connection.py", line 115, in _exec_jsonrpc
    raise ConnectionError('socket_path does not exist or cannot be found')
ConnectionError: socket_path does not exist or cannot be found
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/plugins/strategy/__init__.py b/lib/ansible/plugins/strategy/__init__.py
--- a/lib/ansible/plugins/strategy/__init__.py
+++ b/lib/ansible/plugins/strategy/__init__.py
@@ -913,8 +913,12 @@ def _evaluate_conditional(h):
                 play_context.set_options_from_plugin(connection)
 
             if connection:
-                connection.reset()
-                msg = 'reset connection'
+                try:
+                    connection.reset()
+                    msg = 'reset connection'
+                except ConnectionError as e:
+                    # most likely socket is already closed
+                    display.debug("got an error while closing persistent connection: %s" % e)
             else:
                 msg = 'no connection, nothing to reset'
         else:


Test patch: