Repository: ansible/ansible

Issue:
(P2)nxos_gir_profile_management: creating a profile is not idempotent
<!---
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_gir_profile_management

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.4.0 (devel 9649195c26) last updated 2017/07/24 12:53:01 (GMT -400)
  config file = None
  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.
-->
Ubuntu 14.04 

##### SUMMARY
<!--- Explain the problem briefly -->
Creating a gir profile is **not** idempotent. Deleting one is. 

##### 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
---
cli:
  host: "{{ inventory_hostname }}"
  username: "{{ nxos_cli_user | default('admin') }}"
  password: "{{ nxos_cli_pass | default('admin') }}"

nxapi:
  host: "{{ inventory_hostname }}"
  username: "{{ nxos_nxapi_user | default('admin') }}"
  password: "{{ nxos_nxapi_pass | default('admin') }}"
  transport: nxapi
  user_ssl: no

---
- hosts: nxos
  connection: local
  gather_facts: no

  tasks:
  - name: "Create maintenace mode profile"
    nxos_gir_profile_management:
      mode: maintenance
      commands:
        - router eigrp 11
        - isolate
      state: present
      provider: "{{ cli }}"

```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
The playbook should be idempotent and come back as ok=1 the second time it is applied.


##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
The play is not idempotent. `changed=1` in the output.



Discussion:
cc @GGabriele @jedelman8 @mikewiebe @privateip @rcarrillocruz @trishnaguha
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/network/nxos/nxos_gir_profile_management.py b/lib/ansible/modules/network/nxos/nxos_gir_profile_management.py
--- a/lib/ansible/modules/network/nxos/nxos_gir_profile_management.py
+++ b/lib/ansible/modules/network/nxos/nxos_gir_profile_management.py
@@ -35,7 +35,6 @@
     - Gabriele Gerbino (@GGabriele)
 notes:
     - Tested against NXOSv 7.3.(0)D1(1) on VIRL
-    - This module is not idempotent when C(state=present).
     - C(state=absent) removes the whole profile.
 options:
     commands:
@@ -70,7 +69,7 @@
 
 EXAMPLES = '''
 # Create a maintenance-mode profile
-- nxos_gir_profile:
+- nxos_gir_profile_management:
     mode: maintenance
     commands:
       - router eigrp 11
@@ -79,7 +78,7 @@
     username: "{{ un }}"
     password: "{{ pwd }}"
 # Remove the maintenance-mode profile
-- nxos_gir_profile:
+- nxos_gir_profile_management:
     mode: maintenance
     state: absent
     host: "{{ inventory_hostname }}"
@@ -146,6 +145,9 @@ def get_existing(module):
 
 def state_present(module, existing, commands):
     cmds = list()
+    if existing == commands:
+        # Idempotent case
+        return cmds
     cmds.extend(commands)
     if module.params['mode'] == 'maintenance':
         cmds.insert(0, 'configure maintenance profile maintenance-mode')
@@ -206,9 +208,10 @@ def main():
         if module.check_mode:
             module.exit_json(changed=True, commands=cmds)
         else:
-            load_config(module, cmds)
-            changed = True
-            end_state = invoke('get_existing', module)
+            if cmds:
+                load_config(module, cmds)
+                changed = True
+                end_state = invoke('get_existing', module)
 
     result['changed'] = changed
     if module._verbosity > 0:


Test patch: