Repository: ansible/ansible

Issue:
nxos_vrf_af issues
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and devel branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest -->
 - Bug Report
##### COMPONENT NAME
<!---
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
-->
nxos_vrf_af
##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.0 (devel fed20b825f) last updated 2018/02/15 12:51:12 (GMT -400)
  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 = /root/agents-ci/ansible/lib/ansible
  executable location = /root/agents-ci/ansible/bin/ansible
  python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]
```

##### 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.
-->
Ansible Server : Ubuntu 14.04
Device: N9K running 7.0(3)I7(2)
##### 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.
-->
* There is a minor issue with nxos_vrf_af module. When we do not specify "route_target_both_auto_evpn" parameter in the playbook it is taking as False and removing the configuration.
* The integration test cases are not complete and also platform differences need to be addressed.

Note: I will open a PR shortly to address these above issues


Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/network/nxos/nxos_vrf_af.py b/lib/ansible/modules/network/nxos/nxos_vrf_af.py
--- a/lib/ansible/modules/network/nxos/nxos_vrf_af.py
+++ b/lib/ansible/modules/network/nxos/nxos_vrf_af.py
@@ -124,14 +124,14 @@ def main():
 
         if current:
             have = 'route-target both auto evpn' in current
-            want = bool(module.params['route_target_both_auto_evpn'])
-
-            if want and not have:
-                commands.append('address-family %s unicast' % module.params['afi'])
-                commands.append('route-target both auto evpn')
-            elif have and not want:
-                commands.append('address-family %s unicast' % module.params['afi'])
-                commands.append('no route-target both auto evpn')
+            if module.params['route_target_both_auto_evpn'] is not None:
+                want = bool(module.params['route_target_both_auto_evpn'])
+                if want and not have:
+                    commands.append('address-family %s unicast' % module.params['afi'])
+                    commands.append('route-target both auto evpn')
+                elif have and not want:
+                    commands.append('address-family %s unicast' % module.params['afi'])
+                    commands.append('no route-target both auto evpn')
 
         else:
             commands.append('address-family %s unicast' % module.params['afi'])


Test patch:
diff --git a/test/integration/targets/nxos_vrf_af/tests/common/sanity.yaml b/test/integration/targets/nxos_vrf_af/tests/common/sanity.yaml
--- a/test/integration/targets/nxos_vrf_af/tests/common/sanity.yaml
+++ b/test/integration/targets/nxos_vrf_af/tests/common/sanity.yaml
@@ -13,18 +13,20 @@
   nxos_config:
     commands: "feature nv overlay"
     provider: "{{ connection }}"
+  ignore_errors: yes
 
 - name: Configure nv overlay evpn
   nxos_config:
     commands: "nv overlay evpn"
     provider: "{{ connection }}"
+  ignore_errors: yes
 
 - block:
-  - name: Configure vrf af
-    nxos_vrf_af: &configure
+  - name: Configure vrf af ipv4
+    nxos_vrf_af: &configure4
       vrf: ansible
       afi: ipv4
-      route_target_both_auto_evpn: true
+      route_target_both_auto_evpn: True
       provider: "{{ connection }}"
     register: result
 
@@ -33,18 +35,66 @@
         - "result.changed == true"
 
   - name: "Conf Idempotence"
-    nxos_vrf_af: *configure
+    nxos_vrf_af: *configure4
     register: result
 
   - assert: &false
       that:
         - "result.changed == false"
 
-  - name: Remove vrf af
-    nxos_vrf_af: &remove
+  - name: Configure vrf af ipv6
+    nxos_vrf_af: &configure6
+      vrf: ansible
+      afi: ipv6
+      route_target_both_auto_evpn: True
+      provider: "{{ connection }}"
+    register: result
+
+  - assert: *true
+
+  - name: "Conf Idempotence"
+    nxos_vrf_af: *configure6
+    register: result
+
+  - assert: *false
+
+  - name: Remove router target4
+    nxos_vrf_af: &rrt4
       vrf: ansible
       afi: ipv4
-      route_target_both_auto_evpn: true
+      route_target_both_auto_evpn: False
+      provider: "{{ connection }}"
+    register: result
+
+  - assert: *true
+
+  - name: "Conf Idempotence"
+    nxos_vrf_af: *rrt4
+    register: result
+
+  - assert: *false
+
+  - name: Remove router target6
+    nxos_vrf_af: &rrt6
+      vrf: ansible
+      afi: ipv6
+      route_target_both_auto_evpn: False
+      provider: "{{ connection }}"
+    register: result
+
+  - assert: *true
+
+  - name: "Conf Idempotence"
+    nxos_vrf_af: *rrt6
+    register: result
+
+  - assert: *false
+
+  - name: Remove vrf af v6
+    nxos_vrf_af: &remove6
+      vrf: ansible
+      afi: ipv6
+      route_target_both_auto_evpn: True
       state: absent
       provider: "{{ connection }}"
     register: result
@@ -55,21 +105,37 @@
       seconds: 30
 
   - name: "Remove Idempotence"
-    nxos_vrf_af: *remove
+    nxos_vrf_af: *remove6
     register: result
 
   - assert: *false
 
-  always:
-  - name: Remove feature bgp
-    nxos_feature:
-      feature: bgp
-      state: disabled
+  - name: Remove vrf af v4
+    nxos_vrf_af: &remove4
+      vrf: ansible
+      afi: ipv4
+      route_target_both_auto_evpn: True
+      state: absent
       provider: "{{ connection }}"
+    register: result
 
-  - name: Remove feature nv overlay
+  - assert: *true
+
+  - pause:
+      seconds: 30
+
+  - name: "Remove Idempotence"
+    nxos_vrf_af: *remove4
+    register: result
+
+  - assert: *false
+
+  when: not platform is search("N35")
+
+  always:
+  - name: Remove vrf
     nxos_config:
-      commands: "no feature nv overlay"
+      commands: "no vrf context ansible"
       provider: "{{ connection }}"
     ignore_errors: yes
 
@@ -79,4 +145,16 @@
       provider: "{{ connection }}"
     ignore_errors: yes
 
+  - name: Remove feature nv overlay
+    nxos_config:
+      commands: "no feature nv overlay"
+      provider: "{{ connection }}"
+    ignore_errors: yes
+
+  - name: Remove feature bgp
+    nxos_feature:
+      feature: bgp
+      state: disabled
+      provider: "{{ connection }}"
+
 - debug: msg="END connection={{ ansible_connection }} nxos_vrf_af sanity test"