首页 > 解决方案 > 每个循环变量的ansible显示输出

问题描述

团队,我的以下任务在组中的所有节点上运行,我需要显示循环中使用的每个节点的输出。任何提示?

        command: cat "{{ proc_stats }}"
        register: stats
        delegate_to: "{{ item }}"
        with_items: "{{ groups['kube-gpu-node'] }}"
        failed_when: stats.stdout is not search('FS-Cache')
      - debug:
          var: stats.results[item.item].stdout_lines
        with_items: "{{ groups['kube-gpu-node'] }}"

输出:

TASK [team-services-pre-install-checks : Assure fscache proc stats] *****************************************************************************************************
Thursday 05 December 2019  01:40:18 +0000 (0:00:00.172)       0:00:09.990 *****
changed: [localhost -> test.dumy.net] => (item=dgx0159)

TASK [maglev-services-pre-install-checks : debug] *************************************************************************************************************************
Thursday 05 December 2019  01:40:18 +0000 (0:00:00.174)       0:00:10.165 *****
ok: [localhost] => (item=dgx0159) => {
    "ansible_loop_var": "item",
    "item": "dgx0159",
    "stats.results[item.item].stdout_lines": "VARIABLE IS NOT DEFINED!"
}

当我只使用结果中的索引为一个节点进行调试时,我得到了这个,但我想要所有节点,而不仅仅是一个如何?

      - debug:
          var: stats.results[0].stdout_lines

TASK [team-services-pre-install-checks : debug] *************************************************************************************************************************
Thursday 05 December 2019  01:44:58 +0000 (0:00:00.181)       0:00:11.573 *****
ok: [localhost] => (item=node1) => {
    "ansible_loop_var": "item",
    "item": "dgx0159",
    "stats.results[0].stdout_lines": [
        "FS-Cache statistics(ver:1.0)",
        "Cookies: idx=55 dat=42 spc=0",
        "Objects: alc=85 nal=0 avl=85 ded=86",
        "ChkAux : non=0 ok=33 upd=0 obs=0",
        "Pages  : mrk=52929 unc=52929",
        "Acquire: n=97 nul=0 noc=0 ok=97 nbf=0 oom=0",
        "Lookups: n=85 neg=19 pos=66 crt=19 tmo=0",
        "Invals : n=0 run=0",
        "Updates: n=0 nul=0 run=0",
        "Relinqs: n=97 nul=0 wcr=0 rtr=0",
        "AttrChg: n=0 ok=0 nbf=0 oom=0 run=0",
        "Allocs : n=0 ok=0 wt=0 nbf=0 int=0",
        "Allocs : ops=0 owt=0 abt=0",
        "Retrvls: n=312 ok=226 wt=20 nod=86 nbf=0 int=0 oom=0",
        "Retrvls: ops=312 owt=8 abt=0",
        "Stores : n=13229 ok=13229 agn=0 nbf=0 oom=0 wrxd=0 sol=0",
        "Stores : ops=82 run=13311 pgs=13229 rxd=13229 irxd=0 olm=0 ipp=0",
        "VmScan : nos=0 gon=0 bsy=0 can=0 wt=0",
        "Ops    : pend=8 run=394 enq=13311 can=0 rej=0",
        "Ops    : ini=13541 dfr=1 rel=13541 gc=1",
        "CacheOp: alo=0 luo=0 luc=0 gro=0",
        "CacheOp: inv=0 upo=0 dro=0 pto=0 atc=0 syn=0",
        "CacheOp: rap=0 ras=0 alp=0 als=0 wrp=0 ucp=0 dsp=0",
        "CacheEv: nsp=0 stl=0 rtr=0 cul=0"
    ]
}

标签: ansibleansible-2.xansible-inventoryansible-facts

解决方案


您已经有一个列表status.results,因此您可以对其进行迭代:

      - debug:
          var: item.stdout_lines
        with_items: stats.results

或者从第一个任务中查看项目:

      - debug:
          msg: "result for {{ item.item }} is {{ item.stdout_lines }}"
        with_items: stats.results

推荐阅读