Repository: ansible/ansible

Issue:
eos_config fails to disable configure session
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
<!--- Explain the problem briefly -->
If configure sessions is disabled within Ansible, it still uses configuration session on remote host
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
eos_config

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
2.7
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, 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, BELOW THIS COMMENT, 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.-->

##### 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. -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
  - name: configure interface without session
    eos_config:
      lines:
        - shutdown
      parents: interface Ethernet1
    vars:
      ansible_eos_use_sessions: 0
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Session key not present in result
```
{
    "changed": true,
    "commands": [
        "interface Ethernet1",
        "no shutdown"
    ],
    "invocation": {
        "module_args": {
            "after": null,
            "auth_pass": null,
            "authorize": null,
            "backup": false,
            "before": null,
            "defaults": false,
            "diff_against": "session",
            "diff_ignore_lines": null,
            "force": false,
            "host": null,
            "intended_config": null,
            "lines": [
                "shutdown"
            ],
            "match": "line",
            "parents": [
                "interface Ethernet1"
            ],
            "password": null,
            "port": null,
            "provider": null,
            "replace": "line",
            "running_config": null,
            "save": false,
            "save_when": "never",
            "src": null,
            "ssh_keyfile": null,
            "timeout": null,
            "transport": null,
            "use_ssl": null,
            "username": null,
            "validate_certs": null
        }
    },
    "updates": [
        "interface Ethernet1",
        "no shutdown"
    ]
}
```
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
Session key is present in result, which is not expected as eos session should be disabled
based on task vars
```
 {
    "changed": true,
    "commands": [
        "interface Ethernet1",
        "no shutdown"
    ],
    "diff": {
        "prepared": "--- system:/running-config\n+++ session:/ansible_1532722655-session-config\n@@ -20,7 +20,6 @@\n vlan 2-3\n !\n interface Ethernet1\n-   shutdown\n !\n interface Ethernet2\n    shutdown"
    },
    "invocation": {
        "module_args": {
            "after": null,
            "auth_pass": null,
            "authorize": null,
            "backup": false,
            "before": null,
            "defaults": false,
            "diff_against": "session",
            "diff_ignore_lines": null,
            "force": false,
            "host": null,
            "intended_config": null,
            "lines": [
                "no shutdown"
            ],
            "match": "line",
            "parents": [
                "interface Ethernet1"
            ],
            "password": null,
            "port": null,
            "provider": null,
            "replace": "line",
            "running_config": null,
            "save": false,
            "save_when": "never",
            "src": null,
            "ssh_keyfile": null,
            "timeout": null,
            "transport": null,
            "use_ssl": null,
            "username": null,
            "validate_certs": null
        }
    },
    "session": "ansible_1532722655",
    "updates": [
        "interface Ethernet1",
        "no shutdown"
    ]
}
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/network/eos/eos_config.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/network/eos/eos_config.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @privateip @trishnaguha
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Hi @ganeshrn,

Thank you for the issue, just so you are aware we have a dedicated Working Group for network.
You can find other people interested in this in #ansible-network on Freenode IRC
For more information about communities, meetings and agendas see https://github.com/ansible/community

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: community_workgroups --->

Resolving patch:
diff --git a/lib/ansible/plugins/connection/__init__.py b/lib/ansible/plugins/connection/__init__.py
--- a/lib/ansible/plugins/connection/__init__.py
+++ b/lib/ansible/plugins/connection/__init__.py
@@ -342,6 +342,17 @@ def close(self):
             self._connected = False
         self._implementation_plugins = []
 
+    def set_options(self, task_keys=None, var_options=None, direct=None):
+        super(NetworkConnectionBase, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct)
+        self.set_implementation_plugin_options(task_keys=task_keys, var_options=var_options, direct=direct)
+
+    def set_implementation_plugin_options(self, task_keys=None, var_options=None, direct=None):
+        '''
+        initialize implementation plugin options
+        '''
+        for plugin in self._implementation_plugins:
+            plugin.set_options(task_keys=task_keys, var_options=var_options, direct=direct)
+
     def _update_connection_state(self):
         '''
         Reconstruct the connection socket_path and check if it exists
diff --git a/lib/ansible/plugins/connection/network_cli.py b/lib/ansible/plugins/connection/network_cli.py
--- a/lib/ansible/plugins/connection/network_cli.py
+++ b/lib/ansible/plugins/connection/network_cli.py
@@ -297,7 +297,6 @@ def _connect(self):
             if self.cliconf:
                 display.vvvv('loaded cliconf plugin for network_os %s' % self._network_os, host=host)
                 self._implementation_plugins.append(self.cliconf)
-                self.cliconf.set_options()
             else:
                 display.vvvv('unable to load cliconf for network_os %s' % self._network_os)
 


Test patch:
diff --git a/test/integration/targets/eos_config/tests/cli/check_mode.yaml b/test/integration/targets/eos_config/tests/cli/check_mode.yaml
--- a/test/integration/targets/eos_config/tests/cli/check_mode.yaml
+++ b/test/integration/targets/eos_config/tests/cli/check_mode.yaml
@@ -69,5 +69,6 @@
 - assert:
    that:
    - "result.changed == true"
+   - "'session' not in result"
 
 - debug: msg="END cli/check_mode.yaml on connection={{ ansible_connection }}"