iT邦幫忙

0

Ansible 中,如何对 register 变量中的 stdout_lines 内容进行赛选

  • 分享至 

  • xImage

假设,我通过一个 playboy 去获取服务器的 CPU 信息,如:

it@workstation:~/ansible$ vim test.yml 
it@workstation:~/ansible$ cat test.yml
---
- name: Check hardware of the computer
  hosts: servera
  tasks:
    - name: View CPU informations
      shell: lscpu
      
      register: cpu
    - debug:
        var: cpu.stdout_lines

这是运行的结果:

it@workstation:~/ansible$ ansible-playbook test.yml 
BECOME password: 

PLAY [Check hardware of the computer] **************************************************************

TASK [Gathering Facts] *****************************************************************************
ok: [servera]

TASK [View CPU informations] ***********************************************************************
changed: [servera]

TASK [debug] ***************************************************************************************
ok: [servera] => {
    "cpu.stdout_lines": [
        "Architecture:                    x86_64",
        "CPU op-mode(s):                  32-bit, 64-bit",
        "Byte Order:                      Little Endian",
        "Address sizes:                   43 bits physical, 48 bits virtual",
        "CPU(s):                          1",
        "On-line CPU(s) list:             0",
        "Thread(s) per core:              1",
        "Core(s) per socket:              1",
        "Socket(s):                       1",
        "NUMA node(s):                    1",
        "Vendor ID:                       GenuineIntel",
        "CPU family:                      6",
        "Model:                           94",
        "Model name:                      Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz",
        "Stepping:                        3",
        "CPU MHz:                         3408.000",
        "BogoMIPS:                        6816.00",
        "Hypervisor vendor:               VMware",
        "Virtualization type:             full",
        "L1d cache:                       32 KiB",
        "L1i cache:                       32 KiB",
        "L2 cache:                        256 KiB",
        "L3 cache:                        8 MiB",
        "NUMA node0 CPU(s):               0",
        "Vulnerability Itlb multihit:     KVM: Vulnerable",
        "Vulnerability L1tf:              Mitigation; PTE Inversion",
        "Vulnerability Mds:               Vulnerable: Clear CPU buffers attempted, no microcode; SMT Host state unknown",
        "Vulnerability Meltdown:          Mitigation; PTI",
        "Vulnerability Spec store bypass: Vulnerable",
        "Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization",
        "Vulnerability Spectre v2:        Mitigation; Full generic retpoline, IBPB conditional, IBRS_FW, STIBP disabled, RSB filling",
        "Vulnerability Srbds:             Unknown: Dependent on hypervisor status",
        "Vulnerability Tsx async abort:   Vulnerable: Clear CPU buffers attempted, no microcode; SMT Host state unknown",
        "Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc cpuid pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single pti ibrs ibpb stibp fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 invpcid rtm mpx rdseed adx smap clflushopt xsaveopt xsavec xsaves arat arch_capabilities"
    ]
}

PLAY RECAP *****************************************************************************************
servera                    : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

但 stdout_lines 的内容还是太多,我只想获取其中某个值,如 “Model name” 的值,我应该怎么修改 playboy?

elleryq iT邦新手 4 級 ‧ 2021-01-20 22:15:47 檢舉
改到回答那邊
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
elleryq
iT邦新手 4 級 ‧ 2021-01-20 22:21:38
最佳解答

可以用 grep 或 awk

- shell: lscpu | grep "Model name"
   register: cpu
- debug:
    var: cpu.stdout

我要發表回答

立即登入回答