vmware.vmware_rest icon indicating copy to clipboard operation
vmware.vmware_rest copied to clipboard

document the fact vcenter_resourcepool is not idempotent

Open goneri opened this issue 3 years ago • 1 comments

Copy past of the message from @matthewcosgrove https://github.com/ansible-collections/vmware.vmware_rest/issues/153#issuecomment-713461758

✂️✂️✂️✂️✂️✂️

Dropping my working example here. I am using the cluster details response "my_cluster_details" which contains the moid of its default resource pool which has to be used as the parent

- name: Create resource pool
  hosts: localhost
  vars:
    existing_cluster: "{{ lookup('env', 'GOVC_CLUSTER') }}"

  collections:
  - vmware.vmware_rest
  tasks:
  - name: "get parent cluster for resource pool"
    vcenter_cluster_info:
      filter_names:
      - "{{ existing_cluster }}"
    register: my_cluster
  - name: "get parent cluster details"
    vcenter_cluster_info:
      cluster: '{{ my_cluster.value[0].cluster }}'
    register: my_cluster_details
  - debug: msg="{{ my_cluster_details }}"
  - name: "ensure resource pool created"
    vcenter_resourcepool:
      name: "new-resource-pool"
      parent: "{{ my_cluster_details.value.resource_pool }}"
    register: my_resource_pool
  - debug: msg="{{ my_resource_pool }}"

Unfortunately, I am responsible for ensuring that its idempotent.

First run: works fine Second run: fatal: [localhost]: FAILED! => {"_debug_info": {"operation": "create", "status": 400}, "changed": false, "type": "com.vmware.vapi.std.errors.invalid_argument", "value": {"error_type": "INVALID_ARGUMENT", "messages": []}}

Not an overly helpful error message there.

Would have hoped state: present results in automatic skipping logic implemented in the module. Maybe a downside of auto-generation from the API I suppose.

So finally, to ensure idempotency I have this solution:

- name: Create resource pool
  hosts: localhost
  vars:
    existing_cluster: "{{ lookup('env', 'GOVC_CLUSTER') }}"
    new_resource_pool_name: "new-resource-pool"

  collections:
  - vmware.vmware_rest
  tasks:
  - name: "get parent cluster for resource pool"
    vcenter_cluster_info:
      filter_names:
      - "{{ existing_cluster }}"
    register: my_cluster
  - name: "get parent cluster details"
    vcenter_cluster_info:
      cluster: '{{ my_cluster.value[0].cluster }}'
    register: my_cluster_details
  - debug: msg="{{ my_cluster_details }}"
  - name: "check resource pool exists"
    vcenter_resourcepool_info:
      filter_names: "{{ new_resource_pool_name  }}"
      filter_clusters: "{{ my_cluster_details.id }}"
    register: rp_already_exists
  - debug: msg="{{ rp_already_exists }}"
  - name: "ensure resource pool created"
    vcenter_resourcepool:
      name: "{{ new_resource_pool_name  }}"
      parent: "{{ my_cluster_details.value.resource_pool }}"
    register: my_resource_pool
    when: not rp_already_exists.value
  - debug: msg="{{ my_resource_pool }}"

NOTE: the importance of filtering by cluster in "check resource pool exists" because my resource pool name might not be unique within the vcenter instance.

If you have any more elegant approaches, I would like to see them.

And thanks again for pointing me in the right direction.

goneri avatar Oct 21 '20 16:10 goneri

We rely on VMware REST API and in this case it doesn't return any meaningful error. After a discussion with the team, we've decided we will improve the documentation to explain the situation.

goneri avatar Dec 09 '20 14:12 goneri