tman
tman

Reputation: 425

Ansible retrieve multiple array values from module output

Consider the following returned data from an ansible module, I register the results in a variable called kibana_lc_all.

I'd like to be able to iterate over all of the name values, but I'm not sure how to do so with Ansible... I know I can print the first value via:

- debug:
    msg: "LC info is: {{ kibana_lc_all.results[0].name }}"

But how could I iterate and either print all 3 names, or store the 3 names in an array variable and iterate over them later in another task? Also, there won't always be 3 names, could be anywhere from 1 to 20...

  {
   u'results':[  
   {  
      u'ram_disk_id':u'',
      u'name':u'pro-ELK-Kibana-20170628-1152',
      u'image_id':u'ami-1a96a60c'
   },
   {
      u'ram_disk_id':u'',
      u'name':u'pro-ELK-Kibana-20170625-1050',
      u'image_id':u'ami-1b97d64f'
   }, 
   {
      u'ram_disk_id':u'',
      u'name':u'pro-ELK-Kibana-20170621-0931',
      u'image_id':u'ami-1b97d64f'
   },
  ]
 }

Upvotes: 2

Views: 3665

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68339

Use map filter:

- set_fact:
    my_list: "{{ kibana_lc_all.results | map(attribute='name') | list }}"

or json_query filter:

- set_fact:
    my_list: "{{ kibana_lc_all | json_query('results[].name') }}"

Upvotes: 3

Related Questions