Need to remove '-' from value data in dictionary from API output using Ansible

43 Views Asked by At
    block:
    - name: Retrieve Source Volumes List
      uri:
        url: "https://{{ dms_ip }}/api/source/volumes"
        url_username: "{{ credential_dms.username }}"
        url_password: "{{ credential_dms.password }}"
        method: GET
        force_basic_auth: true
        validate_certs: false
      register: source_volume_data

    - name: register for hostnames full details
      set_fact:
        hostnamea: "{{ source_volume_data.json.volumes | json_query('[].{hostName: hostName, uuid: uuid, deviceName: deviceName, capacity: capacity}') }}"

I need to replace - in uuid, tried different ways nothing worked. Below is the API output.

{
    "capacity": "34361180160",
    "destinationInUse": false,
    "destinationPurpose": "PURPOSE_UNKNOWN",
    "deviceDisplayName": "Dev-S-02210",
    "deviceName": "Dev-S-02210",
    "firstPathLun": 41,
    "hostId": 7,
    "hostName": "Host005",
    "inquiry": "EMC_____SYMMETRIX_______5977",
    "pathCount": 2,
    "paths": [],
    "product": "SYMMETRIX       ",
    "rev": "5977",
    "sectorSize": 512,
    "sharedDevice": false,
    "status": "ONLINE",
    "storageArrayId": 11,
    "storageArrayName": "EMC-11",
    "type": "NEXUS",
    "uuid": "60000970-0002-9670-0118-533032343211",
    "vendor": "EMC     "
},
{
    "capacity": "34361180160",
    "destinationInUse": false,
    "destinationPurpose": "PURPOSE_UNKNOWN",
    "deviceDisplayName": "Dev-S-02211",
    "deviceName": "Dev-S-02211",
    "firstPathLun": 42,
    "hostId": 7,
    "hostName": "Host005",
    "inquiry": "EMC_____SYMMETRIX_______5977",
    "pathCount": 2,
    "paths": [],
    "product": "SYMMETRIX       ",
    "rev": "5977",
    "sectorSize": 512,
    "sharedDevice": false,
    "status": "ONLINE",
    "storageArrayId": 11,
    "storageArrayName": "EMC-11",
    "type": "NEXUS",
    "uuid": "60000970-0002-9670-0118-533032343711",
    "vendor": "EMC     "
}
1

There are 1 best solutions below

1
U880D On

A minimal example playbook

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    volumes:
    - {
    "capacity": "34361180160",
    "destinationInUse": false,
    "destinationPurpose": "PURPOSE_UNKNOWN",
    "deviceDisplayName": "Dev-S-02210",
    "deviceName": "Dev-S-02210",
    "firstPathLun": 41,
    "hostId": 7,
    "hostName": "Host005",
    "inquiry": "EMC_____SYMMETRIX_______5977",
    "pathCount": 2,
    "paths": [],
    "product": "SYMMETRIX       ",
    "rev": "5977",
    "sectorSize": 512,
    "sharedDevice": false,
    "status": "ONLINE",
    "storageArrayId": 11,
    "storageArrayName": "EMC-11",
    "type": "NEXUS",
    "uuid": "60000970-0002-9670-0118-533032343211",
    "vendor": "EMC     "
    }
    - {
    "capacity": "34361180160",
    "destinationInUse": false,
    "destinationPurpose": "PURPOSE_UNKNOWN",
    "deviceDisplayName": "Dev-S-02211",
    "deviceName": "Dev-S-02211",
    "firstPathLun": 42,
    "hostId": 7,
    "hostName": "Host005",
    "inquiry": "EMC_____SYMMETRIX_______5977",
    "pathCount": 2,
    "paths": [],
    "product": "SYMMETRIX       ",
    "rev": "5977",
    "sectorSize": 512,
    "sharedDevice": false,
    "status": "ONLINE",
    "storageArrayId": 11,
    "storageArrayName": "EMC-11",
    "type": "NEXUS",
    "uuid": "60000970-0002-9670-0118-533032343711",
    "vendor": "EMC     "
    }

  tasks:

  - name: Get list of UUIDs
    debug:
      msg: "{{ volumes | map(attribute='uuid') }}"


  - name: Create hostname from volume UUID
    debug:
      msg: "{{ volumes | map(attribute='uuid') | list | map('regex_replace', '-', '') }}"

will result into an output of

TASK [Get list of UUIDs] *********************
ok: [localhost] =>
  msg:
  - 60000970-0002-9670-0118-533032343211
  - 60000970-0002-9670-0118-533032343711

TASK [Create hostname from volume UUID] ******
ok: [localhost] =>
  msg:
  - '60000970000296700118533032343211'
  - '60000970000296700118533032343711'

Further Documentation