-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansiblepi-liang-shou-ji-cpuhe-xin-shu-datamu-lu-nei-cun-xi-tong-ban-ben-xin-xi.html
executable file
·118 lines (101 loc) · 6.96 KB
/
ansiblepi-liang-shou-ji-cpuhe-xin-shu-datamu-lu-nei-cun-xi-tong-ban-ben-xin-xi.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!doctype html>
<html lang="">
<head>
<meta charset="utf-8"/>
<title>ansible批量收集cpu核心数、data目录、内存、系统版本信息 - Restart</title>
<meta name="author" content="wxz" />
<meta name="copyright" content="wxz" />
<meta property="og:site_name" content="Restart" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="ansible批量收集cpu核心数、data目录、内存、系统版本信息" />
<meta name="date" content="2023-10-14 17:20:00+08:00" />
<meta property="og:type" content="article" />
<meta property="og:locale" content="en" />
<meta property="og:published_time" content="2023-10-14 17:20:00+08:00" />
<meta property="og:title" content="ansible批量收集cpu核心数、data目录、内存、系统版本信息" />
<meta property="og:url" content="./ansiblepi-liang-shou-ji-cpuhe-xin-shu-datamu-lu-nei-cun-xi-tong-ban-ben-xin-xi.html" />
<meta property="og:description" content="ansible收集cpu核心数、data目录、内存、系统版本信息 该剧本会收集(支持centos6、centos7系统) 主机ip地址、cpu核心数、data目录、内存、系统版本信息 - name: Gather System Information hosts: servers become: yes tasks: - name: Get number of CPU cores debug: msg …" />
<meta name="description" content="ansible收集cpu核心数、data目录、内存、系统版本信息 该剧本会收集(支持centos6、centos7系统) 主机ip地址、cpu核心数、data目录、内存、系统版本信息 - name: Gather System Information hosts: servers become: yes tasks: - name: Get number of CPU cores debug: msg …" />
<meta name="HandheldFriendly" content="True" />
<meta name="MobileOptimized" content="320" />
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0" />
<link rel="shortcut icon" href="./favicon.ico" type="image/x-icon" />
<link rel="icon" href="./favicon.ico" type="image/x-icon" />
<link href='https://fonts.googleapis.com/css?family=Droid+Sans:700,400|Droid+Sans+Mono' rel='stylesheet' type='text/css' />
<link rel="stylesheet" href="./theme/css/main.css" type="text/css" />
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<header class="clearfix" role="banner">
<div class="wrapper">
<h1 class="huge"><a href=".">Restart</a></h1>
</div>
</header>
<div role="main" class="content clearfix">
<article>
<div class="post wrapper">
<h1>ansible批量收集cpu核心数、data目录、内存、系统版本信息</h1>
<h1>ansible收集cpu核心数、data目录、内存、系统版本信息</h1>
<p>该剧本会收集(支持centos6、centos7系统)
主机ip地址、cpu核心数、data目录、内存、系统版本信息</p>
<div class="highlight"><pre><span></span><code>-<span class="w"> </span>name:<span class="w"> </span>Gather<span class="w"> </span>System<span class="w"> </span>Information
<span class="w"> </span>hosts:<span class="w"> </span>servers
<span class="w"> </span>become:<span class="w"> </span>yes
<span class="w"> </span>tasks:
<span class="w"> </span>-<span class="w"> </span>name:<span class="w"> </span>Get<span class="w"> </span>number<span class="w"> </span>of<span class="w"> </span>CPU<span class="w"> </span>cores
<span class="w"> </span>debug:
<span class="w"> </span>msg:<span class="w"> </span><span class="s2">"{{ ansible_processor_vcpus }}"</span>
<span class="w"> </span>-<span class="w"> </span>name:<span class="w"> </span>Get<span class="w"> </span>data<span class="w"> </span>directory<span class="w"> </span>size
<span class="w"> </span>vars:
<span class="w"> </span>size_bytes:<span class="w"> </span><span class="s2">"{{ ansible_mounts | selectattr('mount', 'match', '/data</span>$<span class="s2">') | map(attribute='size_total') | list | first }}"</span>
<span class="w"> </span>set_fact:
<span class="w"> </span>data_dir_size_gb:<span class="w"> </span><span class="s2">"{{ (size_bytes | int / 1024 / 1024 / 1024) | round(2) }}"</span>
<span class="w"> </span>-<span class="w"> </span>name:<span class="w"> </span>Get<span class="w"> </span>memory<span class="w"> </span>size
<span class="w"> </span>vars:
<span class="w"> </span>size_bytes:<span class="w"> </span><span class="s2">"{{ ansible_memtotal_mb * 1024 * 1024 }}"</span>
<span class="w"> </span>set_fact:
<span class="w"> </span>memory_size_gb:<span class="w"> </span><span class="s2">"{{ (size_bytes | int / 1024 / 1024 / 1024) | round(2) }}"</span>
<span class="w"> </span>-<span class="w"> </span>name:<span class="w"> </span>Get<span class="w"> </span>system<span class="w"> </span>version
<span class="w"> </span>debug:
<span class="w"> </span>msg:<span class="w"> </span><span class="s2">"{{ ansible_distribution }} {{ ansible_distribution_version }}"</span>
<span class="w"> </span>post_tasks:
<span class="w"> </span>-<span class="w"> </span>name:<span class="w"> </span>Display<span class="w"> </span>gathered<span class="w"> </span>information
<span class="w"> </span>debug:
<span class="w"> </span>msg:<span class="w"> </span><span class="s2">"Host: {{ inventory_hostname }}\nCPU Cores: {{ ansible_processor_vcpus }}\nData Directory Size: {{ data_dir_size_gb }} GB\nMemory Size: {{ memory_size_gb }} GB\nSystem Version: {{ ansible_distribution }} {{ ansible_distribution_version }}"</span>
</code></pre></div>
<p>执行过程:这是1.yaml代表上述剧本内容</p>
<div class="highlight"><pre><span></span><code>ansible-playbook<span class="w"> </span><span class="m">1</span>.yaml
</code></pre></div>
<p><img alt="image-20230407114126861" src="https://cdn.jsdelivr.net/gh/MakerCosy/PicCDN/img/image-20230407114126861.png"></p>
<p>注意:
如果系统没有data目录,那台主机将会执行失败,不会输出结果,确保执行主机上有data目录,如果没有,请在hosts变量中主机,剔除掉没有data目录的主机数目</p>
<a href="https://twitter.com/share" class="twitter-share-button" data-via="" data-lang="en" data-size="large" data-related="">Tweet</a>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</div>
<div class="meta wrapper">
<time datetime="2023-10-14T17:20:00+08:00" pubdate>Sat 14 October 2023</time>
<ul class="tag clearfix">
<li><a href="./category/page.html">page</a></li>
</ul>
</div> </article>
</div>
<footer class="clearfix">
<div class="wrapper pages">
<ul class="nav">
<li><a href="./archives.html">Archive</a></li>
</ul>
</div>
<div class="copy wrapper">
<ul class="social">
<li><a href="https://dianyi.ng/">You can add links in your config file</a></li>
<li><a href="#">Another social link</a></li>
</ul>
<p role="contentinfo"> © 2017 wxz
<br>
Proudly powered by <a href="http://alexis.notmyidea.org/pelican/">Pelican</a>.</p>
</div>
</footer>
</body>
</html>