|
| 1 | +--- |
| 2 | +- name: List modules |
| 3 | + ansible.builtin.command: > |
| 4 | + {{ openstack }} rating module list -f json |
| 5 | + register: modules |
| 6 | + changed_when: false |
| 7 | + |
| 8 | +- name: Enable hashmap module |
| 9 | + ansible.builtin.command: > |
| 10 | + {{ openstack }} rating module enable hashmap |
| 11 | + when: not (modules.stdout | from_json | selectattr('Module', 'equalto', 'hashmap') | first)['Enabled'] | bool |
| 12 | + changed_when: true |
| 13 | + |
| 14 | +- name: List hashmap services |
| 15 | + ansible.builtin.command: > |
| 16 | + {{ openstack }} rating hashmap service list -f json |
| 17 | + register: hashmap_services |
| 18 | + changed_when: false |
| 19 | + |
| 20 | +- name: Create hashmap services |
| 21 | + vars: |
| 22 | + existing_services: "{{ hashmap_services.stdout | from_json | map(attribute='Name') | list }}" |
| 23 | + ansible.builtin.command: > |
| 24 | + {{ openstack }} rating hashmap service create {{ item }} |
| 25 | + loop: "{{ (os_ratings_hashmap_field_mapping_services + os_ratings_hashmap_service_mapping_services) | unique | list }}" |
| 26 | + when: item not in existing_services |
| 27 | + changed_when: true |
| 28 | + |
| 29 | +- name: List hashmap groups |
| 30 | + ansible.builtin.command: > |
| 31 | + {{ openstack }} rating hashmap group list -f json |
| 32 | + register: hashmap_groups |
| 33 | + changed_when: false |
| 34 | + |
| 35 | +- name: Create hashmap groups |
| 36 | + vars: |
| 37 | + existing_groups: "{{ hashmap_groups.stdout | from_json | map(attribute='Name') | list }}" |
| 38 | + field_mapping_groups: "{{ query('subelements', os_ratings_hashmap_field_mappings, 'mappings') | map(attribute='1.group') | select('defined') | list }}" |
| 39 | + service_mapping_groups: "{{ os_ratings_hashmap_service_mappings | map(attribute='group') | select('defined') | list }}" |
| 40 | + ansible.builtin.command: > |
| 41 | + {{ openstack }} rating hashmap group create {{ item }} |
| 42 | + loop: "{{ (field_mapping_groups + service_mapping_groups) | unique | list }}" |
| 43 | + when: |
| 44 | + - item is not none and item | length > 0 |
| 45 | + - item not in existing_groups |
| 46 | + changed_when: true |
| 47 | + |
| 48 | +# List again to get IDs of created services. |
| 49 | +- name: List hashmap services |
| 50 | + ansible.builtin.command: > |
| 51 | + {{ openstack }} rating hashmap service list -f json |
| 52 | + register: hashmap_services |
| 53 | + changed_when: false |
| 54 | + |
| 55 | +# List again to get IDs of created groups. |
| 56 | +- name: List hashmap groups |
| 57 | + ansible.builtin.command: > |
| 58 | + {{ openstack }} rating hashmap group list -f json |
| 59 | + register: hashmap_groups |
| 60 | + changed_when: false |
| 61 | + |
| 62 | +- name: List hashmap fields |
| 63 | + vars: |
| 64 | + service_id: "{{ (hashmap_services.stdout | from_json | selectattr('Name', 'equalto', item) | first)['Service ID'] }}" |
| 65 | + ansible.builtin.command: > |
| 66 | + {{ openstack }} rating hashmap field list {{ service_id }} -f json |
| 67 | + loop: "{{ os_ratings_hashmap_field_mapping_services }}" |
| 68 | + register: hashmap_fields |
| 69 | + changed_when: false |
| 70 | + |
| 71 | +# Field mappings |
| 72 | + |
| 73 | +- name: Include field mappings |
| 74 | + ansible.builtin.include_tasks: field-mappings.yml |
| 75 | + vars: |
| 76 | + fields_result: "{{ hashmap_fields.results | selectattr('item', 'equalto', field.service) | first }}" |
| 77 | + fields: "{{ fields_result.stdout | from_json }}" |
| 78 | + service_id: "{{ (hashmap_services.stdout | from_json | selectattr('Name', 'equalto', field.service) | first)['Service ID'] }}" |
| 79 | + loop: "{{ os_ratings_hashmap_field_mappings }}" |
| 80 | + loop_control: |
| 81 | + loop_var: field |
| 82 | + |
| 83 | +# Service mappings |
| 84 | + |
| 85 | +- name: List hashmap service mappings |
| 86 | + vars: |
| 87 | + service_id: "{{ (hashmap_services.stdout | from_json | selectattr('Name', 'equalto', item) | first)['Service ID'] }}" |
| 88 | + ansible.builtin.command: > |
| 89 | + {{ openstack }} rating hashmap mapping list -f json --service-id {{ service_id }} |
| 90 | + loop: "{{ os_ratings_hashmap_service_mapping_services }}" |
| 91 | + register: hashmap_mappings |
| 92 | + changed_when: false |
| 93 | + |
| 94 | +- name: Create hashmap service mappings |
| 95 | + vars: |
| 96 | + mappings_result: "{{ hashmap_mappings.results | selectattr('item', 'equalto', item.service) | first }}" |
| 97 | + mappings: "{{ mappings_result.stdout | from_json }}" |
| 98 | + service_id: "{{ (hashmap_services.stdout | from_json | selectattr('Name', 'equalto', item.service) | first)['Service ID'] }}" |
| 99 | + group_id: "{{ (hashmap_groups.stdout | from_json | selectattr('Name', 'equalto', item.group) | first)['Group ID'] | default('') if item.group is defined else |
| 100 | + '' }}" |
| 101 | + ansible.builtin.command: > |
| 102 | + {{ openstack }} rating hashmap mapping create |
| 103 | + {{ item.cost }} |
| 104 | + --service-id {{ service_id }} |
| 105 | + {% if group_id | length > 0 %}--group-id {{ group_id }}{% endif %} |
| 106 | + --type {{ item.type }} |
| 107 | + loop: "{{ os_ratings_hashmap_service_mappings }}" |
| 108 | + # Condition could be better, but should work with current values. |
| 109 | + when: mappings | length == 0 |
| 110 | + changed_when: true |
0 commit comments