# Buggy code
def ensure_state(self):
    """
    Manage the internal state of tags

    """
    results = dict(
        changed=False,
        tag_status=dict(),
    )
    changed = False
    action = self.params.get("state")
    available_tag_obj = self.get_tags_for_object(
        tag_service=self.tag_service,
        tag_assoc_svc=self.tag_association_svc,
        dobj=self.dynamic_managed_object,
    )
    # Already existing tags from the given object
    avail_tag_obj_name_list = [tag.name for tag in available_tag_obj]
    results["tag_status"]["previous_tags"] = avail_tag_obj_name_list
    results["tag_status"]["desired_tags"] = self.tag_names

    # Check if category and tag combination exists as per user request
    removed_tags_for_set = False
    for tag in self.tag_names:
        category_obj, category_name, tag_name = None, None, None
        if ":" in tag:
            # User specified category
            category_name, tag_name = tag.split(":", 1)
            category_obj = self.search_svc_object_by_name(
                self.category_service, category_name
            )
            if not category_obj:
                self.module.fail_json(
                    msg="Unable to find the category %s" % category_name
                )
        else:
            # User specified only tag
            tag_name = tag

        tag_obj = self.search_svc_object_by_name(self.tag_service, tag_name)
        if not tag_obj:
            self.module.fail_json(msg="Unable to find the tag %s" % tag_name)

        if (
            category_name
            and category_obj
            and not self.is_tag_category(category_obj, tag_obj)
        ):
            self.module.fail_json(
                msg="Category %s does not contain tag %s" % (category_name, tag_name)
            )

        if action in ("add", "present"):
            if tag_obj not in available_tag_obj:
                # Tag is not already applied
                try:
                    self.tag_association_svc.attach(
                        tag_id=tag_obj.id, object_id=self.dynamic_managed_object
                    )
                    changed = True
                except Error as error:
                    self.module.fail_json(msg="%s" % self.get_error_message(error))

        elif action == "set":
            # Remove all tags first
            try:
                if not removed_tags_for_set:
                    for av_tag in available_tag_obj:
                        self.tag_association_svc.detach(
                            tag_id=av_tag.id, object_id=self.dynamic_managed_object
                        )
                    removed_tags_for_set = True
                self.tag_association_svc.attach(
                    tag_id=tag_obj.id, object_id=self.dynamic_managed_object
                )
                changed = True
            except Error as error:
                self.module.fail_json(msg="%s" % self.get_error_message(error))

        elif action in ("remove", "absent"):
            if tag_obj in available_tag_obj:
                try:
                    self.tag_association_svc.detach(
                        tag_id=tag_obj.id, object_id=self.dynamic_managed_object
                    )
                    changed = True
                except Error as error:
                    self.module.fail_json(msg="%s" % self.get_error_message(error))

    results["tag_status"]["current_tags"] = [
        tag.name
        for tag in self.get_tags_for_object(
            self.tag_service, self.tag_association_svc, self.dynamic_managed_object
        )
    ]
    results["changed"] = changed
    self.module.exit_json(**results)


# Traceback (nTypeError)
<localhost> Attempting python interpreter discovery
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: dpelliccia
<localhost> EXEC /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'/usr/bin/python'"'"'; command -v '"'"'python3.7'"'"';                      command -v '"'"'python3.6'"'"'; command -v '"'"'python3.5'"'"'; command -v '"'"'python2.7'"'"'; command -v '"'"'python2.6'"'"'; comma                     nd -v '"'"'/usr/libexec/platform-python'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'python'"'"'; echo ENDFOUND &amp;&amp; sl                     eep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python &amp;&amp; sleep 0'
Using module file /usr/lib/python2.7/site-packages/ansible/modules/cloud/vmware/_vmware_vm_facts.py
Pipelining is enabled.
<localhost> EXEC /bin/sh -c '/usr/bin/python &amp;&amp; sleep 0'
fatal: [localhost -> localhost]: FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"module_stderr": "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py:851: InsecureRequestWarning: Unverified HTTPS reques                     t is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.ht                     ml#ssl-warnings\n  InsecureRequestWarning)\nTraceback (most recent call last):\n  File \"<stdin>\", line 102, in <module>\n  File \"<                     stdin>\", line 94, in _ansiballz_main\n  File \"<stdin>\", line 40, in invoke_module\n  File \"/usr/lib64/python2.7/runpy.py\", line                      180, in run_module\n    fname, loader, pkg_name)\n  File \"/usr/lib64/python2.7/runpy.py\", line 72, in _run_code\n    exec code in r                     un_globals\n  File \"/tmp/ansible_vmware_vm_facts_payload_x51H8H/ansible_vmware_vm_facts_payload.zip/ansible/modules/cloud/vmware/_vm                     ware_vm_facts.py\", line 322, in <module>\n  File \"/tmp/ansible_vmware_vm_facts_payload_x51H8H/ansible_vmware_vm_facts_payload.zip/a                     nsible/modules/cloud/vmware/_vmware_vm_facts.py\", line 316, in main\n  File \"/tmp/ansible_vmware_vm_facts_payload_x51H8H/ansible_vm                     ware_vm_facts_payload.zip/ansible/modules/cloud/vmware/_vmware_vm_facts.py\", line 272, in get_all_virtual_machines\n  File \"/tmp/an                     sible_vmware_vm_facts_payload_x51H8H/ansible_vmware_vm_facts_payload.zip/ansible/modules/cloud/vmware/_vmware_vm_facts.py\", line 208                     , in get_tag_info\nTypeError: get_tags_for_vm() got an unexpected keyword argument 'vm_id'\n/usr/lib/python2.7/site-packages/urllib3/                     connectionpool.py:851: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly ad                     vised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning)\n",
"module_stdout": "",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}

