ansible parse json with several keys with the same name to one list variable ansible parse json with several keys with the same name to one list variable json json

ansible parse json with several keys with the same name to one list variable


This is what filters (and this) for:

- set_fact:    hosts_all: "{{ hosts_json.json.data | map(attribute='hostname') | list }}"    hosts_i: "{{ hosts_json.json.data | map(attribute='hostname') | map('regex_search','.*-i-.*') | select('string') | list }}"

host_all will contain all hostnames, host_i will contain only .*-i-.* matching hostnames.


Try this

- uri:    url: http://rancher.local:8080/v1/hosts    method: GET    user: ##################    password: ################    body_format: json  register: hosts_json- name: init fact  set_fact:    rancher_env_hosts: "[]"- name: test  set_fact:    rancher_env_hosts: "{{rancher_env_hosts}} + [ {{item.hostname}} ]"  when: item.hostname | search(".*-i-.*")  with_items: "{{hosts_json.json.data}}"- name: output  debug:    msg: "hosts: {{rancher_env_hosts}}"

About search you can read here http://docs.ansible.com/ansible/playbooks_tests.html

UPD:
About adding values to array here: Is it possible to set a fact of an array in Ansible?