Repository: ansible/ansible

Issue:
eos_eapi doesn't enable non-default vrfs if the default vrf is already configured
<!---
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
 - Bug Report
 

##### COMPONENT NAME
eos_eapi

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.4.0.0
  config file = /Users/dt/eosplus/eos-ansible-tower-demo/ansible.cfg
  configured module search path = [u'/Users/dt/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.11 (default, Nov 13 2016, 20:35:45) [GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)]
```

##### CONFIGURATION


n/A

##### OS / ENVIRONMENT
N/A
##### SUMMARY
<!--- Explain the problem briefly -->
When a eos device has the eapi feature configured in the default vrf (i.e the running config has this):

```
skinny...04:51:06(config)#show run section api
!
management api http-commands
   no shutdown
```
When you run the following task:

```
 tasks:
    - name: enable api in non-default vrf
      eos_eapi:
          provider: "{{ provider }}"
          state: started
          vrf: management
```

The non-default vrf doesn't get the 'no shutdown' command

so you end up with a state like this:

```
changed: [cd262.sjc.aristanetworks.com] => {
    "ansible_facts": {
        "eos_eapi_urls": {
            "Management1": [
                "https://172.28.160.178:443"
            ],
            "Vlan300": [
                "https://1.1.1.253:443"
            ]
        }
    },
    "changed": true,
    "commands": [
        "management api http-commands",
        "vrf management"
    ],
```

On the switch there is only the default one remains, since the command sent via ansible does not no shut the non-default vrf.



##### 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

---
- hosts: cd262.sjc.aristanetworks.com
  gather_facts: no
  connection: local
  vars:
      run_commands:
          - 'show running-config'

  tasks:
    - name: eapi non-default vrf
      eos_eapi:
          provider: "{{ provider }}"
          state: started
          vrf: management

```



##### EXPECTED RESULTS


The play should send a no shutdown command for the non-default vrf

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
00:54:32 [dt:~/eosplus/eos-ansible-tower-demo] master(+53/-32)+* ± ansible-playbook -i hosts run_commands.yaml -vvv
ansible-playbook 2.4.0.0
  config file = /Users/dt/eosplus/eos-ansible-tower-demo/ansible.cfg
  configured module search path = [u'/Users/dt/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/site-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 2.7.11 (default, Nov 13 2016, 20:35:45) [GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)]
Using /Users/dt/eosplus/eos-ansible-tower-demo/ansible.cfg as config file
Parsed /Users/dt/eosplus/eos-ansible-tower-demo/hosts inventory source with ini plugin

PLAYBOOK: run_commands.yaml ************************************************************************************************************************************************
1 plays in run_commands.yaml

PLAY [cd262.sjc.aristanetworks.com] ****************************************************************************************************************************************
META: ran handlers

TASK [Run Specified Commands] **********************************************************************************************************************************************
task path: /Users/dt/eosplus/eos-ansible-tower-demo/run_commands.yaml:10
<cd262.sjc.aristanetworks.com> using connection plugin network_cli
Using module file /usr/local/lib/python2.7/site-packages/ansible/modules/network/eos/eos_eapi.py
<cd262.sjc.aristanetworks.com> ESTABLISH LOCAL CONNECTION FOR USER: dt
<cd262.sjc.aristanetworks.com> EXEC /bin/sh -c 'echo ~ && sleep 0'
<cd262.sjc.aristanetworks.com> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/dt/.ansible/tmp/ansible-tmp-1508907283.99-63067192211963 `" && echo ansible-tmp-1508907283.99-63067192211963="` echo /Users/dt/.ansible/tmp/ansible-tmp-1508907283.99-63067192211963 `" ) && sleep 0'
<cd262.sjc.aristanetworks.com> PUT /var/folders/vw/2_f0pb_s40z3gxbzqs3s55980000gp/T/tmpyiZlmK TO /Users/dt/.ansible/tmp/ansible-tmp-1508907283.99-63067192211963/eos_eapi.py
<cd262.sjc.aristanetworks.com> EXEC /bin/sh -c 'chmod u+x /Users/dt/.ansible/tmp/ansible-tmp-1508907283.99-63067192211963/ /Users/dt/.ansible/tmp/ansible-tmp-1508907283.99-63067192211963/eos_eapi.py && sleep 0'
<cd262.sjc.aristanetworks.com> EXEC /bin/sh -c 'python /Users/dt/.ansible/tmp/ansible-tmp-1508907283.99-63067192211963/eos_eapi.py; rm -rf "/Users/dt/.ansible/tmp/ansible-tmp-1508907283.99-63067192211963/" > /dev/null 2>&1 && sleep 0'
changed: [cd262.sjc.aristanetworks.com] => {
    "ansible_facts": {
        "eos_eapi_urls": {
            "Management1": [
                "https://172.28.160.178:443"
            ],
            "Vlan300": [
                "https://1.1.1.253:443"
            ]
        }
    },
    "changed": true,
    "commands": [
        "management api http-commands",
        "vrf management"
    ],
    "failed": false,
    "invocation": {
        "module_args": {
            "auth_pass": null,
            "authorize": null,
            "config": null,
            "host": "cd262.sjc.aristanetworks.com",
            "http": null,
            "http_port": null,
            "https": null,
            "https_port": null,
            "local_http": null,
            "local_http_port": null,
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "port": null,
            "provider": {
                "auth_pass": null,
                "authorize": null,
                "host": "cd262.sjc.aristanetworks.com",
                "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                "port": null,
                "ssh_keyfile": null,
                "timeout": null,
                "transport": "cli",
                "use_ssl": true,
                "username": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                "validate_certs": false
            },
            "socket": null,
            "ssh_keyfile": null,
            "state": "started",
            "timeout": null,
            "transport": "cli",
            "use_ssl": true,
            "username": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "validate_certs": false,
            "vrf": "management"
        }
    },
    "session_name": "ansible_1508907284"
```



Discussion:
cc @privateip @trishnaguha
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
looks like the check on whether the no shutdown command is entered needs to see if the current vrf is the one that the user is trying to enable as well
resolved_by_pr: 32112

Resolving patch:
diff --git a/lib/ansible/modules/network/eos/eos_eapi.py b/lib/ansible/modules/network/eos/eos_eapi.py
--- a/lib/ansible/modules/network/eos/eos_eapi.py
+++ b/lib/ansible/modules/network/eos/eos_eapi.py
@@ -260,11 +260,17 @@ def add(cmd):
         else:
             add('protocol unix-socket')
 
+    if needs_update('state') and not needs_update('vrf'):
+        if want['state'] == 'stopped':
+            add('shutdown')
+        elif want['state'] == 'started':
+            add('no shutdown')
+
 
     if needs_update('vrf'):
         add('vrf %s' % want['vrf'])
-
-    if needs_update('state'):
+        # switching operational vrfs here
+        # need to add the desired state as well
         if want['state'] == 'stopped':
             add('shutdown')
         elif want['state'] == 'started':


Test patch:
diff --git a/test/units/modules/network/eos/test_eos_eapi.py b/test/units/modules/network/eos/test_eos_eapi.py
--- a/test/units/modules/network/eos/test_eos_eapi.py
+++ b/test/units/modules/network/eos/test_eos_eapi.py
@@ -134,6 +134,11 @@ def test_eos_eapi_vrf(self):
         commands = ['management api http-commands', 'vrf test', 'no shutdown']
         self.start_unconfigured(changed=True, commands=commands)
 
+    def test_eos_eapi_change_from_default_vrf(self):
+        set_module_args(dict(vrf='test'))
+        commands = ['management api http-commands', 'vrf test', 'no shutdown']
+        self.start_configured(changed=True, commands=commands)
+
     def test_eos_eapi_vrf_missing(self):
         set_module_args(dict(vrf='missing'))
         self.start_unconfigured(failed=True)