Repository: ansible/ansible

Issue:
eos_l2_interface fails when using eapi transport
<!---
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
when using the eos_l2_interface with transport eapi the play fails with 
`"msg": "CLI command 2 of 2 'sh int Ethernet35 switchport | include Switchport' failed: invalid command"`

##### 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_l2_interface

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
dt@tactools:~$ ansible --version
ansible 2.5.3
  config file = None
  configured module search path = [u'/home/dt/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Apr 10 2015, 08:09:05) [GCC 4.8.3 20140911 (Red Hat 4.8.3-7)]
dt@tactools:~$
```

##### 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).-->
N/A
##### 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.-->
N/A

##### 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
1 - hosts: all
  2   connection: local
  3   gather_facts: false
  4
  5   tasks:
  6      - name: Add / Remove Switchports
  7        eos_l2_interface:
  8          provider: "{{ provider }}"
  9          aggregate: "{{ switchports }}"
 10
 11   vars:
 12    switchports:
 13        - {name: Ethernet10, state: present, mode: trunk, native_vlan: 10, trunk_allowed_vlans: 2-50}
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
For Ethernet10 to have the desired configuration

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
dt@tactools:~$ sudo ansible-playbook -i hosts test_vlan.yml  -vvv
ansible-playbook 2.5.3
  config file = None
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible-playbook
  python version = 2.7.5 (default, Apr 10 2015, 08:09:05) [GCC 4.8.3 20140911 (Red Hat 4.8.3-7)]
No config file found; using defaults
Parsed /home/dt/hosts inventory source with ini plugin

PLAYBOOK: test_vlan.yml ***********************************************************************************************************************************************************************************************************************
1 plays in test_vlan.yml

PLAY [all] ************************************************************************************************************************************************************************************************************************************
META: ran handlers

TASK [Add / Remove Switchports] ***************************************************************************************************************************************************************************************************************
task path: /home/dt/test_vlan.yml:6
Using module file /usr/lib/python2.7/site-packages/ansible/modules/network/eos/eos_l2_interface.py
<mn100> ESTABLISH LOCAL CONNECTION FOR USER: root
<mn100> EXEC /bin/sh -c 'echo ~root && sleep 0'
<mn100> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1530641574.42-120149284331045 `" && echo ansible-tmp-1530641574.42-120149284331045="` echo /root/.ansible/tmp/ansible-tmp-1530641574.42-120149284331045 `" ) && sleep 0'
<mn100> PUT /root/.ansible/tmp/ansible-local-365jXrcwQ/tmppKKP7a TO /root/.ansible/tmp/ansible-tmp-1530641574.42-120149284331045/eos_l2_interface.py
<mn100> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1530641574.42-120149284331045/ /root/.ansible/tmp/ansible-tmp-1530641574.42-120149284331045/eos_l2_interface.py && sleep 0'
<mn100> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1530641574.42-120149284331045/eos_l2_interface.py && sleep 0'
<mn100> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1530641574.42-120149284331045/ > /dev/null 2>&1 && sleep 0'
fatal: [mn100]: FAILED! => {
    "changed": false,
    "code": 1002,
    "invocation": {
        "module_args": {
            "access_vlan": null,
            "aggregate": [
                {
                    "access_vlan": null,
                    "mode": "trunk",
                    "name": "ethernet10",
                    "native_vlan": "10",
                    "state": "present",
                    "trunk_allowed_vlans": "2-50"
                }
            ],
            "auth_pass": null,
            "authorize": true,
            "host": "mn100",
            "mode": null,
            "name": null,
            "native_vlan": null,
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "port": 443,
            "provider": {
                "auth_pass": null,
                "authorize": true,
                "host": "mn100",
                "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                "port": 443,
                "ssh_keyfile": null,
                "timeout": 300,
                "transport": "eapi",
                "use_proxy": true,
                "use_ssl": true,
                "username": "ansible",
                "validate_certs": false
            },
            "ssh_keyfile": null,
            "state": "present",
            "timeout": 300,
            "transport": "eapi",
            "trunk_allowed_vlans": null,
            "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "url_username": "ansible",
            "use_ssl": true,
            "username": "ansible",
            "validate_certs": false
        }
    },
    "msg": "CLI command 2 of 2 'sh int Ethernet35 switchport | include Switchport' failed: invalid command"
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/network/eos/eos_l2_interface.py b/lib/ansible/modules/network/eos/eos_l2_interface.py
--- a/lib/ansible/modules/network/eos/eos_l2_interface.py
+++ b/lib/ansible/modules/network/eos/eos_l2_interface.py
@@ -207,8 +207,9 @@ def map_config_to_obj(module):
     instances = list()
 
     for item in set(match):
-        command = 'sh int {0} switchport | include Switchport'
-        switchport_cfg = run_commands(module, command.format(item))[0].split(':')[1].strip()
+        command = {'command': 'show interfaces {0} switchport | include Switchport'.format(item),
+                   'output': 'text'}
+        switchport_cfg = run_commands(module, command)[0].split(':')[1].strip()
         if switchport_cfg == 'Enabled':
             state = 'present'
         else:


Test patch: