我有 1000 台服务器的以下数据(lscpu 输出),我希望在 excel 中以行格式显示,而不是以命令输出的列格式显示
1.2.3.4 CHANGED
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 39 bits physical, 48 bits virtual
CPU(s): 8
On-line CPU(s) list: 0-7
Thread(s) per core: 2
Core(s) per socket: 4
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 140
Model name: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
Stepping: 1
CPU MHz: 2800.000
CPU max MHz: 4700.0000
CPU min MHz: 400.0000
BogoMIPS: 5606.40
Virtualization: VT-x
L1d cache: 192 KiB
L1i cache: 128 KiB
L2 cache: 5 MiB
L3 cache: 12 MiB
5.6.7.8 CHANGED
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 39 bits physical, 48 bits virtual
CPU(s): 8
On-line CPU(s) list: 0-7
Thread(s) per core: 2
Core(s) per socket: 4
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 140
Model name: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
Stepping: 1
CPU MHz: 2800.000
CPU max MHz: 4700.0000
CPU min MHz: 400.0000
BogoMIPS: 5606.40
Virtualization: VT-x
L1d cache: 192 KiB
L1i cache: 128 KiB
L2 cache: 5 MiB
L3 cache: 12 MiB
我想要在 Excel 中为多台服务器提供此信息
IP Architecture CPU op-mode(s) ... ...L3 cache
data
data
data
答案1
您可以打印输出处理器在JSON 格式,然后选择您需要的杰奇:
[user@laptop ~]$ lscpu -J|jq .lscpu[0]
{
"field": "Architecture:",
"data": "x86_64"
}
[user@laptop ~]$
或者采用 CSV 格式:
[user@laptop ~]$ lscpu --parse --output-all
# The following is the parsable format, which can be fed to other
# programs. Each different item in every column has an unique ID
# starting usually from zero.
# BOGOMIPS,CPU,Core,Socket,Cluster,Node,Book,DRAWER,L1d:L1i:L2:L3,Polarization,Address,Configured,Online,Mhz,SCALMHZ%,Maxmhz,Minmhz
5188.21,0,0,0,,0,,,0:0:0:0,U,,,Y,1809.9220,57%,3200.0000,500.0000
5188.21,1,0,0,,0,,,0:0:0:0,U,,,Y,797.6560,25%,3200.0000,500.0000
5188.21,2,1,0,,0,,,1:1:1:0,U,,,Y,880.4450,28%,3200.0000,500.0000
5188.21,3,1,0,,0,,,1:1:1:0,U,,,Y,2095.2959,65%,3200.0000,500.0000
[user@laptop ~]$
另一种方法是使用ansible:
[user@laptop ~]$ ansible -i localhost, -m setup all|sed 's/localhost | SUCCESS => //'|jq .ansible_facts.ansible_processor
[
"0",
"GenuineIntel",
"Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz",
"1",
"GenuineIntel",
"Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz",
"2",
"GenuineIntel",
"Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz",
"3",
"GenuineIntel",
"Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz"
]
[user@laptop ~]$
第三种方法是解析以下信息/proc/cpuinfo
:
[user@laptop ~]$ grep 'model name' /proc/cpuinfo
model name : Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz
model name : Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz
model name : Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz
model name : Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz
[user@laptop ~]$
答案2
使用JSON格式,将数据注册到变量中输出
- command: lscpu -J
register: lscpu_out
提取所有变量输出,获取属性标准输出,将字符串转换为 YAML,并获取属性处理器
lscpu_data: "{{ ansible_play_hosts|
map('extract', hostvars, ['lscpu_out', 'stdout'])|
map('from_yaml')|
map(attribute='lscpu') }}"
获取键(字段)列表
lscpu_keys: "{{ lscpu_data.0|map(attribute='field') }}"
并获取值列表(数据)
lscpu_vals: "{{ lscpu_data|map('map', attribute='data') }}"
将数据写入控制器上的文件。适合内容满足您的需求
- copy:
dest: /tmp/lscpu.csv
content: |
"IP" "{{ lscpu_keys|join('" "') }}"
{% for s,v in ansible_play_hosts|zip(lscpu_vals) %}
"{{ s }}" "{{ v|join('" "') }}"
{% endfor %}
run_once: true
delegate_to: localhost
给出(节选)
shell> cat /tmp/lscpu.csv
"IP" "Architecture:" "CPU op-mode(s):" "Byte Order:" "Address sizes:" ...
"srv1" "x86_64" "32-bit, 64-bit" "Little Endian" "39 bits physical, 48 bits virtual" ...
"srv2" "x86_64" "32-bit, 64-bit" "Little Endian" "16" ...
完整测试剧本的示例
- hosts: all
vars:
lscpu_data: "{{ ansible_play_hosts|
map('extract', hostvars, ['lscpu_out', 'stdout'])|
map('from_yaml')|
map(attribute='lscpu') }}"
lscpu_keys: "{{ lscpu_data.0|map(attribute='field') }}"
lscpu_vals: "{{ lscpu_data|map('map', attribute='data') }}"
tasks:
- command: lscpu -J
register: lscpu_out
- copy:
dest: /tmp/lscpu.csv
content: |
"IP" "{{ lscpu_keys|join('" "') }}"
{% for s,v in ansible_play_hosts|zip(lscpu_vals) %}
"{{ s }}" "{{ v|join('" "') }}"
{% endfor %}
run_once: true
delegate_to: localhost