小编nae*_*rak的帖子

Ansible循环来自URI Call的JSON输出

我一直试图让它工作一段时间,但我无法这样做.我希望这是我失踪的小事.

我试图解析使用with_items的任务的JSON输出.据我所知,最终组件的变量列表将包含在结果数组中.

   - name: Get list of components for each host
      uri: url="http://192.168.0.11:8080/api/v1/clusters/pstl-cluster/hosts/{{ hostvars[item].ansible_host_fqdn }}"
         method=GET
         force_basic_auth=yes
         user=admin
         password=admin
         HEADER_X-Requested-By="ambari"
         status_code=200,201,202
         return_content=yes
      register: list_of_components
      with_items: "{{ groups['hadoop_cluster'] }}"

#   - debug: msg="Components are {{ (list_of_components.results|from_json)|json_query('content.host_components[*].HostRoles.component_name') }}"
    #- debug: var=list_of_components
#   - debug: msg="Components are {{ list_of_components.results[0].item.content.host_components[*].HostRoles.component_name }}"
    - debug: msg="Components are {{ item }}"
      with_items: "{{ list_of_components.results|from_json }}"
Run Code Online (Sandbox Code Playgroud)

"获取每个主机的组件列表"任务的示例调试输出发布在下面.我试图得到一个元组(host,component_name),我可以在下一个任务中循环

我提到过,我对Ansible的理解相当有限.

ok: [localhost] => (item=slave2) => {
    "changed": false,
    "connection": "close",
    "content": "{\n  \"href\" : \"http://192.168.0.11:8080/api/v1/clusters/pstl-cluster/hosts/slave2.mycluster\",\n  \"Hosts\" : {\n    \"cluster_name\" : …
Run Code Online (Sandbox Code Playgroud)

json ansible

5
推荐指数
1
解决办法
9339
查看次数

标签 统计

ansible ×1

json ×1