HewlettPackard / oneview-ansible

This project is no longer being developed and has limited support. Please use the newer Ansible Collection project: https://github.com/HewlettPackard/oneview-ansible-collection
Apache License 2.0
104 stars 65 forks source link

Rack rename do not work #597

Closed Mounaam closed 3 years ago

Mounaam commented 3 years ago

Scenario/Intent

When trying to rename a rack, ansible task remain unchanged and nothing is done

Environment Details

"git clone" then "docker container build" on November 19th

Steps to Reproduce

rename.yml

- hosts: all
  vars:
    config: "{{ playbook_dir }}/oneview_config.json"
    rack_name: "LL0-N-08X"

  tasks:
    - name: Rename the default rack
      oneview_rack:
        config: "{{ config }}"
        state: present
        data:
          name: '2AB100LMNB'
          newName: "{{ rack_name }}"

    - debug: var=rack['name']

# ansible-playbook rename.yml

Expected Result

Rename of the rack to newName when it exists with (old) name

TASK [Rename the default rack]
changed: [localhost]

TASK [debug]
ok: [localhost] => {
    "rack['name']": "LL0-N-08"
}

Actual Result

Nothing done

TASK [Rename the default rack]
ok: [localhost]

TASK [debug]
ok: [localhost] => {
    "rack['name']": "2AB100LMNB"
}

My comments

library/module_utils/oneview.py:dict_merge(dict1, dict2) do not work as expected It makes a deep merge of 2 dictionaries and returns the merged dictionary. Values in dict2 will overwrite same key values of dict1.

  1. merged_data = dict_merge(self.data, self.current_resource) Common keys in current_resource will overwrite what we want to change in data. We have to permute.

2.dict_merge() modifies dict1 passed to it. After permutation because of point 1 above, original current_resource is now modified. When _update() compare current and merged data, it doesn't detect any change. We have to work on a copy of dict1 before modify it to keep original unchanged.

  1. rename task is not idempotent. When no rack with (old) name exists but there is a rack with newName, it will create a new rack with (old) name. We have to check first if rack with newName exists.
nabhajit-ray commented 3 years ago

Thanks for the sharing the fix. We will review it and the update the pull request.