-1

Team, I have a task where am trying to reference value from a dictionary defined in my values file. but getting below error, any hint? am doing this exercise to use this reference in my other tasks later but first debug step itself is failing.

I referred this link [ansible dict reference][1]

values.yaml

loop_device: "loop"
available_device_default_config:
  - device: any
    partitions:
      - number: 1
        start: 0%
        end: 100%
        storage_class: services

task

- name: lookup dictionary vars
    debug:
      var: available_device_default_config.device

task output error

34  TASK [local_volume_mount : lookup dictionary vars] *****************************
  Wednesday 28 April 2021  21:51:34 +0000 (0:00:04.915)       0:00:05.052 ******* 
  ok: [node1] => {
      "available_device_default_config.device": "VARIABLE IS NOT DEFINED!"
  }

expected output:

ok: [node1] => {
      "any"
  }

I tried below but no luck

var: "{{ available_device_default_config.device }}"

var: "{{ available_device_default_config['device'] }}"

AhmFM
  • 1,552
  • 3
  • 23
  • 53
  • I added expected output that i was able to acheive with static values baked into task directly. – AhmFM Apr 28 '21 at 21:18

1 Answers1

1

The variable available_device_default_config is defined as a list of dicts. So you cannot access the property device directly, because it is a property of the first item in available_device_default_config. You need to look into it via available_device_default_config[0]

- name: "set facts"
  set_fact:
    available_device_default_config:
      - device: any
        partitions:
          - number: 1
            start: 0%
            end: 100%
            storage_class: services

- name: "lookup dictionary vars"
  debug:
    msg: "{{ available_device_default_config }}"

- name: "lookup dictionary vars"
  debug:
    msg: "{{ available_device_default_config[0].device }}"

The result would be

TASK [set facts] ****************************************************************
ok: [localhost]

TASK [lookup dictionary vars] ***************************************************
ok: [localhost] => 
  msg:
  - device: any
    partitions:
    - end: 100%
      number: 1
      start: 0%
      storage_class: services

TASK [lookup dictionary vars] ***************************************************
ok: [localhost] => 
  msg: any
TRW
  • 876
  • 7
  • 23
  • can you please also hint how could just search with name instead of index 0? like for device value what is subkey value? – AhmFM May 07 '21 at 19:09