vmware / ansible-for-nsxt

Ansible delivers simple IT automation that ends repetitive tasks and frees up DevOps teams for more strategic work. This project is to enable this automation for NSX-T installation.
Other
171 stars 145 forks source link

nsxt_vm_tags incorrect behavior when updating multiple tags with same scope. #445

Closed doskelion closed 1 year ago

doskelion commented 2 years ago

Describe the bug

having this 10 pair of scope/tag:

  - scope: "Application"
    tag: "TestTag1"
  - scope: "Application"
    tag: "TestTag2"
  - scope: "Application"
    tag: "TestTag3"
  - scope: "ApplicationType"
    tag: "Database"
  - scope: "Backup"
    tag: "Bkp-None"
  - scope: "Class"
    tag: "Pub"
  - scope: "Domain"
    tag: "domain.com"
  - scope: "OS"
    tag: "Windows"
  - scope: "Secure"
    tag: "True"
  - scope: "Tenant"
    tag: "Customer1"

Noticed behavior first run updates the 10 tags but when doing next run it updates still but it changes to 9 tags in nsx-t.

Output shown below keeps doing "changed"

PLAY [VM Tags] *******************************************************************************************************************************************************************************************************

TASK [debug : Update NSX-T VM tags] **********************************************************************************************************************************************************************************
Tuesday 01 November 2022  14:34:44 -0400 (0:00:00.044)       0:00:00.044 ******
changed: [NSXTestVM3]

PLAY RECAP ***********************************************************************************************************************************************************************************************************
NSXTestVM3                 : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Tuesday 01 November 2022  14:34:50 -0400 (0:00:06.268)       0:00:06.313 ******
===============================================================================

Reproduction steps

  1. Use this task:
  1. Run the playbook twice or more and keeps stating changed.

Expected behavior

When nsx-t tags are not synced based on add_tags, first run should do the update:

PLAY [VM Tags] *******************************************************************************************************************************************************************************************************

TASK [debug : Update NSX-T VM tags] **********************************************************************************************************************************************************************************
Tuesday 01 November 2022  14:38:10 -0400 (0:00:00.048)       0:00:00.048 ******
changed: [NSXTestVM3]

PLAY RECAP ***********************************************************************************************************************************************************************************************************
NSXTestVM3                 : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Running the second time should say OK:

PLAY [VM Tags] *******************************************************************************************************************************************************************************************************

TASK [debug : Update NSX-T VM tags] **********************************************************************************************************************************************************************************
Tuesday 01 November 2022  14:38:23 -0400 (0:00:00.054)       0:00:00.054 ******
ok: [NSXTestVM3]

PLAY RECAP ***********************************************************************************************************************************************************************************************************
NSXTestVM3                 : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Additional context

Please review my work around on line 271:

diff -u nsxt_vm_tags.py nsxt_vm_tags-fixed.py
--- nsxt_vm_tags.py     2022-11-01 14:42:53.339695831 -0400
+++ nsxt_vm_tags-fixed.py       2022-11-01 13:41:05.403809620 -0400
@@ -268,7 +268,7 @@
             if TagElement(tag) in final_tags_set:
                 for final_tag in final_tags:
                     if final_tag['scope'] == tag['scope']:
-                        final_tag['tag'] = tag['tag']
+                        final_tag['tag'] == tag['tag']
                         break
             else:
                 final_tags += tag,

See if that makes sense. Thanks.

doskelion commented 2 years ago

to add some info on ansible --version command:

ansible [core 2.13.1]
python version = 3.8.12 (default, May 10 2022, 23:46:40) [GCC 8.5.0 20210514 (Red Hat 8.5.0-10)]
jinja version = 3.1.2
libyaml = True