{"id":103790,"date":"2024-09-14T15:16:10","date_gmt":"2024-09-14T07:16:10","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=103790"},"modified":"2024-09-14T15:16:10","modified_gmt":"2024-09-14T07:16:10","slug":"ansible-df-h","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/ansible-df-h.html","title":{"rendered":"ansible\u5b9e\u6218—\u78c1\u76d8\u4f7f\u7528\u7387\u7b5b\u9009"},"content":{"rendered":"

\u5e74\u5e95\u4e86\u867d\u7136\u6709\u5404\u79cd\u544a\u8b66\uff0c\u4e0d\u8fc7\u8fd8\u662f\u9700\u8981\u505a\u4e00\u4e0b\u5168\u7f51\u5de1\u68c0\uff0c\u8fd9\u91cc\u4ee5\u78c1\u76d8\u4f7f\u7528\u7387\u4e3a\u4f8b\u3002\u7531\u4e8e\u76ee\u524d\u5e73\u53f0\u4e0a\u4f7f\u7528\u7684\u662f\u5546\u4e1a\u7248\u7684\u81ea\u52a8\u5316\u5de5\u5177bmc\uff0c\u5176\u5728\u6267\u884c\u5206\u53d1\u90fd\u65b9\u9762\u975e\u5e38\u4e0d\u5b58\uff0c\u4e0d\u8fc7\u5728\u53d6\u503c\u8fd4\u56de\u4e0a\u505a\u7684\u4e0d\u597d\uff0d\uff0d\uff0d\u5f53\u7136\u4e5f\u548c\u5176\u662f\u5546\u4e1a\u5316\u5de5\u5177\u6709\u5173\uff0c\u5f88\u591a\u4e1c\u897f\u4e0d\u5f00\u6e90\uff0c\u7528shell \u7684\u8bed\u6cd5\u6982\u5ff5\u5199nsh\u65f6\u53c8\u6267\u884c\u4e0d\u901a\u8fc7\uff0cNND\u603b\u662f\u8981\u95ee\u539f\u5382\u652f\u6491 \u3002\u521a\u597d\u5728\u6d4b\u8bd5\u73af\u5883\u4e0a\u6709\u4e4b\u524d\u5b89\u88c5\u7684ansible\u73af\u5883\uff0c\u4e5f\u6d4b\u8bd5\u4e0b\u7528ansible \u6765\u5b9e\u73b0\u8be5\u529f\u80fd\u3002<\/p>\n

\u4e00\u3001\u5b9e\u73b0\u76ee\u6807<\/strong><\/div>\n

\u5c06\u6240\u6709\u4e3b\u673a\u7684\u78c1\u76d8\u5927\u4e8e75%\u7684\u4e3b\u673a\u83b7\u53d6\u5230\uff0c\u5e76\u8f93\u51fa\u4e3a\u4e0b\u9762\u7684\u683c\u5f0f\uff1a<\/p>\n

\u4e3b\u673a<\/span>IP <\/span>\u4e3b\u673a\u540d<\/span> \u78c1\u76d8\u6302\u8f7d\u70b9\u4fe1\u606f<\/span> \u78c1\u76d8\u4f7f\u7528\u7387<\/span><\/p>\n

\u5047\u5982 host1\u4e0a\u6709\u591a\u4e2a\u5206\u533a\u90fd\u5927\u4e8e75% \uff0c\u5219\u5199\u591a\u6761\u3002\u5f53\u4e3b\u673a\u4e0a\u6ca1\u6709\u4e00\u53f0\u7b26\u5408\u65f6\uff0c\u5219\u4ec0\u4e48\u90fd\u4e0d\u8f93\u51fa\u3002<\/p>\n

\u6ce8\uff1a\u73b0\u7f51\u4e3b\u673a\u4e0a\u6709\u4e24\u4e2a bond \u7f51\u5361\uff0c\u5176\u4e2d\u4e00\u4e2a\u914d\u7f6e\u7684\u662f10\u6bb5\u7684IP\uff0c\u53e6\u4e00\u4e2a\u914d\u7f6e\u7684\u662f192\u6bb5\u7684IP\u3002\u8fd9\u91cc\u8981\u83b7\u53d6\u7684\u662f10\u6bb5\u7684IP\u3002<\/p>\n

\u4e8c\u3001 ansible + awk \u7b80\u5355\u8f93\u51fa<\/strong><\/div>\n

\u8be5\u95ee\u9898\u6211\u5904\u7406\u7684\u7ed3\u679c\u662f\u4f7f\u7528ansible api \u5199\u7684\u4e00\u4e2apython\u811a\u672c\u5b9e\u73b0\u7684\u3002\u5148\u770b\u4e0b\u6211\u5728\u4e00\u4e2a\u6280\u672f\u7fa4\u548c\u4e00\u4e2a\u5927\u725b\u7684\u8ba8\u8bba\u7684\u7ed3\u679c\u3002<\/p>\n

\"\"<\/a><\/p>\n

 <\/p>\n

\u5982\u4e0a\u56fe\uff0c\u5927\u725b\u7684\u505a\u6cd5\u7528\u7684\u662fawk \u7684next\u65b9\u6cd5\u8fdb\u884c\u7684\u5904\u7406\uff0cawk \u7684next\u529f\u80fd\u6211\u4e5f\u505a\u8fc7\u8bb0\u5f55\uff0c\u5177\u4f53\u53c2\u770bawk next\u591a\u884c\u5408\u5e76<\/a> \u3002<\/p>\n

\u8fd9\u91cc\u6709\u51e0\u4e2a\u6ce8\u610f\u70b9\u9700\u8981\u8bf4\u660e\u4e0b\uff1a<\/p>\n

1\u3001\u5927\u725b\u7684\u5199\u6cd5\u4e2d\u5728\u7b5b\u9009\u4e0a\u5199\u7684\u4e0d\u4e25\u8c28\uff0c$5\u524d\u5e94\u8be5\u6709int\u8f6c\u5316\uff0c\u4e0d\u8fc7\u4e0d\u80fd\u6b63\u786e\u7684\u53d6\u5230\u503c<\/strong><\/span><\/div>\n
[root@361way ~]# df -hP|awk 'NR>1 && $5 > 20'\r\n\/dev\/xvda1 20G 4.6G 15G 25% \/\r\n\/dev\/xvdb 20G 645M 18G 4% \/data1\r\n[root@361way ~]# df -hP|awk 'NR>1 && int($5) > 20'\r\n\/dev\/xvda1 20G 4.6G 15G 25% \/\r\n<\/pre>\n

\u6709\u6ca1\u6709\u770b\u5230\uff0c\u5982\u679c\u4e0d\u505aint\u683c\u5f0f\u8f6c\u6362\u53d6\u7684\u7ed3\u679c\u662f\u6240\u6709\u5206\u533a\u7684\u4fe1\u606f\uff0c\u800c\u52a0\u4e0aint\u540e\u83b7\u53d6\u7684\u624d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c<\/p>\n

2\u3001\u5728ansible\u6267\u884c\u65f6$5\u524d\u9762\u8981\u52a0\u4e0a\u8f6c\u4e49<\/strong><\/span><\/div>\n
# ansible all -m shell -a \"df -hP|awk 'NR>1 && int($5) > 50'\"\r\n10.212.52.252 | FAILED | rc=2 >>\r\nawk: fatal: 0 is invalid as number of arguments for int\r\n[root@361way ~]# df -hP|awk 'NR>1 && int(\\$5) > 20'\r\nawk: NR>1 && int(\\$5) > 20\r\nawk: ^ backslash not last character on line\r\n<\/pre>\n

\u5982\u4e0a\u9762\u7684\u7ed3\u679c\uff0c\u5982\u679c\u5728ansible\u6267\u884c\u65f6\u4e0d\u52a0\u8f6c\u4e49\u65f6\u4f1a\u6709\u62a5\u9519\u63d0\u793a\uff0c\u5982\u679c\u52a0\u4e0a\u8f6c\u4e49\u5728\u4e3b\u673a\u7aef\u6267\u884c\u65f6\u4f1a\u81ea\u52a8\u662f\u5c06\u8f6c\u4e49\u7b26\u53bb\u6389\u7684\u7ed3\u679c\u3002\u800c\u5728\u4e3b\u673a\u7aef\u6267\u884c\u65f6\uff0c\u5982\u679c\u52a0\u4e0a\u4e86\u8f6c\u4e49\u4e5f\u4f1a\u62a5\u9519\u3002\u6240\u4ee5\u4e3b\u673a\u7aef\u4e00\u5b9a\u4e0d\u80fd\u52a0\u4e0a\u8f6c\u4e49\u3002<\/p>\n

<\/strong>3\u3001\u5728\u5b9e\u9645\u5e94\u7528\u65f6\uff0c\u8f93\u51fa\u7ed3\u679c\u53ef\u80fd\u548c\u5927\u725b\u7684\u8f93\u51fa\u6709\u5dee\u8ddd<\/strong><\/span><\/div>\n
[root@localhost ~]# ansible all -m shell -a \"df -hP|awk 'NR>1 && int(\\$5) > 30'\"|awk '\/success\/{ip=$1;next}{print ip,$0}'\r\n10.212.52.252 \/dev\/sda9 9.9G 2.9G 6.5G 31% \/opt\r\n10.212.52.252 \/dev\/sda6 5.0G 1.9G 2.8G 41% \/tmp\r\n10.212.52.252 \/dev\/sda5 9.9G 3.9G 5.5G 42% \/usr\r\n10.212.52.252\r\n10.212.52.14 \/dev\/cciss\/c0d0p5 9.9G 3.2G 6.3G 34% \/usr\r\n10.212.52.14\r\n10.212.52.16 \/dev\/cciss\/c0d0p7 9.9G 4.0G 5.4G 43% \/tmp\r\n10.212.52.16 \/dev\/cciss\/c0d0p5 9.9G 2.9G 6.5G 31% \/usrdf: `\/root\/.gvfs': Permission denied\r\n10.212.52.16\r\n<\/pre>\n

\u4e0a\u9762\u7684\u7ed3\u679c\u662f\u6211\u5728\u81ea\u5df1\u7684\u6d4b\u8bd5\u73af\u5883\u4e0a\u6267\u884c\u7684\u7ed3\u679c\u3002\u53ef\u4ee5\u770b\u5230\u591a\u51fa\u7684\u7a7a\u884c\u4e5f\u6253\u5370\u4e86\u4e3b\u673a\u7684IP \u3002\u8fd8\u4f1a\u9700\u8981\u6ce8\u610f\u7684\uff0c\u6211\u8fd9\u91ccansible\u8f93\u51fa\u7684success\u662f\u5c0f\u5199\u7684\u3002<\/p>\n

\u4e0d\u77e5\u9053\u4ee5\u4e0a\u7684\u95ee\u9898\u662f\u4e0d\u662f\u4f7f\u7528\u7684\u73af\u5883\u4e0d\u540c\u9020\u6210\u7684\u3002\u6211\u8fd0\u884c\u7684\u73af\u5883\u662fansible\u4e3b\u673a\u4e3aredhat6\uff0cansible\u7248\u672c\u4e3a1.9\uff0c\u88ab\u53d6\u6570\u636e\u4e3b\u673a\u6709redhat6\u548csuse11 \u3002\u4e0d\u8fc7\u8fd9\u90fd\u662f\u5c0f\u95ee\u9898\uff0c\u540c\u6837\u53ef\u4ee5\u901a\u8fc7\u5904\u7406\u83b7\u53d6\u5230\u6b63\u5e38\u7684\u7ed3\u679c\u3002<\/p>\n

4\u3001ansible api \u6267\u884c<\/strong><\/span><\/div>\n

\u4e0a\u9762\u7684\u5927\u725b\u7684\u7ed3\u679c\uff0c\u6211\u4f7f\u7528ansible api \u6267\u884c\uff0c\u5982\u4e0b\uff0c\u53ef\u4ee5\u5bf9\u6bd4\u4e0b\uff1a<\/p>\n

[root@localhost ~]# cat \/tmp\/test.py\r\n#!\/usr\/bin\/env python\r\n# coding=utf-8\r\n# author \uff1a www.361way.com\r\n# mail : itybku@139.com\r\nimport ansible.runner\r\n#import json\r\nrunner = ansible.runner.Runner(\r\nmodule_name='shell',\r\nmodule_args=\"df -hP|awk 'NR>1 && int($5)>30'\",\r\npattern='all',\r\nforks=10\r\n)\r\nresults = runner.run()\r\n#print results\r\nfor (hostname, result) in results['contacted'].items():\r\nif not 'failed' in result:\r\nfor line in result['stdout'].split('\\n'):\r\nprint \"%s %s\" % (hostname, line)\r\n# \u6267\u884c\u7ed3\u679c\u5982\u4e0b\r\n[root@localhost ~]# python \/tmp\/test.py\r\n10.212.52.16 \/dev\/cciss\/c0d0p7 9.9G 4.0G 5.4G 43% \/tmp\r\n10.212.52.16 \/dev\/cciss\/c0d0p5 9.9G 2.9G 6.5G 31% \/usr\r\n10.212.52.252 \/dev\/sda9 9.9G 2.9G 6.5G 31% \/opt\r\n10.212.52.252 \/dev\/sda6 5.0G 1.9G 2.8G 41% \/tmp\r\n10.212.52.252 \/dev\/sda5 9.9G 3.9G 5.5G 42% \/usr\r\n10.212.52.14 \/dev\/cciss\/c0d0p5 9.9G 3.2G 6.3G 34% \/usr\r\n<\/pre>\n
\u4e09\u3001\u8fd8\u662fansible api \u83b7\u53d6\u78c1\u76d8\u4fe1\u606f<\/strong><\/div>\n

\u4e0a\u9762\u7684\u65b9\u6cd5\u4e2d\u5b9e\u9645\u6267\u884c\u65f6\uff0c\u6bd4\u6211\u4eec\u9884\u671f\u9700\u8981\u7684\u6548\u679c\u5c11\u4e86\u4e3b\u673a\u540d\u4e00\u9879\u3002\u8fd9\u91cc\u6211\u6362\u505a\u6267\u884c\u811a\u672c\u5b9e\u73b0\uff0c\u5b9e\u73b0\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n

#\/bin\/bash\r\n# author : www.361way.com\r\nIP=`ip add show|grep inet|grep 10|awk '{print $2}'`\r\ndf -hl|grep '^\/'|sed 's\/%\/\/g'|awk '{if($5>30) print $0}'|while read line\r\ndo\r\necho $IP `hostname` $line\r\ndone\r\n# \u6267\u884c\u7ed3\u679c\u5982\u4e0b\r\n# sh aa.sh\r\n10.212.52.253\/24 localhost \/dev\/sda3 9.5G 5.7G 3.4G 64 \/\r\n10.212.52.253\/24 localhost \/dev\/sda2 39G 19G 18G 52 \/home\r\n10.212.52.253\/24 localhost \/dev\/sda6 9.5G 7.1G 2.0G 78 \/usr\r\n<\/pre>\n

\u4f7f\u7528ansible api \u6267\u884c\u8be5\u811a\u672c\u7684\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

# python dfscript.py\r\ndf: `\/root\/.gvfs': Permission denied\r\ndf: `\/root\/.gvfs': Permission denied\r\n10.212.52.16\/24 linux \/dev\/cciss\/c0d0p7 9.9G 4.0G 5.4G 43 \/tmp\r\n10.212.52.16\/24 linux \/dev\/cciss\/c0d0p5 9.9G 2.9G 6.5G 31 \/usr\r\n10.212.52.252\/24 zjhz-bmc-test \/dev\/sda9 9.9G 2.9G 6.5G 31 \/opt\r\n10.212.52.252\/24 zjhz-bmc-test \/dev\/sda6 5.0G 1.9G 2.8G 41 \/tmp\r\n10.212.52.252\/24 zjhz-bmc-test \/dev\/sda5 9.9G 3.9G 5.5G 42 \/usr\r\n10.212.52.14\/24 linux \/dev\/cciss\/c0d0p5 9.9G 3.2G 6.3G 34 \/usr\r\n<\/pre>\n

dfscript.py\u811a\u672c\u5185\u5bb9\u5982\u4e0b<\/p>\n

# cat dfscript.py\r\n#!\/usr\/bin\/env python\r\n# coding=utf-8\r\n# author \uff1a www.361way.com\r\n# mail : itybku@139.com\r\nimport ansible.runner\r\n#import json\r\nrunner = ansible.runner.Runner(\r\nmodule_name='script',\r\nmodule_args=\"aa.sh\",\r\npattern='all',\r\nforks=10\r\n)\r\nresults = runner.run()\r\n#print results\r\nfor (hostname, result) in results['contacted'].items():\r\nif not 'failed' in result:\r\nfor line in result['stdout'].split('\\r\\n'):\r\n#print \"%s %s\" % (hostname, line)\r\nprint line\r\n<\/pre>\n

\u76f4\u63a5\u5bf9\u8be5\u811a\u672c\u6267\u884c\u540e\u7684\u7ed3\u679c\u8fdb\u884cgrep \u6807\u51c6\u8f93\u51fa\u65f6\uff0c\u4f1a\u53d1\u73b0\u5176\u884c\u4e0e\u884c\u4e4b\u95f4\u662f\u4ee5\\r\\n\u8fd9\u6837\u7684\u65b9\u5f0f\u5206\u884c\u7684\u3002<\/p>\n

\"\"<\/a><\/p>\n

\u6240\u4ee5\u5728\u6570\u636e\u83b7\u53d6\u65b9\u9762\uff0c\u5c3d\u91cf\u4ee5ansible api \u7684\u65b9\u5f0f\u8fdb\u884c\u83b7\u53d6\uff0c\u800capi \u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u65e0\u6cd5\u662f\u51e0\u4e2a\u53c2\u6570\u7684\u66ff\u6362\u540e\u9762\u518d\u8c03\u7528run\u65b9\u6cd5\uff0c\u6700\u7ec8\u5728\u5bf9\u7ed3\u679c\u8fdb\u884c\u5904\u7406\u3002\u800c\u6d89\u53ca\u591a\u9879\u4fe1\u606f\u83b7\u53d6\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u81ea\u5b9a\u4e49\u6a21\u5757\u7684\u65b9\u6cd5\uff0c\u5148\u5c06\u6240\u9700\u6570\u636e\u53d6\u56de\u6765\uff0c\u4ee5json\u65b9\u5f0f\u8fd4\u56de\uff0d\uff0d\uff0dansible\u81ea\u5b9a\u4e49\u6a21\u5757\u6240\u8981\u6c42\u7684\u683c\u5f0f\u3002\u8fd4\u56de\u540e\u53ef\u4ee5\u518d\u4ee5api \u6216\u5176\u4ed6\u65b9\u5f0f\u5904\u7406\u8fd4\u56de\u7684\u6570\u636e\u5373\u53ef\u3002<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.361way.com\/ansible-diskinfo\/4995.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttps:\/\/lrxjmw.cn\/ansible-df-h.html<\/a>\u7f16\u8f91\uff1a\u5f20\u96c4\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5e74\u5e95\u4e86\u867d\u7136\u6709\u5404\u79cd\u544a\u8b66\uff0c\u4e0d\u8fc7\u8fd8\u662f\u9700\u8981\u505a\u4e00\u4e0b\u5168\u7f51\u5de1\u68c0\uff0c\u8fd9\u91cc\u4ee5\u78c1\u76d8\u4f7f\u7528\u7387\u4e3a\u4f8b\u3002\u7531\u4e8e\u76ee\u524d\u5e73\u53f0\u4e0a\u4f7f\u7528\u7684\u662f\u5546\u4e1a\u7248\u7684\u81ea\u52a8\u5316\u5de5 […]<\/p>\n","protected":false},"author":643,"featured_media":104120,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-103790","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/103790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=103790"}],"version-history":[{"count":6,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/103790\/revisions"}],"predecessor-version":[{"id":104283,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/103790\/revisions\/104283"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/104120"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=103790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=103790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=103790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}