# Fixed code
def ensure_state(self):
    """
    Manage the internal state of tags

    """
    results = dict(
        changed=False,
        tag_status=dict(),
    )
    changed = False
    action = self.params.get("state")
    available_tag_obj = self.get_tags_for_object(
        tag_service=self.tag_service,
        tag_assoc_svc=self.tag_association_svc,
        dobj=self.dynamic_managed_object,
    )

    _temp_prev_tags = [
        "%s:%s" % (tag["category_name"], tag["name"])
        for tag in self.get_tags_for_dynamic_obj(mid=self.dynamic_managed_object)
    ]
    results["tag_status"]["previous_tags"] = _temp_prev_tags
    results["tag_status"]["desired_tags"] = self.tag_names

    # Check if category and tag combination exists as per user request
    removed_tags_for_set = False
    for tag in self.tag_names:
        category_obj, category_name, tag_name = None, None, None
        if ":" in tag:
            # User specified category
            category_name, tag_name = tag.split(":", 1)
            category_obj = self.search_svc_object_by_name(
                self.category_service, category_name
            )
            if not category_obj:
                self.module.fail_json(
                    msg="Unable to find the category %s" % category_name
                )
        else:
            # User specified only tag
            tag_name = tag

        if category_name:
            tag_obj = self.get_tag_by_category(
                tag_name=tag_name, category_name=category_name
            )
        else:
            tag_obj = self.get_tag_by_name(tag_name=tag_name)

        if not tag_obj:
            self.module.fail_json(msg="Unable to find the tag %s" % tag_name)

        if action in ("add", "present"):
            if tag_obj not in available_tag_obj:
                # Tag is not already applied
                try:
                    self.tag_association_svc.attach(
                        tag_id=tag_obj.id, object_id=self.dynamic_managed_object
                    )
                    changed = True
                except Error as error:
                    self.module.fail_json(msg="%s" % self.get_error_message(error))

        elif action == "set":
            # Remove all tags first
            try:
                if not removed_tags_for_set:
                    for av_tag in available_tag_obj:
                        self.tag_association_svc.detach(
                            tag_id=av_tag.id, object_id=self.dynamic_managed_object
                        )
                    removed_tags_for_set = True
                self.tag_association_svc.attach(
                    tag_id=tag_obj.id, object_id=self.dynamic_managed_object
                )
                changed = True
            except Error as error:
                self.module.fail_json(msg="%s" % self.get_error_message(error))

        elif action in ("remove", "absent"):
            if tag_obj in available_tag_obj:
                try:
                    self.tag_association_svc.detach(
                        tag_id=tag_obj.id, object_id=self.dynamic_managed_object
                    )
                    changed = True
                except Error as error:
                    self.module.fail_json(msg="%s" % self.get_error_message(error))

    _temp_curr_tags = [
        "%s:%s" % (tag["category_name"], tag["name"])
        for tag in self.get_tags_for_dynamic_obj(mid=self.dynamic_managed_object)
    ]
    results["tag_status"]["current_tags"] = _temp_curr_tags
    results["changed"] = changed
    self.module.exit_json(**results)