7

I have an issue to parse a json using ansible I have a task that connected to rancher and get a json file

task:

- uri:
    url: http://rancher.local:8080/v1/hosts
    method: GET
    user: ##################
    password: ################
    body_format: json
  register: hosts_json

- name: test
  set_fact:
    rancher_env_hosts: "{{ item.hostname }}"
  #when: item.hostname == "*-i-*"
  with_items: "{{hosts_json.json.data}}"

- name: output
  debug:
    msg: "hosts: {{rancher_env_hosts}}"

and I get the following json (after edit it to be more readable):

{
    "json": {
        "data": [
            {
                "hostname": "rancher-i-host-02",
                "id": "adsfsa"
            },
            {
                "hostname": "rancher-i-host-01",
                "id": "gfdgfdg"
            },
            {
                "hostname": "rancher-q-host-01",
                "id": "dfgdg"
            },
            {
                "hostname": "rancher-q-host-02",
                "id": "dfgdg"
            }
        ]

    }

}

When I start the playbook I get only the last host name in the variable and not all the list of hostname. can I register all the list to the same variable?

In addition, I also added a line with the a comment "#" in order to get only the host names that match the string "-i-" bit it's not working. any idea?

2
  • Is rancher_env_hosts need to be string with comma/space separated values? Or it's need to be array? Commented Nov 9, 2016 at 14:06
  • Hi @Terra the next thing will be to run task for each hostname woth the ansible so probably array is preferred but maybe I can handle both of them. Commented Nov 9, 2016 at 14:16

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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 a list in Ansible?

2 Comments

Did give it a thumbs up for the overall solution, but combining the list doesn't seem to give good results. I get something like hosts: [] + [ rancher-i-host-02 ] in the debug msg output. So it's really just appending strings
To solution to above comment is to initiate the list with [] (so no "[]") and then when appending: rancher_env_hosts: "{{ rancher_env_hosts + [ item.hostname ] }}" Not sure if that is valid in all versions of ansible..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.