Repository: ansible-collections/community.vmware

Issue:
vmware_vmkernel: incorrectly handles idempotence for Repl and Repl_NFC services
---
name: 🐛 Bug report
about: Create a report to help us improve
---
<!--- Verify first that your issue is not already reported on GitHub -->
<!--- Also test if the latest release and devel branch are affected too -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
<!--- Explain the problem briefly below -->
vmware_vmkernel incorrectly handles Repl and Repl_NFC services

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
Module: vmware_vmkernel

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below
ansible 2.9.1
  config file = None
  configured module search path = ['/ansible/playbooks/library']
  ansible python module location = /usr/lib/python3.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.7.5 (default, Oct 17 2019, 12:25:15) [GCC 8.3.0]
```

##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
```paste below
DEFAULT_MODULE_PATH(env: ANSIBLE_LIBRARY) = ['/ansible/playbooks/library']
DEFAULT_MODULE_UTILS_PATH(env: ANSIBLE_MODULE_UTILS) = ['/ansible/playbooks/library/module_utils']
```

##### OS / ENVIRONMENT
<!--- Provide all relevant information below, e.g. target OS versions, network device firmware, etc. -->
VMware vCenter 6.7
ESXi 6.7

##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->
Playbook that enables replication and replication_nfc.
Run playbook and it will configure VMK correctly.
Running again will also show changed even when services are correctly configured,
<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Make VMK port with Replication services enabled
    vmware_vmkernel:
      hostname: "{{ vcenter_server }}"
      username: "{{ vc_service_account }}"
      password: "{{ vc_service_account_password }}"
      esxi_hostname: "{{ inventory_hostname }}"
      validate_certs: False
      dvswitch_name: "{{ dvs_name }}"
      portgroup_name: "{{ replication_pg  }}"
      network:
        type: 'static'
        ip_address: "{{ vmk_rep_ip }}"
        subnet_mask: "{{ vmk_rep_subnetmask }}"
      enable_replication: True
      enable_replication_nfc: True
      mtu: 9000
      state: present
    delegate_to: localhost
```

<!--- HINT: You can paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->
When Repl and Repl_NFC services are enabled the playbook is idempotent.

##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->
Even execution of the playbook shows changed for VMkernel Adapter services would be updated
<!--- Paste verbatim command output between quotes -->
```paste below
TASK [esxi-makevmk : Make VMK port with Replication services enabled] *********************************************************************
changed: [esxi-host.company.com -> localhost] => {"changed": true, "device": "vmk5", "ipv4": "static", "ipv4_gw": "No override", "ipv4_ip": "192.168.8.17", "ipv4_sm": "255.255.255.0", "msg": "VMkernel Adapter services would be updated", "mtu": 9000, "portgroup": "replication_pg", "services": "Repl, Repl_NFC", "services_previous": "Repl, Repl_NFC", "switch": "dvswitch", "tcpip_stack": "default"}

```


Discussion:
Files identified in the description:
* [`plugins/modules/vmware_vmkernel.py`](https://github.com/ansible-collections/vmware/blob/main/plugins/modules/vmware_vmkernel.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @Akasurde @Tomorrow9 @goneri @lparkes @pgbidkar @warthog9
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Issue appears to be with the following code block
```
if (self.enable_replication and self.vnic.device not in service_type_vmks['vSphereReplication']) or \
                    (not **self.enable_provisioning** and self.vnic.device in service_type_vmks['vSphereReplication']):
                changed_services = changed_service_rep = True

            if (self.enable_replication_nfc and self.vnic.device not in service_type_vmks['vSphereReplicationNFC']) or \
                    (not **self.enable_provisioning** and self.vnic.device in service_type_vmks['vSphereReplicationNFC']):
                changed_services = changed_service_rep_nfc = True
```
Should be 
```
if (self.enable_replication and self.vnic.device not in service_type_vmks['vSphereReplication']) or \
                    (not self.enable_replication and self.vnic.device in service_type_vmks['vSphereReplication']):
                changed_services = changed_service_rep = True

            if (self.enable_replication_nfc and self.vnic.device not in service_type_vmks['vSphereReplicationNFC']) or \
                    (not self.enable_replication_nfc and self.vnic.device in service_type_vmks['vSphereReplicationNFC']):
                changed_services = changed_service_rep_nfc = True
```

Resolving patch:
diff --git a/plugins/modules/vmware_vmkernel.py b/plugins/modules/vmware_vmkernel.py
--- a/plugins/modules/vmware_vmkernel.py
+++ b/plugins/modules/vmware_vmkernel.py
@@ -614,11 +614,11 @@ def host_vmk_update(self):
                 changed_services = changed_service_prov = True
 
             if (self.enable_replication and self.vnic.device not in service_type_vmks['vSphereReplication']) or \
-                    (not self.enable_provisioning and self.vnic.device in service_type_vmks['vSphereReplication']):
+                    (not self.enable_replication and self.vnic.device in service_type_vmks['vSphereReplication']):
                 changed_services = changed_service_rep = True
 
             if (self.enable_replication_nfc and self.vnic.device not in service_type_vmks['vSphereReplicationNFC']) or \
-                    (not self.enable_provisioning and self.vnic.device in service_type_vmks['vSphereReplicationNFC']):
+                    (not self.enable_replication_nfc and self.vnic.device in service_type_vmks['vSphereReplicationNFC']):
                 changed_services = changed_service_rep_nfc = True
             if changed_services:
                 changed_list.append("services")


Test patch: