Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vmware_tag_manager always returns changed: True when in set mode. #1265

Closed
spectras opened this issue Mar 26, 2022 · 1 comment · Fixed by #1465
Closed

vmware_tag_manager always returns changed: True when in set mode. #1265

spectras opened this issue Mar 26, 2022 · 1 comment · Fixed by #1465

Comments

@spectras
Copy link

SUMMARY

vmware_tag_manager always returns changed: True when in set mode.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

plugins/modules/vmware_tag_manager.py

ANSIBLE VERSION
ansible [core 2.12.2]
  config file = /mnt/d/Workspace/infrastructure/configuration/ansible.cfg
  configured module search path = ['/mnt/d/Workspace/infrastructure/configuration/library']
  ansible python module location = /mnt/d/Workspace/infrastructure/configuration/.env/lib/python3.9/site-packages/ansible
  ansible collection location = /usr/share/ansible/collections
  executable location = /mnt/d/Workspace/infrastructure/configuration/.env/bin/ansible
  python version = 3.9.7 (default, Sep 10 2021, 14:59:43) [GCC 11.2.0]
  jinja version = 3.0.3
  libyaml = True
COLLECTION VERSION

# /mnt/d/Workspace/infrastructure/configuration/.env/lib/python3.9/site-packages/ansible_collections
Collection       Version
---------------- -------
community.vmware 1.17.1
CONFIGURATION
ANSIBLE_PIPELINING(/mnt/d/Workspace/infrastructure/configuration/ansible.cfg) = True
DEFAULT_FORCE_HANDLERS(/mnt/d/Workspace/infrastructure/configuration/ansible.cfg) = True
DEFAULT_GATHERING(/mnt/d/Workspace/infrastructure/configuration/ansible.cfg) = smart
DEFAULT_PRIVATE_ROLE_VARS(/mnt/d/Workspace/infrastructure/configuration/ansible.cfg) = True
DISPLAY_SKIPPED_HOSTS(/mnt/d/Workspace/infrastructure/configuration/ansible.cfg) = False
OS / ENVIRONMENT

Targeting vSphere 6.5.0.38000

STEPS TO REPRODUCE
- name: Set VM tags
  delegate_to: localhost
  community.vmware.vmware_tag_manager:
    hostname: "{{ vsphere_server }}"
    username: "{{ vsphere_user }}"
    password: "{{ vsphere_password }}"

    object_type: VirtualMachine
    moid: "{{ vm_info.instance.moid }}"
    tag_names: "{{ autovm_tags }}"
    state: set
EXPECTED RESULTS

The task should be changed only if the set of tags was modified by the plugin.

ACTUAL RESULTS

The task is always changed if it contains at least one tag.

Additional note: on this line, changed is set to True unconditionally when in set mode.
https://github.com/ansible-collections/community.vmware/blob/main/plugins/modules/vmware_tag_manager.py#L349

@mariolenz
Copy link
Collaborator

Yes, it looks like the module always removes all tags and then adds them again one by one when specifying set:

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))

softwarefactory-project-zuul bot pushed a commit that referenced this issue Sep 22, 2022
vmware_tag_manager: Fix idempotency for state set

SUMMARY
vmware_tag_manager isn't idempotent when using state: set.
Fixes #1265
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
vmware_tag_manager
ADDITIONAL INFORMATION

  
    
      community.vmware/plugins/modules/vmware_tag_manager.py
    
    
        Lines 339 to 349
      in
      fac9919
    
  
  
    

        
          
           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))
mariolenz added a commit to mariolenz/community.vmware that referenced this issue Sep 22, 2022
…s#1465)

vmware_tag_manager: Fix idempotency for state set

SUMMARY
vmware_tag_manager isn't idempotent when using state: set.
Fixes ansible-collections#1265
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
vmware_tag_manager
ADDITIONAL INFORMATION

      community.vmware/plugins/modules/vmware_tag_manager.py

        Lines 339 to 349
      in
      fac9919

           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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants