Repository: ansible/ansible

Issue:
nxos_evpn_vni state present not working
<!---
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 -->
nxos_evpn_vni

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.4.0
  config file = /etc/ansible/ansible.cfg
  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 = /usr/bin/ansible
  python version = 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]

```

##### CONFIGURATION
<!---
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.
-->
RHEL 7.2

##### SUMMARY
<!--- Explain the problem briefly -->

Setting evpn configs as state:absent. Ansible outputs "ok" not "changed". Checked NxOS and configs were not deleted. 

##### 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 -->
```
      - name: Ensure EVPN is not on device
        nxos_evpn_vni:
          vni: "{{ vnid }}"
          username: un
          password: pwd
          host: "{{ item }}"
          state: absent
          transport: cli
        with_items:
          - "{{ leaf1ip }}"
          - "{{ leaf2ip }}"
          - "{{ leaf3ip }}"
          - "{{ leaf4ip }}"
        register: delevpn
        ignore_errors: yes
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
```
changed: [localhost] => (item=leaf1)
changed: [localhost] => (item=leaf2)
changed: [localhost] => (item=leaf3)
changed: [localhost] => (item=leaf4)
```
expected configs deleted when playbook is ran



##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
output below is the same for the four items
```
<127.0.0.1> connection transport is cli
<> using connection plugin network_cli
<> socket_path: /root/.ansible/pc/531159983e
open_shell() returned 0 ok
Using module file /usr/lib/python2.7/site-packages/ansible/modules/network/nxos/nxos_evpn_vni.py
<127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1495645321.48-36455347467130 `" && echo ansible-tmp-1495645321.48-36455347467130="` echo /root/.ansible/tmp/ansible-tmp-1495645321.48-36455347467130 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmp10V3Dw TO /root/.ansible/tmp/ansible-tmp-1495645321.48-36455347467130/nxos_evpn_vni.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1495645321.48-36455347467130/ /root/.ansible/tmp/ansible-tmp-1495645321.48-36455347467130/nxos_evpn_vni.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1495645321.48-36455347467130/nxos_evpn_vni.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1495645321.48-36455347467130/" > /dev/null 2>&1 && sleep 0'
ok: [localhost] => (item=) => {
    "changed": false,
    "commands": [],
    "invocation": {
        "module_args": {
            "config": null,
            "host": "",
            "include_defaults": "True",
            "password": "",
            "port": null,
            "provider": null,
            "route_distinguisher": null,
            "route_target_both": null,
            "route_target_export": null,
            "route_target_import": null,
            "save": false,
            "ssh_keyfile": null,
            "state": "absent",
            "timeout": 10,
            "transport": "cli",
            "use_ssl": null,
            "username": "",
            "validate_certs": null,
            "vni": "5304"
        }
    },
    "item": "",
    "warnings": [
        "argument username has been deprecated and will be removed in a future version",
        "argument host has been deprecated and will be removed in a future version",
        "argument password has been deprecated and will be removed in a future version"
    ]
}

```



Discussion:
@trishnaguha 
cc @jedelman8 @GGabriele @rahushen @privateip @rcarrillocruz @mikewiebe
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@donsantos Can you please make sure that the config is already present on your device or not?
@trishnaguha 
this is the config for creating it and output 

```
    - name: Configure EVPN
      nxos_evpn_vni:
        vni: "{{ vnid }}"
        route_distinguisher: auto
        route_target_import: auto
        route_target_export: auto
        username: un
        password: pwd
        host: "{{ item }}"
        state: present
        transport: cli
      with_items:
        - "{{ leaf1ip }}"
        - "{{ leaf2ip }}"
        - "{{ leaf3ip }}"
        - "{{ leaf4ip }}"
      register: evpnconfig
      ignore_errors: yes
```

```
changed: [localhost] => (item=) => {
    "changed": true,
    "commands": [
        "evpn",
        "vni 5304 l2"
    ],
    "invocation": {
        "module_args": {
            "config": null,
            "host": "",
            "include_defaults": "True",
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "port": null,
            "provider": null,
            "route_distinguisher": "auto",
            "route_target_both": null,
            "route_target_export": [
                "auto"
            ],
            "route_target_import": [
                "auto"
            ],
            "save": false,
            "ssh_keyfile": null,
            "state": "present",
            "timeout": 10,
            "transport": "cli",
            "use_ssl": null,
            "username": "",
            "validate_certs": null,
            "vni": "5304"
        }
    },
    "item": "",
    "warnings": [
        "argument username has been deprecated and will be removed in a future version",
        "argument host has been deprecated and will be removed in a future version",
        "argument password has been deprecated and will be removed in a future version"
    ]
}
```

After creation I ran nxos_evpn_vni with state: absent and received "ok" as the output

Resolving patch:
diff --git a/lib/ansible/modules/network/nxos/nxos_evpn_vni.py b/lib/ansible/modules/network/nxos/nxos_evpn_vni.py
--- a/lib/ansible/modules/network/nxos/nxos_evpn_vni.py
+++ b/lib/ansible/modules/network/nxos/nxos_evpn_vni.py
@@ -220,9 +220,8 @@ def state_present(module, existing, proposed):
             command = '{0} {1}'.format(key, value)
             commands.append(command)
 
-    else:
-        commands = ['vni {0} l2'.format(module.params['vni'])]
-        parents = ['evpn']
+    if commands:
+        parents = ['evpn', 'vni {0} l2'.format(module.params['vni'])]
 
     return commands, parents
 


Test patch:
diff --git a/test/units/modules/network/nxos/test_nxos_evpn_vni.py b/test/units/modules/network/nxos/test_nxos_evpn_vni.py
--- a/test/units/modules/network/nxos/test_nxos_evpn_vni.py
+++ b/test/units/modules/network/nxos/test_nxos_evpn_vni.py
@@ -50,9 +50,13 @@ def load_fixtures(self, commands=None):
         self.load_config.return_value = None
 
     def test_nxos_evpn_vni_present(self):
-        set_module_args(dict(vni='6000', state='present'))
+        set_module_args(dict(vni='6000',
+                             route_target_import='5000:10',
+                             state='present'))
         result = self.execute_module(changed=True)
-        self.assertEqual(result['commands'], ['evpn', 'vni 6000 l2'])
+        self.assertEqual(result['commands'], ['evpn',
+                                              'vni 6000 l2',
+                                              'route-target import 5000:10'])
 
     def test_nxos_evpn_vni_absent_not_existing(self):
         set_module_args(dict(vni='12000', state='absent'))