Repository: ansible-collections/community.vmware

Issue:
vmware_host_active_directory: Problems with corner cases
##### SUMMARY
When there's a problem with the domain membership of a host, the module always reports a `change` but doesn't do anything (lines 160ff).

We ran into this issue when we accidentally removed the computer account of an existing ESXi host. Our playbook reported changing the AD membership over and over again which was a bit weird. After all, after the first change I'd expect an `OK` on all later runs.

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
vmware_host_active_directory

##### ANSIBLE VERSION
```
ansible 2.9.6
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.7.5 (default, Nov 14 2019, 00:43:46) [GCC 7.3.0]
```

##### CONFIGURATION
```
```

##### STEPS TO REPRODUCE
Join an ESXi host to AD, remove the computer account and run the task to join AD several times. It always reports `changed` but nothing happens.

##### EXPECTED RESULTS
I'm not sure... there are so many different problems that can occur (`clientTrustBroken`, `inconsistentTrust`, `noServers`, `serverTrustBroken` or even `otherProblem ` or `unknown`) that it's probably impossible to deal with them. Maybe the module should simply fail in these cases with an appropriate error message. Something like "The ESXi host seems to be an AD member but reports an `unkown` problem". This would make it pretty obvious that manual troubleshooting is needed.

##### ACTUAL RESULTS
The module reports a change but doesn't do anything.



Discussion:
I agree with you @mariolenz. This should not happen.

Resolving patch:
diff --git a/plugins/modules/vmware_host_active_directory.py b/plugins/modules/vmware_host_active_directory.py
--- a/plugins/modules/vmware_host_active_directory.py
+++ b/plugins/modules/vmware_host_active_directory.py
@@ -162,14 +162,13 @@ def ensure(self):
                         results['result'][host.name]['membership_state'] = active_directory_info.domainMembershipStatus
                         results['result'][host.name]['joined_domain'] = active_directory_info.joinedDomain
                         results['result'][host.name]['trusted_domains'] = active_directory_info.trustedDomain
-                        msg = "Host is joined to AD domain, but "
+                        msg = host.name + " is joined to AD domain, but "
                         if active_directory_info.domainMembershipStatus == 'clientTrustBroken':
                             msg += "the client side of the trust relationship is broken"
                         elif active_directory_info.domainMembershipStatus == 'inconsistentTrust':
                             msg += "unexpected domain controller responded"
                         elif active_directory_info.domainMembershipStatus == 'noServers':
-                            msg += "the host thinks it's part of a domain and " \
-                                "no domain controllers could be reached to confirm"
+                            msg += "no domain controllers could be reached to confirm"
                         elif active_directory_info.domainMembershipStatus == 'serverTrustBroken':
                             msg += "the server side of the trust relationship is broken (or bad machine password)"
                         elif active_directory_info.domainMembershipStatus == 'otherProblem':
@@ -177,6 +176,7 @@ def ensure(self):
                         elif active_directory_info.domainMembershipStatus == 'unknown':
                             msg += "the Active Directory integration provider does not support domain trust checks"
                         results['result'][host.name]['msg'] = msg
+                        self.module.fail_json(msg=msg)
                 # Enable and join AD domain
                 else:
                     if self.module.check_mode:


Test patch: