google.cloud icon indicating copy to clipboard operation
google.cloud copied to clipboard

gcp_compute_disk.py on disk is allways changed. Even if no change occured. (Including Patch for the Bug)

Open Tux12Fun opened this issue 1 year ago • 0 comments

SUMMARY

If i create a disk with the module gcp_compute_disk.py first time the task is changed. This is okay. But if i rerun the Playbook without any change the task is every time in state changed. Not in state OK as expected.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

gcp_compute_disk.py

ANSIBLE VERSION
ansible [core 2.16.3]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/etc/ansible/library']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /etc/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True

COLLECTION VERSION
Latest 
google.cloud                             1.3.0
shiped with Ansible on Ubuntu 24.04
CONFIGURATION
ALLBACKS_ENABLED(/etc/ansible/ansible.cfg) = ['ansible.posix.profile_tasks']
COLLECTIONS_PATHS(/etc/ansible/ansible.cfg) = ['/etc/ansible/collections']
CONFIG_FILE() = /etc/ansible/ansible.cfg
DEFAULT_LOAD_CALLBACK_PLUGINS(/etc/ansible/ansible.cfg) = True
DEFAULT_MODULE_PATH(/etc/ansible/ansible.cfg) = ['/etc/ansible/library']
DEFAULT_STDOUT_CALLBACK(/etc/ansible/ansible.cfg) = yaml
PERSISTENT_COMMAND_TIMEOUT(/etc/ansible/ansible.cfg) = 300
RETRY_FILES_ENABLED(/etc/ansible/ansible.cfg) = False
OS / ENVIRONMENT

OS: Ubuntu 24.04 LTS with GCP

STEPS TO REPRODUCE
    - name:                            Create Disks
      gcp_compute_disk:
         name:                         "{{ item.name }}"
         size_gb:                      "{{ item.size }}"
         source_image:                 "{{ item.source_image|default(omit) }}"
         zone:                         "{{ CE_GCE_ZONE }}"
         project:                      "{{ CE_GCE_PROJECT_ID__DISK }}"
         auth_kind:                    serviceaccount
         service_account_file:         "{{ CE_GCE_CREDENTIAL_FILE }}"
         type:                         "{{ item.type|default('pd-standard') }}"
         scopes:
           - https://www.googleapis.com/auth/compute
         state:                        present
      delegate_to:                     localhost
      with_items:
         - { name: "{{ CE_GCE_INSTANCE_NAME }}", size: "{{ CE_GCE_VM_ROOT_DISK_SIZE }}", source_image: "{{ CE_GCE_VM_SOURCE_IMG }}", auto_delete: true, boot: true }
         - "{{ CE_GCE_VM_DISK|default([]) }}"
      register:                        ce_gcp_instance__disks
EXPECTED RESULTS
  1. Run state changed
  2. Run state ok
ACTUAL RESULTS
At the moment the Task is allways changed.

I did a bit of debugging and found the following Issue. On the diff (Method: def is_different(module, response):) the Resulsts are different. Here is the Output I got on a second round: (I added a print with DIFFFF and the output of the two objects:

  module_stdout: |-
    DIFFFFFFFFFFFFFF:
    {'name': 'test1-mysql', 'sizeGb': 20, 'type': 'https://compute.googleapis.com/compute/v1/projects/yourproject-prod/zones/europe-west3-c/diskTypes/pd-balanced'}
    {'name': 'test1-mysql', 'sizeGb': '20', 'type': 'https://www.googleapis.com/compute/v1/projects/yourproject-prod/zones/europe-west3-c/diskTypes/pd-balanced'}

As you can see the request Variable:

  • request_vals -> contains: https://compute.googleapis.com
  • response_vals -> contains: https://www.googleapis.com as the type of the disk.

So we can either fix this on this compare if we replace the Domain Part or we change the following lines.

def disk_type_selflink(name, params):
    if name is None:
        return
    url = r"https://www.googleapis.com/compute/v1/projects/.*/zones/.*/diskTypes/.*"
    if not re.match(url, name):
        name = "https://www.googleapis.com/compute/v1/projects/{project}/zones/{zone}/diskTypes/%s".format(**params) % name
    return name

This is what I tried and was successfull. Change the creation of the disk_type_selflink method from compute.googleapis.com to www.googleapis.com. --> This worked for me.

Here is a patchfile with my Changes.

--- /gcp_compute_disk.py        2024-06-24 13:26:12.924958064 +0200
+++ /usr/lib/python3/dist-packages/ansible_collections/google/cloud/plugins/modules/gcp_compute_disk.py 2024-06-24 13:46:13.724766189 +0200
@@ -753,9 +753,9 @@
 def disk_type_selflink(name, params):
     if name is None:
         return
-    url = r"https://compute.googleapis.com/compute/v1/projects/.*/zones/.*/diskTypes/.*"
+    url = r"https://www.googleapis.com/compute/v1/projects/.*/zones/.*/diskTypes/.*"
     if not re.match(url, name):
-        name = "https://compute.googleapis.com/compute/v1/projects/{project}/zones/{zone}/diskTypes/%s".format(**params) % name
+        name = "https://www.googleapis.com/compute/v1/projects/{project}/zones/{zone}/diskTypes/%s".format(**params) % name
     return name

This could also occur on other modules. Maybe we need a general solution for this topic.

Tux12Fun avatar Jun 24 '24 12:06 Tux12Fun