{"id":46454,"date":"2025-07-30T03:12:26","date_gmt":"2025-07-29T19:12:26","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/46454.html"},"modified":"2025-07-30T03:12:26","modified_gmt":"2025-07-29T19:12:26","slug":"ansible%e8%87%aa%e5%8a%a8%e5%8c%96%e6%9c%8d%e5%8a%a1%e5%99%a8%e6%9b%b4%e6%96%b0%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/46454.html","title":{"rendered":"Ansible\u81ea\u52a8\u5316\u670d\u52a1\u5668\u66f4\u65b0\u6307\u5357"},"content":{"rendered":"<p>  \u672c\u6587\u8fd8\u6709\u914d\u5957\u7684\u7cbe\u54c1\u8d44\u6e90&#xff0c;\u70b9\u51fb\u83b7\u53d6  <img decoding=\"async\" alt=\"menu-r.4af5f7ec.gif\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729191225-68891d190640f.gif\" style=\"width:16px;margin-left:4px;vertical-align:text-bottom;cursor:text\" \/> <\/p>\n<p> \u7b80\u4ecb&#xff1a;Ansible\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u81ea\u52a8\u5316\u5de5\u5177&#xff0c;\u7528\u4e8e\u914d\u7f6e\u7ba1\u7406\u3001\u5e94\u7528\u90e8\u7f72\u4ee5\u53ca\u4efb\u52a1\u6267\u884c\u3002\u57fa\u4e8eAgentless\u67b6\u6784&#xff0c;Ansible\u5229\u7528SSH\u534f\u8bae\u8fdc\u7a0b\u7ba1\u7406\u670d\u52a1\u5668&#xff0c;\u7b80\u5316\u4e86\u7cfb\u7edf\u7ba1\u7406\u5458\u5de5\u4f5c\u3002\u672c\u6587\u6df1\u5165\u63a2\u8ba8\u4e86Ansible\u7684\u7ec4\u6210\u90e8\u5206\u548c\u4e3b\u8981\u64cd\u4f5c&#xff0c;\u5305\u62ec\u63a7\u5236\u8282\u70b9\u3001\u88ab\u7ba1\u7406\u8282\u70b9\u3001Inventory\u3001Playbook\u3001Gathering Facts\u3001Tasks\u3001Handlers\u548cRoles&#xff0c;\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528Ansible\u6267\u884c\u670d\u52a1\u5668\u66f4\u65b0\u3002\u7279\u522b\u5f3a\u8c03\u4e86\u4f7f\u7528ansible-playbook\u6765\u6267\u884c\u5305\u542b\u66f4\u65b0\u6307\u4ee4\u7684\u811a\u672c\u548cansible\u6a21\u5757&#xff0c;\u4ee5\u53ca\u5982\u4f55\u6839\u636e\u73af\u5883\u914d\u7f6eInventory\u6765\u8fd0\u884c\u66f4\u65b0playbook\u3002\u201dansible-update_servers\u201d\u9879\u76ee\u5c55\u793a\u4e86\u5982\u4f55\u81ea\u52a8\u5316\u670d\u52a1\u5668\u66f4\u65b0\u6d41\u7a0b&#xff0c;\u5c55\u793a\u4e86Ansible\u5728IT\u57fa\u7840\u8bbe\u65bd\u81ea\u52a8\u5316\u4e2d\u7684\u5e94\u7528\u3002  <img decoding=\"async\" alt=\"ansible-update_servers:ansible\u66f4\u65b0\u670d\u52a1\u5668\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729191225-68891d190c923.png\" \/> <\/p>\n<h2> 1. Ansible\u81ea\u52a8\u5316\u5de5\u5177\u4ecb\u7ecd <\/h2>\n<p> \u81ea\u52a8\u5316\u662f\u73b0\u4ee3IT\u8fd0\u7ef4\u7684\u6807\u914d\u4e4b\u4e00&#xff0c;\u800cAnsible\u5c31\u662f\u8be5\u9886\u57df\u4e2d\u7684\u4e00\u9897\u8000\u773c\u660e\u661f\u3002\u5b83\u57fa\u4e8ePython\u5f00\u53d1&#xff0c;\u80fd\u591f\u5b9e\u73b0IaC&#xff08;Infrastructure as Code&#xff09;&#xff0c;\u901a\u8fc7\u7b80\u5355\u7684\u914d\u7f6e\u5373\u53ef\u5b9e\u73b0\u590d\u6742\u7684\u7cfb\u7edf\u7ba1\u7406\u4efb\u52a1\u3002Ansible\u4e0d\u9700\u8981\u5728\u76ee\u6807\u4e3b\u673a\u4e0a\u5b89\u88c5\u4ee3\u7406&#xff0c;\u5b83\u5229\u7528SSH\u534f\u8bae\u8fdb\u884c\u901a\u4fe1&#xff0c;\u7ed3\u5408YAML\u8bed\u8a00\u7f16\u5199\u7684Playbook&#xff0c;\u53ef\u4ee5\u8f7b\u677e\u90e8\u7f72\u5e94\u7528\u3001\u914d\u7f6e\u7cfb\u7edf\u3001\u6267\u884c\u4efb\u52a1\u3002 <\/p>\n<p> \u5728\u672c\u7ae0\u4e2d&#xff0c;\u6211\u4eec\u5c06\u7b80\u5355\u56de\u987eAnsible\u7684\u5386\u53f2\u548c\u53d1\u5c55&#xff0c;\u6df1\u5165\u63a2\u8ba8\u5176\u65e0\u4ee3\u7406&#xff08;Agentless&#xff09;\u67b6\u6784\u7684\u4f18\u52bf&#xff0c;\u5e76\u521d\u6b65\u4e86\u89e3SSH\u534f\u8bae\u5728Ansible\u5de5\u4f5c\u6d41\u7a0b\u4e2d\u7684\u5e94\u7528\u3002\u901a\u8fc7\u672c\u7ae0&#xff0c;\u8bfb\u8005\u5c06\u5bf9Ansible\u6709\u4e00\u4e2a\u57fa\u672c\u7684\u8ba4\u8bc6&#xff0c;\u5e76\u4e3a\u5176\u540e\u7eed\u7ae0\u8282\u7684\u5b66\u4e60\u6253\u4e0b\u826f\u597d\u7684\u57fa\u7840\u3002\u63a5\u4e0b\u6765\u7684\u7ae0\u8282\u5c06\u9010\u6b65\u6df1\u5165&#xff0c;\u8be6\u7ec6\u4ecb\u7ecdAnsible\u7684\u5173\u952e\u7ec4\u4ef6&#xff0c;\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528Ansible\u8fdb\u884c\u9ad8\u6548\u7684\u81ea\u52a8\u5316\u8fd0\u7ef4\u4efb\u52a1\u3002 <\/p>\n<p> \u73b0\u5728&#xff0c;\u8ba9\u6211\u4eec\u5f00\u59cb\u63a2\u7d22Ansible\u81ea\u52a8\u5316\u5de5\u5177\u7684\u65c5\u7a0b\u3002 <\/p>\n<h2> 2. Agentless\u67b6\u6784\u548cSSH\u534f\u8bae <\/h2>\n<h3> 2.1 Ansible\u7684Agentless\u67b6\u6784\u4f18\u52bf <\/h3>\n<h4> 2.1.1 \u7ba1\u7406\u8282\u70b9\u4e0e\u88ab\u7ba1\u7406\u8282\u70b9 <\/h4>\n<p> Ansible\u7684Agentless\u67b6\u6784\u8bbe\u8ba1\u7406\u5ff5\u6452\u5f03\u4e86\u4f20\u7edf\u9700\u8981\u5728\u88ab\u7ba1\u7406\u8282\u70b9\u4e0a\u5b89\u88c5\u4ee3\u7406\u7a0b\u5e8f\u7684\u505a\u6cd5&#xff0c;\u8f6c\u800c\u5229\u7528SSH\u534f\u8bae\u76f4\u63a5\u8fde\u63a5\u81f3\u76ee\u6807\u7cfb\u7edf\u8fdb\u884c\u7ba1\u7406\u548c\u914d\u7f6e\u3002\u5728\u8fd9\u6837\u7684\u67b6\u6784\u4e2d&#xff0c;\u5b58\u5728\u4e24\u4e2a\u4e3b\u8981\u7ec4\u4ef6&#xff1a;\u7ba1\u7406\u8282\u70b9&#xff08;Control Node&#xff09;\u548c\u88ab\u7ba1\u7406\u8282\u70b9&#xff08;Managed Nodes&#xff09;\u3002 <\/p>\n<ul>\n<li>  \u7ba1\u7406\u8282\u70b9  &#xff1a;\u8fd9\u662f\u8fd0\u884cAnsible\u4efb\u52a1\u7684\u673a\u5668\u3002\u7ba1\u7406\u5458\u5728\u7ba1\u7406\u8282\u70b9\u4e0a\u7f16\u5199\u548c\u6267\u884cPlaybook&#xff0c;\u63a7\u5236\u88ab\u7ba1\u7406\u8282\u70b9\u7684\u64cd\u4f5c\u3002\u7ba1\u7406\u8282\u70b9\u901a\u5e38\u9700\u8981\u5b89\u88c5Ansible\u8f6f\u4ef6\u5305\u53ca\u5176\u4f9d\u8d56\u3002 <\/li>\n<li>  \u88ab\u7ba1\u7406\u8282\u70b9  &#xff1a;\u8fd9\u4e9b\u662f\u88abAnsible\u63a7\u5236\u548c\u7ba1\u7406\u7684\u8fdc\u7a0b\u670d\u52a1\u5668\u3002\u5b83\u4eec\u4e0d\u9700\u8981\u5728\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4efb\u4f55\u989d\u5916\u7684Ansible\u4ee3\u7406\u6216\u670d\u52a1\u3002\u901a\u8fc7SSH\u548c\u57fa\u4e8ePython\u7684\u5de5\u5177&#xff0c;Ansible\u80fd\u591f\u4ece\u7ba1\u7406\u8282\u70b9\u6267\u884c\u547d\u4ee4\u548c\u811a\u672c\u3002 <\/li>\n<\/ul>\n<p> \u8fd9\u79cd\u8bbe\u8ba1\u5927\u5e45\u7b80\u5316\u4e86\u8fd0\u7ef4\u7684\u590d\u6742\u6027&#xff0c;\u56e0\u4e3a\u65e0\u9700\u62c5\u5fc3\u5728\u5404\u4e2a\u670d\u52a1\u5668\u4e0a\u5b89\u88c5\u548c\u7ef4\u62a4\u4ee3\u7406\u7684\u95ee\u9898\u3002\u8fd9\u610f\u5473\u7740\u7ba1\u7406\u8282\u70b9\u53ef\u4ee5\u8f7b\u677e\u5730\u6269\u5c55\u4ee5\u7ba1\u7406\u4efb\u610f\u6570\u91cf\u7684\u88ab\u7ba1\u7406\u8282\u70b9&#xff0c;\u53ea\u8981\u5b83\u4eec\u53ef\u4ee5\u901a\u8fc7SSH\u53ef\u8fbe\u3002 <\/p>\n<h4> 2.1.2 Agentless\u67b6\u6784\u7684\u6548\u7387\u4e0e\u5b89\u5168\u6027 <\/h4>\n<p> \u7531\u4e8eAgentless\u67b6\u6784\u7684\u4e3b\u8981\u4f18\u70b9\u4e4b\u4e00\u662f\u5b83\u6d88\u9664\u4e86\u4ee3\u7406&#xff0c;\u56e0\u6b64\u6574\u4f53\u7ba1\u7406\u5f00\u9500\u5927\u5927\u964d\u4f4e&#xff0c;\u8fd9\u5e26\u6765\u4e86\u6548\u7387\u7684\u63d0\u5347&#xff1a; <\/p>\n<ul>\n<li>  \u65e0\u4ee3\u7406\u67b6\u6784\u7684\u6548\u7387  &#xff1a;\u5728\u6ca1\u6709\u4ee3\u7406\u7684\u60c5\u51b5\u4e0b&#xff0c;\u90e8\u7f72\u548c\u914d\u7f6e\u7cfb\u7edf\u53d8\u5f97\u66f4\u5feb\u901f&#xff0c;\u4e14\u4e0d\u9700\u8981\u8003\u8651\u4ee3\u7406\u7684\u66f4\u65b0\u548c\u517c\u5bb9\u6027\u95ee\u9898\u3002 <\/li>\n<li>  \u7ef4\u62a4\u5f00\u9500\u7684\u51cf\u5c11  &#xff1a;\u7ba1\u7406\u5458\u65e0\u9700\u7ba1\u7406\u3001\u66f4\u65b0\u6216\u8865\u4e01\u7cfb\u7edf\u4e0a\u7684\u4ee3\u7406\u8f6f\u4ef6&#xff0c;\u4ece\u800c\u8282\u7ea6\u65f6\u95f4\u548c\u8d44\u6e90\u3002 <\/li>\n<\/ul>\n<p> \u5b89\u5168\u6027\u65b9\u9762&#xff0c;Ansible\u7684Agentless\u67b6\u6784\u540c\u6837\u8868\u73b0\u51fa\u8272&#xff1a; <\/p>\n<ul>\n<li>  \u5b89\u5168\u7684\u8fdc\u7a0b\u6267\u884c  &#xff1a;\u901a\u8fc7SSH\u5bc6\u94a5\u8ba4\u8bc1\u548c\u4e3b\u673a\u5bc6\u94a5\u68c0\u67e5&#xff0c;\u786e\u4fdd\u4e86\u901a\u4fe1\u8fc7\u7a0b\u7684\u5b89\u5168\u6027\u3002 <\/li>\n<li>  \u6700\u5c0f\u6743\u9650\u539f\u5219  &#xff1a;Ansible\u4ec5\u9700\u8981\u5fc5\u8981\u7684\u6743\u9650\u6765\u6267\u884c\u4efb\u52a1&#xff0c;\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f7f\u7528sudo\u6743\u9650&#xff0c;\u800c\u4e0d\u662f\u4ee5root\u7528\u6237\u767b\u5f55\u3002\u8fd9\u9650\u5236\u4e86\u6f5c\u5728\u7684\u5b89\u5168\u98ce\u9669\u3002 <\/li>\n<\/ul>\n<p> \u6b64\u5916&#xff0c;\u56e0\u4e3a\u4e0d\u9700\u8981\u6253\u5f00\u66f4\u591a\u7684\u7aef\u53e3\u6216\u8bbe\u7f6e\u4ee3\u7406\u76d1\u542c\u670d\u52a1&#xff0c;\u7cfb\u7edf\u672c\u8eab\u7684\u66b4\u9732\u9762\u76f8\u5bf9\u66f4\u5c0f&#xff0c;\u4ece\u800c\u63d0\u9ad8\u4e86\u5b89\u5168\u6027\u3002 <\/p>\n<h3> 2.2 SSH\u534f\u8bae\u5728Ansible\u4e2d\u7684\u5e94\u7528 <\/h3>\n<h4> 2.2.1 SSH\u7684\u5de5\u4f5c\u539f\u7406 <\/h4>\n<p> Secure Shell&#xff08;SSH&#xff09;\u662f\u4e00\u79cd\u5b89\u5168\u7684\u7f51\u7edc\u534f\u8bae&#xff0c;\u7528\u4e8e\u5728\u4e0d\u5b89\u5168\u7684\u7f51\u7edc\u4e2d\u63d0\u4f9b\u52a0\u5bc6\u7684\u7f51\u7edc\u670d\u52a1\u3002Ansible\u5229\u7528SSH\u534f\u8bae\u6765\u5b89\u5168\u5730\u8fde\u63a5\u5230\u8fdc\u7a0b\u4e3b\u673a\u5e76\u6267\u884c\u547d\u4ee4\u3002 <\/p>\n<p> SSH\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u901a\u4fe1&#xff1a; <\/p>\n<li>  \u7248\u672c\u534f\u5546  &#xff1a;\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u901a\u8fc7\u4ea4\u6362SSH\u7248\u672c\u4fe1\u606f\u6765\u786e\u5b9a\u652f\u6301\u7684\u6700\u9ad8\u7248\u672c\u3002 <\/li>\n<li>  \u5bc6\u94a5\u4ea4\u6362  &#xff1a;\u4f7f\u7528\u975e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\u4ea4\u6362\u5bc6\u94a5&#xff0c;\u786e\u4fdd\u5bc6\u94a5\u7684\u5b89\u5168\u4f20\u8f93\u3002 <\/li>\n<li>  \u8eab\u4efd\u9a8c\u8bc1  &#xff1a;\u5ba2\u6237\u7aef\u901a\u8fc7\u63d0\u4f9b\u5bc6\u7801\u6216\u5bc6\u94a5\u7684\u65b9\u5f0f\u5bf9\u670d\u52a1\u5668\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002 <\/li>\n<li>  \u4f1a\u8bdd\u52a0\u5bc6  &#xff1a;\u4e00\u65e6\u901a\u8fc7\u8eab\u4efd\u9a8c\u8bc1&#xff0c;\u540e\u7eed\u7684\u6240\u6709\u901a\u4fe1\u90fd\u901a\u8fc7\u52a0\u5bc6\u901a\u9053\u8fdb\u884c&#xff0c;\u4fdd\u62a4\u6570\u636e\u7684\u9690\u79c1\u548c\u5b8c\u6574\u6027\u3002 <\/li>\n<h4> 2.2.2 SSH\u5bc6\u94a5\u4ea4\u6362\u4e0e\u8ba4\u8bc1\u673a\u5236 <\/h4>\n<p> SSH\u5bc6\u94a5\u4ea4\u6362\u7b97\u6cd5\u786e\u4fdd\u4e86\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u53ef\u4ee5\u5b89\u5168\u5730\u5171\u4eab\u5bc6\u94a5\u4fe1\u606f\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u4e0d\u9700\u8981\u63d0\u524d\u4ea4\u6362\u5bc6\u94a5&#xff0c;\u5e76\u4e14\u4f7f\u7528\u4e86\u975e\u5bf9\u79f0\u52a0\u5bc6\u65b9\u6cd5&#xff0c;\u4f7f\u5f97\u5373\u4f7f\u5bc6\u94a5\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u88ab\u622a\u83b7&#xff0c;\u4e5f\u65e0\u6cd5\u88ab\u7b2c\u4e09\u65b9\u89e3\u5bc6\u3002 <\/p>\n<p> \u8ba4\u8bc1\u673a\u5236\u662fSSH\u5b89\u5168\u901a\u4fe1\u4e2d\u7684\u53e6\u4e00\u4e2a\u5173\u952e\u7ec4\u6210\u90e8\u5206&#xff0c;\u5b83\u5305\u62ec&#xff1a; <\/p>\n<ul>\n<li>  \u5bc6\u7801\u8ba4\u8bc1  &#xff1a;\u7528\u6237\u8f93\u5165\u5bc6\u7801\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002 <\/li>\n<li>  \u516c\u94a5\u8ba4\u8bc1  &#xff1a;\u66f4\u5b89\u5168\u7684\u65b9\u5f0f&#xff0c;\u5ba2\u6237\u7aef\u4f7f\u7528\u79c1\u94a5\u7b7e\u540d\u6d88\u606f&#xff0c;\u670d\u52a1\u5668\u4f7f\u7528\u76f8\u5e94\u7684\u516c\u94a5\u9a8c\u8bc1\u7b7e\u540d\u3002 <\/li>\n<li>  \u591a\u56e0\u7d20\u8ba4\u8bc1  &#xff1a;\u7ed3\u5408\u5bc6\u7801\u548c\u5bc6\u94a5\u8fdb\u884c\u8ba4\u8bc1&#xff0c;\u751a\u81f3\u53ef\u4ee5\u52a0\u5165\u5176\u4ed6\u56e0\u7d20\u5982\u77ed\u4fe1\u9a8c\u8bc1\u7801\u3002 <\/li>\n<\/ul>\n<h4> 2.2.3 SSH\u7aef\u53e3\u8f6c\u53d1\u4e0e\u96a7\u9053 <\/h4>\n<p> SSH\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u8fdc\u7a0b\u767b\u5f55\u534f\u8bae&#xff0c;\u5b83\u8fd8\u652f\u6301\u7aef\u53e3\u8f6c\u53d1\u529f\u80fd&#xff0c;\u8fd9\u5141\u8bb8SSH\u96a7\u9053\u6765\u4fdd\u62a4\u5185\u90e8\u7f51\u7edc\u7684\u6d41\u91cf\u3002 <\/p>\n<ul>\n<li>  \u672c\u5730\u7aef\u53e3\u8f6c\u53d1  &#xff1a;\u5c06\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0a\u7684\u7aef\u53e3\u6620\u5c04\u5230\u672c\u5730\u673a\u5668\u4e0a\u7684\u4e00\u4e2a\u7aef\u53e3&#xff0c;\u8fd9\u6837\u6240\u6709\u901a\u8fc7\u8be5\u672c\u5730\u7aef\u53e3\u7684\u6d41\u91cf\u90fd\u4f1a\u88ab\u8f6c\u53d1\u5230\u8fdc\u7a0b\u7aef\u53e3\u3002 <\/li>\n<li>  \u8fdc\u7a0b\u7aef\u53e3\u8f6c\u53d1  &#xff1a;\u5c06\u672c\u5730\u673a\u5668\u7684\u4e00\u4e2a\u7aef\u53e3\u6620\u5c04\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0a\u7684\u4e00\u4e2a\u7aef\u53e3&#xff0c;\u8fd9\u6837\u6240\u6709\u901a\u8fc7\u8be5\u8fdc\u7a0b\u7aef\u53e3\u7684\u6d41\u91cf\u90fd\u4f1a\u88ab\u8f6c\u53d1\u5230\u672c\u5730\u7aef\u53e3\u3002 <\/li>\n<\/ul>\n<p> \u901a\u8fc7\u7aef\u53e3\u8f6c\u53d1&#xff0c;SSH\u53ef\u4ee5\u521b\u5efa\u52a0\u5bc6\u7684\u901a\u9053\u6765\u4f20\u8f93\u654f\u611f\u6570\u636e&#xff0c;\u4f8b\u5982\u6570\u636e\u5e93\u8fde\u63a5\u6216\u5185\u90e8\u670d\u52a1\u7684\u6d41\u91cf\u3002 <\/p>\n<p>graph LR<br \/>\n    A[\u672c\u5730\u673a\u5668] &#8212; SSH\u96a7\u9053 &#8211;&gt; B[SSH\u670d\u52a1\u5668]<br \/>\n    B &#8212; \u7aef\u53e3\u8f6c\u53d1 &#8211;&gt; C[\u8fdc\u7a0b\u670d\u52a1]<\/p>\n<p>    style A fill:#f9f,stroke:#333,stroke-width:2px<br \/>\n    style B fill:#ccf,stroke:#333,stroke-width:2px<br \/>\n    style C fill:#cfc,stroke:#333,stroke-width:2px<\/p>\n<p> \u5728\u4e0a\u8ff0\u573a\u666f\u4e2d&#xff0c;\u7f51\u7edc\u6d41\u91cf\u901a\u8fc7SSH\u96a7\u9053\u5b89\u5168\u5730\u4f20\u8f93&#xff0c;\u4fdd\u8bc1\u4e86\u6570\u636e\u7684\u673a\u5bc6\u6027\u548c\u5b8c\u6574\u6027&#xff0c;\u907f\u514d\u4e86\u6570\u636e\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u7684\u76d1\u542c\u548c\u7be1\u6539\u3002 <\/p>\n<p> \u5728\u4e0b\u4e00\u7ae0\u8282\u4e2d&#xff0c;\u6211\u4eec\u5c06\u7ee7\u7eed\u63a2\u8ba8Ansible\u7684\u5176\u5b83\u7ec4\u6210\u90e8\u5206&#xff0c;\u5305\u62ec\u63a7\u5236\u8282\u70b9\u3001\u88ab\u7ba1\u7406\u8282\u70b9\u3001Inventory\u548cPlaybook\u7684\u8bbe\u8ba1\u4e0e\u4f7f\u7528\u3002 <\/p>\n<h2> 3. Ansible\u7ec4\u6210\u90e8\u5206&#xff1a;\u63a7\u5236\u8282\u70b9\u3001\u88ab\u7ba1\u7406\u8282\u70b9\u3001Inventory\u3001Playbook <\/h2>\n<h3> 3.1 Ansible\u63a7\u5236\u8282\u70b9\u7684\u914d\u7f6e\u4e0e\u7ba1\u7406 <\/h3>\n<h4> 3.1.1 \u63a7\u5236\u8282\u70b9\u7684\u5b89\u88c5\u4e0e\u914d\u7f6e <\/h4>\n<p> \u63a7\u5236\u8282\u70b9\u662fAnsible\u7684\u4e3b\u8981\u64cd\u4f5c\u5e73\u53f0&#xff0c;\u8d1f\u8d23\u53d1\u9001\u4efb\u52a1\u5230\u88ab\u7ba1\u7406\u8282\u70b9\u6267\u884c\u3002\u901a\u5e38\u60c5\u51b5\u4e0b&#xff0c;\u4efb\u4f55\u5b89\u88c5\u4e86Python\u7684Linux\u6216Unix\u7cfb\u7edf\u90fd\u53ef\u4ee5\u4f5c\u4e3a\u63a7\u5236\u8282\u70b9\u3002\u5b89\u88c5Ansible\u975e\u5e38\u7b80\u5355&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u6216pip\u6765\u5b89\u88c5&#xff1a; <\/p>\n<p># \u4f7f\u7528\u5305\u7ba1\u7406\u5668\u5b89\u88c5&#xff08;\u4ee5Ubuntu\u4e3a\u4f8b&#xff09;<br \/>\nsudo apt-get update<br \/>\nsudo apt-get install ansible<\/p>\n<p># \u4f7f\u7528pip\u5b89\u88c5<br \/>\npip install ansible<\/p>\n<p> \u5b89\u88c5\u5b8c\u6210\u540e&#xff0c;\u9700\u8981\u914d\u7f6e\u63a7\u5236\u8282\u70b9&#xff0c;\u4e3b\u8981\u662f\u7f16\u8f91  \/etc\/ansible\/hosts  \u6587\u4ef6&#xff0c;\u5b9a\u4e49\u88ab\u7ba1\u7406\u8282\u70b9\u3002\u8fd9\u91cc\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u914d\u7f6e\u793a\u4f8b&#xff1a; <\/p>\n<p># \/etc\/ansible\/hosts<br \/>\n[webservers]<br \/>\nwebserver1 ansible_host&#061;192.168.1.10<br \/>\nwebserver2 ansible_host&#061;192.168.1.11<\/p>\n<p>[dbservers]<br \/>\ndbserver1 ansible_host&#061;192.168.1.20<\/p>\n<h4> 3.1.2 \u4e0e\u88ab\u7ba1\u7406\u8282\u70b9\u7684\u901a\u4fe1\u4e0e\u7ba1\u7406 <\/h4>\n<p> \u914d\u7f6e\u597dInventory\u6587\u4ef6\u540e&#xff0c;\u63a7\u5236\u8282\u70b9\u5c31\u53ef\u4ee5\u901a\u8fc7SSH\u4e0e\u88ab\u7ba1\u7406\u8282\u70b9\u901a\u4fe1\u4e86\u3002\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d&#xff0c;\u9700\u8981\u786e\u4fddSSH\u8fde\u63a5\u65e0\u5bc6\u7801\u8ba4\u8bc1&#xff0c;\u8fd9\u901a\u5e38\u610f\u5473\u7740\u9700\u8981\u5728\u63a7\u5236\u8282\u70b9\u4e0a\u751f\u6210SSH\u5bc6\u94a5\u5e76\u5c06\u516c\u94a5\u590d\u5236\u5230\u88ab\u7ba1\u7406\u8282\u70b9\u3002 <\/p>\n<p># \u5728\u63a7\u5236\u8282\u70b9\u4e0a\u751f\u6210\u5bc6\u94a5\u5bf9<br \/>\nssh-keygen -t rsa -b 4096<\/p>\n<p># \u5c06\u516c\u94a5\u590d\u5236\u5230\u88ab\u7ba1\u7406\u8282\u70b9<br \/>\nssh-copy-id root&#064;192.168.1.10<br \/>\nssh-copy-id root&#064;192.168.1.11<br \/>\nssh-copy-id root&#064;192.168.1.20<\/p>\n<p> \u63a5\u4e0b\u6765&#xff0c;\u53ef\u4ee5\u6d4b\u8bd5\u63a7\u5236\u8282\u70b9\u548c\u88ab\u7ba1\u7406\u8282\u70b9\u4e4b\u95f4\u7684SSH\u901a\u4fe1&#xff1a; <\/p>\n<p>ansible all -m ping<\/p>\n<p> \u8fd9\u4e2a\u547d\u4ee4\u4f1a\u5411\u6240\u6709\u5728Inventory\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u4e3b\u673a\u53d1\u9001\u4e00\u4e2aping\u8bf7\u6c42&#xff0c;\u5982\u679c\u8fd4\u56de\u6210\u529f\u5219\u8868\u793a\u914d\u7f6e\u6b63\u786e\u3002 <\/p>\n<h3> 3.2 Inventory\u7684\u6784\u5efa\u4e0e\u7ba1\u7406 <\/h3>\n<h4> 3.2.1 Inventory\u6587\u4ef6\u7684\u4f5c\u7528\u4e0e\u683c\u5f0f <\/h4>\n<p> Inventory\u6587\u4ef6\u5728Ansible\u4e2d\u626e\u6f14\u7740\u91cd\u8981\u89d2\u8272&#xff0c;\u5b83\u662fAnsible\u7528\u6765\u8bc6\u522b\u3001\u5206\u7ec4\u548c\u5b9a\u4f4d\u88ab\u7ba1\u7406\u8282\u70b9\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u9ed8\u8ba4\u7684Inventory\u6587\u4ef6\u8def\u5f84\u662f  \/etc\/ansible\/hosts  &#xff0c;\u4f46\u7528\u6237\u4e5f\u53ef\u4ee5\u901a\u8fc7  -i  \u53c2\u6570\u6307\u5b9a\u4e00\u4e2a\u4e0d\u540c\u7684Inventory\u6587\u4ef6\u3002 <\/p>\n<p> Inventory\u6587\u4ef6\u53ef\u4ee5\u662f\u7b80\u5355\u7684\u9759\u6001\u6587\u4ef6&#xff0c;\u4e5f\u53ef\u4ee5\u662f\u52a8\u6001\u751f\u6210\u7684&#xff0c;\u4f8b\u5982&#xff0c;\u4f7f\u7528\u811a\u672c\u67e5\u8be2DNS\u8bb0\u5f55\u6216\u8005\u6570\u636e\u5e93\u6765\u52a8\u6001\u751f\u6210\u3002\u4e0b\u9762\u662fInventory\u6587\u4ef6\u7684\u4e00\u4e2a\u793a\u4f8b&#xff1a; <\/p>\n<p># \u793a\u4f8bInventory\u6587\u4ef6<br \/>\n[webservers]<br \/>\nwebserver1 ansible_host&#061;192.168.1.10<br \/>\nwebserver2 ansible_host&#061;192.168.1.11<\/p>\n<p>[dbservers]<br \/>\ndbserver1 ansible_host&#061;192.168.1.20<br \/>\ndbserver2 ansible_host&#061;192.168.1.21<\/p>\n<p># \u4f7f\u7528\u522b\u540d\u7b80\u5316\u5bf9\u7279\u5b9a\u4e3b\u673a\u7684\u64cd\u4f5c<br \/>\n[webservers:vars]<br \/>\nansible_user&#061;myuser<\/p>\n<p> \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d&#xff0c;\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u7ec4&#xff1a;  webservers  \u548c  dbservers  \u3002\u6bcf\u4e2a\u7ec4\u4e2d\u7684\u4e3b\u673a\u90fd\u6709\u4e00\u4e2a\u4e3b\u673a\u53d8\u91cf  ansible_host  \u7528\u4e8e\u6307\u5b9aIP\u5730\u5740\u3002\u7ec4  webservers  \u4e0b\u8fd8\u5b9a\u4e49\u4e86\u7ec4\u53d8\u91cf  ansible_user  \u6765\u6307\u5b9a\u9ed8\u8ba4\u7684SSH\u7528\u6237\u3002 <\/p>\n<h4> 3.2.2 \u52a8\u6001Inventory\u7684\u4f7f\u7528 <\/h4>\n<p> \u5bf9\u4e8e\u5927\u578b\u7684IT\u73af\u5883&#xff0c;\u9759\u6001Inventory\u6587\u4ef6\u53ef\u80fd\u53d8\u5f97\u96be\u4ee5\u7ef4\u62a4\u3002Ansible\u652f\u6301\u52a8\u6001Inventory&#xff0c;\u5141\u8bb8\u7528\u6237\u7f16\u5199\u811a\u672c\u6765\u4ece\u4e91\u670d\u52a1\u3001\u5bb9\u5668\u7f16\u6392\u7cfb\u7edf\u3001\u6570\u636e\u5e93\u7b49\u6570\u636e\u6e90\u52a8\u6001\u751f\u6210Inventory\u3002 <\/p>\n<p> \u4e00\u4e2a\u5e38\u7528\u7684\u52a8\u6001Inventory\u63d2\u4ef6\u662f  aws_ec2.py  &#xff0c;\u5b83\u53ef\u4ee5\u7528\u6765\u4eceAmazon EC2\u81ea\u52a8\u63d0\u53d6Inventory\u4fe1\u606f\u3002\u9996\u5148&#xff0c;\u9700\u8981\u5b89\u88c5AWS CLI\u5e76\u914d\u7f6e\u597d\u8bbf\u95ee\u51ed\u8bc1&#xff0c;\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4&#xff1a; <\/p>\n<p>aws ec2 describe-instances &#8211;output text &#8211;query &#039;Reservations[*].Instances[*].[Tags[?Key&#061;&#061;&#096;Name&#096;]|[0].Value, PrivateIpAddress]&#039; &gt; ec2_inventory.txt<\/p>\n<p> \u7136\u540e&#xff0c;\u9700\u8981\u5728Ansible\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u6307\u5b9a\u4f7f\u7528\u8fd9\u4e2a\u52a8\u6001Inventory\u811a\u672c&#xff1a; <\/p>\n<p># ansible.cfg<br \/>\n[defaults]<br \/>\ninventory &#061; ec2_inventory.txt<\/p>\n<p> \u73b0\u5728Ansible\u5c31\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u52a8\u6001\u751f\u6210\u7684Inventory\u6587\u4ef6\u6765\u7ba1\u7406\u548c\u90e8\u7f72EC2\u5b9e\u4f8b\u4e86\u3002 <\/p>\n<h3> 3.3 Playbook\u7684\u8bbe\u8ba1\u4e0e\u7f16\u5199 <\/h3>\n<h4> 3.3.1 Playbook\u7684YAML\u8bed\u6cd5\u57fa\u7840 <\/h4>\n<p> Playbook\u662fAnsible\u4efb\u52a1\u548c\u914d\u7f6e\u7684\u63cf\u8ff0\u6027\u6587\u4ef6&#xff0c;\u5b83\u662f\u7528YAML\u683c\u5f0f\u7f16\u5199\u7684\u3002YAML\u662f\u4e00\u79cd\u4eba\u7c7b\u53ef\u8bfb\u7684\u6570\u636e\u5e8f\u5217\u5316\u6807\u51c6\u683c\u5f0f&#xff0c;\u975e\u5e38\u9002\u5408\u7528\u4e8e\u914d\u7f6e\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Playbook\u793a\u4f8b&#xff0c;\u7528\u4e8e\u5b89\u88c5\u5e76\u914d\u7f6eApache Web\u670d\u52a1\u5668&#xff1a; <\/p>\n<p>&#8211; name: Install and start Apache<br \/>\n  hosts: webservers<br \/>\n  become: yes<br \/>\n  tasks:<br \/>\n    &#8211; name: Install apache2<br \/>\n      apt:<br \/>\n        name: apache2<br \/>\n        state: present<\/p>\n<p>    &#8211; name: Start apache2<br \/>\n      service:<br \/>\n        name: apache2<br \/>\n        state: started<br \/>\n        enabled: yes<\/p>\n<p> \u5728\u8fd9\u4e2aPlaybook\u4e2d&#xff0c;\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a\u201cInstall and start Apache\u201d\u7684\u4efb\u52a1&#xff0c;\u6307\u5b9a\u4e86\u76ee\u6807\u4e3b\u673a\u7ec4\u4e3a\u201cwebservers\u201d\u3002\u4f7f\u7528\u4e86  become: yes  \u6765\u542f\u7528\u63d0\u5347\u6743\u9650\u6267\u884c\u4efb\u52a1\u3002\u7136\u540e\u5b9a\u4e49\u4e86\u4e24\u4e2a\u4efb\u52a1&#xff1a;\u5b89\u88c5Apache\u548c\u542f\u52a8Apache\u670d\u52a1\u3002 <\/p>\n<h4> 3.3.2 Playbook\u7684\u7ed3\u6784\u548c\u6a21\u5757\u4f7f\u7528 <\/h4>\n<p> Playbook\u901a\u5e38\u5305\u542b\u4ee5\u4e0b\u51e0\u4e2a\u7ed3\u6784\u90e8\u5206&#xff1a; <\/p>\n<ul>\n<li>  name  : \u63cf\u8ff0Playbook\u7684\u4f5c\u7528\u3002 <\/li>\n<li>  hosts  : \u6307\u5b9a\u76ee\u6807\u4e3b\u673a\u6216\u4e3b\u673a\u7ec4\u3002 <\/li>\n<li>  tasks  : \u5217\u51fa\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1\u3002 <\/li>\n<li>  variables  : \u5b9a\u4e49\u53d8\u91cf&#xff0c;\u53ef\u4ee5\u5728Playbook\u4e2d\u4f7f\u7528\u3002 <\/li>\n<li>  handlers  : \u5b9a\u4e49\u4efb\u52a1\u89e6\u53d1\u65f6\u4f1a\u6267\u884c\u7684\u4e8b\u4ef6\u3002 <\/li>\n<\/ul>\n<p> \u5728\u7f16\u5199Playbook\u65f6&#xff0c;\u63a8\u8350\u4f7f\u7528Ansible\u63d0\u4f9b\u7684\u6a21\u5757\u6765\u5b8c\u6210\u4efb\u52a1&#xff0c;\u800c\u4e0d\u662f\u4f7f\u7528\u81ea\u5b9a\u4e49\u811a\u672c\u3002\u6a21\u5757\u662fAnsible\u5c01\u88c5\u597d\u7684\u529f\u80fd\u5355\u5143&#xff0c;\u5b83\u4eec\u88ab\u8bbe\u8ba1\u6210\u5e42\u7b49\u7684&#xff0c;\u8fd9\u610f\u5473\u7740\u65e0\u8bba\u8fd0\u884c\u591a\u5c11\u6b21&#xff0c;\u5b83\u4eec\u90fd\u4f1a\u5c06\u7cfb\u7edf\u914d\u7f6e\u6210\u4e00\u4e2a\u7279\u5b9a\u7684\u72b6\u6001\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684Ansible\u6a21\u5757&#xff1a; <\/p>\n<ul>\n<li>  apt  : \u7528\u4e8e\u7ba1\u7406Debian\/Ubuntu\u8f6f\u4ef6\u5305\u3002 <\/li>\n<li>  yum  : \u7528\u4e8e\u7ba1\u7406RedHat\/CentOS\u8f6f\u4ef6\u5305\u3002 <\/li>\n<li>  service  : \u7528\u4e8e\u7ba1\u7406\u7cfb\u7edf\u670d\u52a1\u3002 <\/li>\n<li>  file  : \u7528\u4e8e\u7ba1\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u5c5e\u6027\u3002 <\/li>\n<\/ul>\n<p> \u4f7f\u7528\u6a21\u5757\u53ef\u4ee5\u8ba9Playbook\u66f4\u52a0\u7b80\u6d01&#xff0c;\u6613\u4e8e\u7ef4\u62a4\u3002\u4f8b\u5982&#xff0c;\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86  apt  \u6a21\u5757\u6765\u5b89\u88c5\u8f6f\u4ef6\u5305&#xff0c;\u800c  service  \u6a21\u5757\u7528\u6765\u7ba1\u7406\u670d\u52a1\u3002\u8fd9\u4e9b\u6a21\u5757\u901a\u8fc7\u63a5\u6536\u53c2\u6570\u6765\u6267\u884c\u5177\u4f53\u7684\u64cd\u4f5c&#xff0c;\u4f7f\u5f97\u6574\u4e2aPlaybook\u66f4\u52a0\u6a21\u5757\u5316\u3002 <\/p>\n<p> \u63a5\u4e0b\u6765\u7684\u7ae0\u8282\u5c06\u66f4\u6df1\u5165\u5730\u63a2\u8ba8\u5982\u4f55\u7f16\u5199\u5177\u4f53\u7684Playbook&#xff0c;\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528Ansible\u7684\u9ad8\u7ea7\u7279\u6027\u6765\u4f18\u5316\u4efb\u52a1\u6267\u884c\u6d41\u7a0b\u3002 <\/p>\n<h2> 4. Ansible\u4e3b\u8981\u64cd\u4f5c&#xff1a;Gathering Facts\u3001Tasks\u3001Handlers\u3001Roles <\/h2>\n<h3> 4.1 Gathering Facts\u7684\u81ea\u52a8\u5316\u4fe1\u606f\u6536\u96c6 <\/h3>\n<p> \u5728Ansible\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177\u4e2d&#xff0c;Gathering Facts\u662f\u81ea\u52a8\u5316\u4fe1\u606f\u6536\u96c6\u7684\u73af\u8282\u3002Gathering Facts\u80fd\u591f\u5e2e\u52a9Ansible\u5728\u8fd0\u884cPlaybook\u4e4b\u524d&#xff0c;\u81ea\u52a8\u6536\u96c6\u88ab\u7ba1\u7406\u8282\u70b9\u4e0a\u7684\u6709\u7528\u4fe1\u606f&#xff0c;\u4f8b\u5982\u4e3b\u673a\u540d\u3001IP\u5730\u5740\u3001\u64cd\u4f5c\u7cfb\u7edf\u7c7b\u578b\u3001\u7f51\u7edc\u63a5\u53e3\u3001\u78c1\u76d8\u4fe1\u606f\u7b49\u3002\u8fd9\u4e9b\u4e8b\u5b9e\u53d8\u91cf\u5728Playbook\u4e2d\u53ef\u4ee5\u968f\u65f6\u8c03\u7528&#xff0c;\u4ee5\u5b9e\u73b0\u57fa\u4e8e\u76ee\u6807\u8282\u70b9\u4e0d\u540c\u7279\u6027\u7684\u52a8\u6001\u914d\u7f6e\u3002 <\/p>\n<h4> 4.1.1 \u4e8b\u5b9e\u53d8\u91cf\u7684\u4f7f\u7528\u4e0e\u5b9a\u5236 <\/h4>\n<p> \u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;Ansible\u4f1a\u5728\u6267\u884cPlaybook\u4e4b\u524d\u6536\u96c6\u76ee\u6807\u8282\u70b9\u7684\u4e8b\u5b9e\u4fe1\u606f&#xff0c;\u5e76\u5b58\u50a8\u5230\u53d8\u91cf\u4e2d&#xff0c;\u8fd9\u4e9b\u53d8\u91cf\u53ef\u4ee5\u662f  {{ ansible_hostname }}  &#xff0c;  {{ ansible_ip_addresses }}  \u7b49&#xff0c;\u5b83\u4eec\u5728Playbook\u4e2d\u53ef\u7528\u4e8e\u6267\u884c\u6761\u4ef6\u5224\u65ad\u6216\u751f\u6210\u52a8\u6001\u914d\u7f6e\u3002 <\/p>\n<p> \u5982\u679c\u9700\u8981\u5b9a\u5236\u6216\u6269\u5c55\u8fd9\u4e9b\u4e8b\u5b9e\u4fe1\u606f&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u81ea\u5b9a\u4e49\u7684\u63d2\u4ef6\u6216\u521b\u5efa\u81ea\u5b9a\u4e49\u7684facts\u6765\u5b9e\u73b0\u3002\u81ea\u5b9a\u4e49facts\u53ef\u4ee5\u5b58\u653e\u5728  \/etc\/ansible\/facts.d\/  \u76ee\u5f55\u4e0b&#xff0c;\u6587\u4ef6\u683c\u5f0f\u53ef\u4ee5\u662f  .fact  \u3001  .ini  \u6216  .json  \u7b49\u3002 <\/p>\n<p># \/etc\/ansible\/facts.d\/host.fact<br \/>\n[host_info]<br \/>\nhostname&#061;{{ ansible_hostname }}<br \/>\nip_address&#061;{{ ansible_all_ipv4_addresses[0] }}<\/p>\n<p> \u4e0a\u8ff0\u914d\u7f6e\u540e&#xff0c;\u81ea\u5b9a\u4e49\u7684\u4e8b\u5b9e\u53d8\u91cf  {{ host_info.hostname }}  \u548c  {{ host_info.ip_address }}  \u5c31\u53ef\u4ee5\u5728Playbook\u4e2d\u88ab\u5f15\u7528\u3002 <\/p>\n<h4> 4.1.2 \u81ea\u5b9a\u4e49facts\u7684\u65b9\u6cd5 <\/h4>\n<p> \u81ea\u5b9a\u4e49facts\u7684\u521b\u5efa\u65b9\u6cd5\u591a\u6837&#xff0c;\u4e3b\u8981\u901a\u8fc7\u7f16\u5199\u914d\u7f6e\u6587\u4ef6\u6216\u8005\u76f4\u63a5\u7f16\u5199Python\u811a\u672c\u5b9e\u73b0\u3002\u5bf9\u4e8e\u590d\u6742\u573a\u666f&#xff0c;\u63a8\u8350\u4f7f\u7528\u81ea\u5b9a\u4e49Python\u811a\u672c&#xff0c;\u56e0\u4e3a\u5176\u66f4\u7075\u6d3b\u3001\u529f\u80fd\u5f3a\u5927\u3002 <\/p>\n<p> \u5bf9\u4e8ePython\u811a\u672c&#xff0c;\u9700\u8981\u9075\u5b88\u4ee5\u4e0b\u89c4\u5219&#xff1a; <\/p>\n<li> \u547d\u540d\u5fc5\u987b\u4ee5  ansible_facts  \u4e3a\u524d\u7f00\u3002 <\/li>\n<li> \u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u662f\u5b57\u5178&#xff08;dict&#xff09;\u3002 <\/li>\n<li> \u5728\u811a\u672c\u6587\u4ef6\u9876\u90e8\u5fc5\u987b\u58f0\u660e\u7f16\u7801\u683c\u5f0f&#xff0c;\u4f8b\u5982  # -*- coding: utf-8 -*-  \u3002 <\/li>\n<p> \u4f8b\u5982&#xff0c;\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a  custom_facts.py  \u7684Python\u811a\u672c&#xff1a; <\/p>\n<p># custom_facts.py<br \/>\n# -*- coding: utf-8 -*-<\/p>\n<p>def run():<br \/>\n    custom_facts &#061; {}<br \/>\n    custom_facts[&#039;hostname&#039;] &#061; &#039;MyCustomHostname&#039;<br \/>\n    custom_facts[&#039;uptime&#039;] &#061; &#039;480 hours&#039;<br \/>\n    return custom_facts<\/p>\n<p>if __name__ &#061;&#061; &#039;__main__&#039;:<br \/>\n    print run()<\/p>\n<p> \u5c06\u6b64\u811a\u672c\u653e\u7f6e\u5728  \/etc\/ansible\/facts.d\/  \u76ee\u5f55\u4e0b&#xff0c;\u7136\u540e\u4f7f\u7528  ansible localhost -m setup  \u547d\u4ee4\u6267\u884c\u65f6&#xff0c;\u5c06\u770b\u5230\u81ea\u5b9a\u4e49\u7684\u4e8b\u5b9e\u4fe1\u606f\u3002 <\/p>\n<h3> 4.2 Tasks\u7684\u4efb\u52a1\u6267\u884c\u4e0e\u7ba1\u7406 <\/h3>\n<h4> 4.2.1 Task\u6a21\u5757\u7684\u4ecb\u7ecd\u4e0e\u5e94\u7528 <\/h4>\n<p> Task\u662fAnsible Playbook\u4e2d\u7684\u57fa\u672c\u6267\u884c\u5355\u5143&#xff0c;\u7528\u4e8e\u5b9a\u4e49\u5728\u7279\u5b9a\u4e3b\u673a\u4e0a\u8981\u6267\u884c\u7684\u5177\u4f53\u4efb\u52a1\u3002\u6bcf\u4e2aTask\u901a\u5e38\u5bf9\u5e94\u4e00\u4e2aAnsible\u6a21\u5757&#xff0c;\u4f8b\u5982  copy  \u3001  file  \u3001  yum  \u7b49&#xff0c;\u8fd9\u4e9b\u6a21\u5757\u80fd\u591f\u5b8c\u6210\u4ece\u6587\u4ef6\u4f20\u8f93\u5230\u8f6f\u4ef6\u5b89\u88c5\u7b49\u5404\u7c7b\u64cd\u4f5c\u3002 <\/p>\n<p> \u4e00\u4e2aTask\u7684\u5b9a\u4e49\u683c\u5f0f\u5982\u4e0b&#xff1a; <\/p>\n<p>&#8211; name: Task description<br \/>\n  module: module arguments<\/p>\n<p> Task\u7684\u6267\u884c\u53ef\u4ee5\u901a\u8fc7  ansible-playbook  \u547d\u4ee4\u884c\u5de5\u5177\u542f\u52a8&#xff0c;\u4e00\u4e2a\u5178\u578b\u7684Task\u4f8b\u5b50\u4e3a\u5b89\u88c5Apache&#xff1a; <\/p>\n<p>&#8211; name: Install httpd package<br \/>\n  yum:<br \/>\n    name: httpd<br \/>\n    state: present<\/p>\n<p> \u5728\u8fd9\u4e2aTask\u4e2d&#xff0c;  yum  \u6a21\u5757\u88ab\u7528\u6765\u5b89\u88c5  httpd  \u8f6f\u4ef6\u5305&#xff0c;  state  \u53c2\u6570\u6307\u5b9a\u8981\u786e\u4fdd\u5305\u5904\u4e8e\u5df2\u5b89\u88c5\u72b6\u6001\u3002 <\/p>\n<h4> 4.2.2 \u591a\u4efb\u52a1\u7684\u7ec4\u7ec7\u4e0e\u6267\u884c\u7b56\u7565 <\/h4>\n<p> \u5728Playbook\u4e2d&#xff0c;\u591a\u4e2aTask\u53ef\u4ee5\u7ec4\u7ec7\u5728\u4e00\u8d77\u5f62\u6210\u4e00\u4e2a\u5de5\u4f5c\u6d41\u7a0b\u3002\u901a\u8fc7\u5728Task\u4e4b\u95f4\u8bbe\u7f6e\u4f9d\u8d56\u5173\u7cfb&#xff0c;\u53ef\u4ee5\u4f18\u5316\u6267\u884c\u987a\u5e8f&#xff0c;\u786e\u4fdd\u4efb\u52a1\u4ee5\u6b63\u786e\u7684\u987a\u5e8f\u6267\u884c\u3002\u4f8b\u5982&#xff0c;\u5148\u521b\u5efa\u76ee\u5f55&#xff0c;\u7136\u540e\u590d\u5236\u6587\u4ef6&#xff1a; <\/p>\n<p>&#8211; name: Create web directory<br \/>\n  file:<br \/>\n    path: &#034;\/var\/www\/html&#034;<br \/>\n    state: directory<\/p>\n<p>&#8211; name: Copy index.html<br \/>\n  copy:<br \/>\n    src: index.html<br \/>\n    dest: &#034;\/var\/www\/html\/index.html&#034;<br \/>\n  notify: restart apache<\/p>\n<p>tasks:<br \/>\n&#8211; name: Restart apache<br \/>\n  service:<br \/>\n    name: httpd<br \/>\n    state: restarted<\/p>\n<p> \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d&#xff0c;  notify  \u5173\u952e\u5b57\u7528\u4e8e\u5728  copy  \u4efb\u52a1\u5b8c\u6210\u540e&#xff0c;\u89e6\u53d1\u7531  handlers  \u5b9a\u4e49\u7684  restart apache  \u4efb\u52a1\u3002 <\/p>\n<h3> 4.3 Handlers\u4e0eRoles\u7684\u9ad8\u7ea7\u64cd\u4f5c <\/h3>\n<h4> 4.3.1 Handlers\u7684\u6761\u4ef6\u89e6\u53d1\u673a\u5236 <\/h4>\n<p> Handlers\u662f\u4e00\u79cd\u7279\u6b8a\u7684Task&#xff0c;\u53ea\u5728\u88ab\u901a\u77e5\u65f6\u624d\u6267\u884c&#xff0c;\u5e76\u4e14\u5728\u540c\u4e00\u4e2aPlaybook\u7684\u6267\u884c\u4e2d&#xff0c;\u5373\u4f7f\u88ab\u591a\u6b21\u901a\u77e5\u4e5f\u53ea\u4f1a\u6267\u884c\u4e00\u6b21\u3002\u8fd9\u79cd\u673a\u5236\u975e\u5e38\u9002\u5408\u7528\u4e8e\u914d\u7f6e\u6587\u4ef6\u53d8\u52a8\u540e\u7684\u670d\u52a1\u91cd\u542f\u6216\u91cd\u65b0\u52a0\u8f7d\u3002 <\/p>\n<p> Handlers\u7684\u5b9a\u4e49\u65b9\u5f0f\u4e0e\u666e\u901aTask\u76f8\u540c&#xff0c;\u4f46\u5176\u53ea\u6709\u5728\u88ab  notify  \u6307\u4ee4\u8c03\u7528\u65f6\u624d\u4f1a\u6267\u884c\u3002\u5982\u679c\u591a\u4e2a\u4efb\u52a1\u89e6\u53d1\u4e86\u540c\u4e00\u4e2aHandler&#xff0c;Handler\u53ea\u4f1a\u6267\u884c\u4e00\u6b21\u3002 <\/p>\n<p>handlers:<br \/>\n  &#8211; name: restart apache<br \/>\n    service:<br \/>\n      name: httpd<br \/>\n      state: restarted<\/p>\n<p> \u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d&#xff0c;  notify: restart apache  \u5c06\u4f1a\u5728\u76f8\u5e94\u7684Task\u6267\u884c\u5b8c\u6bd5\u540e&#xff0c;\u89e6\u53d1Handler\u4efb\u52a1\u3002 <\/p>\n<h4> 4.3.2 Roles\u7684\u76ee\u5f55\u7ed3\u6784\u4e0e\u4f5c\u7528 <\/h4>\n<p> Roles\u662fAnsible\u4e2d\u7ec4\u7ec7Playbook\u7684\u4e00\u79cd\u65b9\u5f0f&#xff0c;\u5b83\u901a\u8fc7\u6807\u51c6\u5316\u7684\u76ee\u5f55\u7ed3\u6784\u6765\u5b58\u653e\u76f8\u5173\u7684\u6587\u4ef6\u548c\u4fe1\u606f&#xff0c;\u4f7f\u5f97Playbook\u66f4\u6613\u4e8e\u7ba1\u7406\u548c\u91cd\u7528\u3002 <\/p>\n<p> \u4e00\u4e2aRole\u7684\u76ee\u5f55\u7ed3\u6784\u901a\u5e38\u5982\u4e0b&#xff1a; <\/p>\n<p>\/roles\/<br \/>\n    \/rolename\/<br \/>\n        \/tasks\/<br \/>\n        \/handlers\/<br \/>\n        \/files\/<br \/>\n        \/templates\/<br \/>\n        \/vars\/<br \/>\n        \/meta\/<\/p>\n<ul>\n<li>  tasks  \u76ee\u5f55\u5b58\u653eRole\u7684\u4e3b\u8981\u4efb\u52a1\u3002 <\/li>\n<li>  handlers  \u76ee\u5f55\u5b58\u653e\u8be5Role\u4e13\u7528\u7684Handlers\u3002 <\/li>\n<li>  files  \u76ee\u5f55\u5b58\u653e\u9759\u6001\u6587\u4ef6&#xff0c;\u5982\u914d\u7f6e\u6587\u4ef6\u6216\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002 <\/li>\n<li>  templates  \u76ee\u5f55\u5b58\u653eJinja2\u6a21\u677f\u6587\u4ef6\u3002 <\/li>\n<li>  vars  \u76ee\u5f55\u5b58\u653e\u53d8\u91cf\u6587\u4ef6\u3002 <\/li>\n<li>  meta  \u76ee\u5f55\u5b58\u653eRole\u7684\u5143\u4fe1\u606f&#xff0c;\u5982\u4f9d\u8d56\u5173\u7cfb\u3002 <\/li>\n<\/ul>\n<p> \u4f8b\u5982&#xff0c;\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a  myrole  \u7684Role&#xff0c;\u5176\u76ee\u5f55\u7ed3\u6784\u548c\u5185\u5bb9\u793a\u4f8b\u5982\u4e0b&#xff1a; <\/p>\n<p>\/roles\/<br \/>\n    \/myrole\/<br \/>\n        \/tasks\/<br \/>\n            main.yml<br \/>\n        \/handlers\/<br \/>\n            main.yml<br \/>\n        \/files\/<br \/>\n            myconfig.conf<br \/>\n        \/templates\/<br \/>\n            template.j2<\/p>\n<p> \u5728  tasks\/main.yml  \u4e2d\u5b9a\u4e49Role\u7684\u4e3b\u8981\u4efb\u52a1&#xff0c;\u800c\u5728  handlers\/main.yml  \u4e2d\u5b9a\u4e49Handlers\u3002\u901a\u8fc7\u8fd9\u6837\u7684\u7ec4\u7ec7\u65b9\u5f0f&#xff0c;Playbook\u53ef\u4ee5\u6309\u903b\u8f91\u7ec4\u5408\u4e0d\u540c\u7684Role&#xff0c;\u4ece\u800c\u8fbe\u5230\u9ad8\u6548\u548c\u53ef\u7ef4\u62a4\u7684\u76ee\u7684\u3002 <\/p>\n<p> \u6839\u636e\u4e0a\u8ff0\u5185\u5bb9&#xff0c;\u53ef\u4ee5\u770b\u51faAnsible\u4e2dGathering Facts\u3001Tasks\u3001Handlers\u548cRoles\u7684\u64cd\u4f5c\u662f\u5b9e\u73b0\u7cfb\u7edf\u81ea\u52a8\u5316\u7ba1\u7406\u548c\u7ef4\u62a4\u7684\u5173\u952e\u90e8\u5206\u3002\u901a\u8fc7\u5408\u7406\u8bbe\u8ba1\u548c\u4f7f\u7528\u8fd9\u4e9b\u7ec4\u4ef6&#xff0c;\u53ef\u4ee5\u6784\u5efa\u51fa\u5f3a\u5927\u4e14\u9ad8\u6548\u7684\u81ea\u52a8\u5316\u6d41\u7a0b\u3002 <\/p>\n<h2> 5. \u66f4\u65b0\u670d\u52a1\u5668\u6d41\u7a0b\u548cansible-playbook\u4f7f\u7528 <\/h2>\n<p> \u66f4\u65b0\u670d\u52a1\u5668\u662f\u7ef4\u62a4\u7cfb\u7edf\u5b89\u5168\u4e0e\u7a33\u5b9a\u7684\u91cd\u8981\u624b\u6bb5\u3002\u5b83\u4e0d\u4ec5\u6d89\u53ca\u7cfb\u7edf\u6587\u4ef6\u7684\u540c\u6b65&#xff0c;\u8fd8\u53ef\u80fd\u5305\u62ec\u5e94\u7528\u7a0b\u5e8f\u548c\u4f9d\u8d56\u5305\u7684\u5347\u7ea7\u3002\u56e0\u6b64&#xff0c;\u66f4\u65b0\u670d\u52a1\u5668\u7684\u6d41\u7a0b\u9700\u8981\u8c28\u614e\u89c4\u5212\u5e76\u81ea\u52a8\u5316\u6267\u884c&#xff0c;\u4ee5\u51cf\u5c11\u624b\u52a8\u64cd\u4f5c\u7684\u98ce\u9669\u548c\u65f6\u95f4\u6d88\u8017\u3002Ansible\u4f5c\u4e3a\u4e00\u6b3e\u5f3a\u5927\u7684\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177&#xff0c;\u5176playbook\u529f\u80fd\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u66f4\u65b0\u670d\u52a1\u5668\u7684\u590d\u6742\u6027\u3002 <\/p>\n<h3> 5.1 \u66f4\u65b0\u670d\u52a1\u5668\u7684\u7b56\u7565\u4e0e\u89c4\u5212 <\/h3>\n<p> \u5728\u8fdb\u884c\u670d\u52a1\u5668\u66f4\u65b0\u4e4b\u524d&#xff0c;\u9996\u5148\u9700\u8981\u5bf9\u66f4\u65b0\u76ee\u6807\u548c\u9700\u6c42\u8fdb\u884c\u8be6\u7ec6\u5206\u6790&#xff0c;\u7136\u540e\u5236\u5b9a\u51fa\u8be6\u7ec6\u7684\u66f4\u65b0\u6d41\u7a0b\u6b65\u9aa4\u3002\u8fd9\u4e00\u8fc7\u7a0b\u662f\u66f4\u65b0\u670d\u52a1\u5668\u7684\u57fa\u7840&#xff0c;\u5b83\u786e\u4fdd\u66f4\u65b0\u540e\u7684\u670d\u52a1\u5668\u80fd\u591f\u6ee1\u8db3\u4e1a\u52a1\u9700\u6c42\u548c\u7cfb\u7edf\u7a33\u5b9a\u6027\u7684\u8981\u6c42\u3002 <\/p>\n<h4> 5.1.1 \u7cfb\u7edf\u66f4\u65b0\u7684\u76ee\u6807\u4e0e\u9700\u6c42\u5206\u6790 <\/h4>\n<p> \u66f4\u65b0\u670d\u52a1\u5668\u524d\u9700\u8981\u660e\u786e\u76ee\u6807&#xff0c;\u4f8b\u5982\u66f4\u65b0\u7684\u64cd\u4f5c\u7cfb\u7edf\u3001\u5e94\u7528\u7a0b\u5e8f&#xff0c;\u6216\u8005\u7279\u5b9a\u7684\u5e93\u6587\u4ef6\u7b49\u3002\u540c\u65f6\u9700\u8981\u6536\u96c6\u9700\u6c42&#xff0c;\u6bd4\u5982\u66f4\u65b0\u540e\u9700\u8981\u652f\u6301\u7684\u65b0\u7279\u6027\u3001\u5b89\u5168\u6027\u8981\u6c42\u3001\u517c\u5bb9\u6027\u6d4b\u8bd5\u7b49\u3002 <\/p>\n<p> \u9700\u6c42\u5206\u6790\u5b8c\u6210\u540e&#xff0c;\u9700\u8981\u5236\u5b9a\u66f4\u65b0\u7b56\u7565\u3002\u7b56\u7565\u53ef\u80fd\u5305\u62ec&#xff1a; <\/p>\n<ul>\n<li>  \u56de\u6eda\u8ba1\u5212  &#xff1a;\u786e\u4fdd\u5728\u66f4\u65b0\u51fa\u73b0\u95ee\u9898\u65f6\u80fd\u591f\u8fc5\u901f\u6062\u590d\u5230\u66f4\u65b0\u524d\u7684\u72b6\u6001\u3002 <\/li>\n<li>  \u6d4b\u8bd5\u73af\u5883\u7684\u66f4\u65b0  &#xff1a;\u5728\u6b63\u5f0f\u66f4\u65b0\u4e4b\u524d&#xff0c;\u5728\u6d4b\u8bd5\u73af\u5883\u4e2d\u6a21\u62df\u66f4\u65b0\u6d41\u7a0b&#xff0c;\u9a8c\u8bc1\u66f4\u65b0\u662f\u5426\u7a33\u5b9a\u53ef\u884c\u3002 <\/li>\n<li>  \u65f6\u95f4\u7a97\u53e3\u9009\u62e9  &#xff1a;\u4e3a\u4e86\u51cf\u5c11\u5bf9\u4e1a\u52a1\u7684\u5f71\u54cd&#xff0c;\u66f4\u65b0\u64cd\u4f5c\u5e94\u9009\u62e9\u5728\u4e1a\u52a1\u8d1f\u8f7d\u4f4e\u8c37\u671f\u6267\u884c\u3002 <\/li>\n<\/ul>\n<h4> 5.1.2 \u66f4\u65b0\u6d41\u7a0b\u7684\u6b65\u9aa4\u89c4\u5212 <\/h4>\n<p> \u89c4\u5212\u66f4\u65b0\u6d41\u7a0b\u65f6&#xff0c;\u9700\u8981\u8003\u8651\u4ee5\u4e0b\u6b65\u9aa4&#xff1a; <\/p>\n<li> \u66f4\u65b0\u524d\u7684\u51c6\u5907\u5de5\u4f5c&#xff0c;\u5982\u5907\u4efd\u6570\u636e\u5e93\u548c\u5173\u952e\u6587\u4ef6\u3002 <\/li>\n<li> \u66f4\u65b0\u8fc7\u7a0b\u4e2d\u7684\u8be6\u7ec6\u6b65\u9aa4&#xff0c;\u5305\u62ec\u5177\u4f53\u7684\u5b89\u88c5\u547d\u4ee4\u548c\u914d\u7f6e\u6587\u4ef6\u7684\u66f4\u6539\u3002 <\/li>\n<li> \u66f4\u65b0\u540e\u7684\u9a8c\u8bc1&#xff0c;\u786e\u4fdd\u6240\u6709\u66f4\u65b0\u7684\u7ec4\u4ef6\u90fd\u8fd0\u884c\u6b63\u5e38&#xff0c;\u6ca1\u6709\u4efb\u4f55\u670d\u52a1\u4e2d\u65ad\u3002 <\/li>\n<li> \u6587\u6863\u8bb0\u5f55&#xff0c;\u5c06\u66f4\u65b0\u8fc7\u7a0b\u4e2d\u6240\u505a\u7684\u66f4\u6539\u8be6\u7ec6\u8bb0\u5f55\u4e0b\u6765&#xff0c;\u65b9\u4fbf\u672a\u6765\u67e5\u9605\u548c\u56de\u6eda\u3002 <\/li>\n<h3> 5.2 ansible-playbook\u7684\u7f16\u5199\u4e0e\u6267\u884c <\/h3>\n<p> \u4e00\u65e6\u66f4\u65b0\u7b56\u7565\u548c\u6b65\u9aa4\u89c4\u5212\u5b8c\u6210&#xff0c;\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199ansible-playbook\u6765\u81ea\u52a8\u5316\u66f4\u65b0\u670d\u52a1\u5668\u7684\u8fc7\u7a0b\u3002 <\/p>\n<h4> 5.2.1 \u7f16\u5199\u66f4\u65b0\u7528\u7684Playbook <\/h4>\n<p> Playbook\u662f\u7528YAML\u8bed\u8a00\u7f16\u5199\u7684\u811a\u672c&#xff0c;\u5b83\u63cf\u8ff0\u4e86\u81ea\u52a8\u5316\u4efb\u52a1\u7684\u6267\u884c\u987a\u5e8f\u548c\u903b\u8f91\u3002\u4e00\u4e2a\u5178\u578b\u7684\u66f4\u65b0\u670d\u52a1\u5668\u7684Playbook\u53ef\u80fd\u5305\u542b\u4ee5\u4e0b\u7ed3\u6784&#xff1a; <\/p>\n<p>&#8211; name: \u66f4\u65b0\u670d\u52a1\u5668<br \/>\n  hosts: all<br \/>\n  become: yes<br \/>\n  tasks:<br \/>\n    &#8211; name: \u5b89\u88c5aptitude&#xff08;\u7528\u4e8e\u7ba1\u7406Debian\u7cfb\u5217\u7cfb\u7edf\u5305&#xff09;<br \/>\n      apt:<br \/>\n        name: aptitude<br \/>\n        state: present<br \/>\n    &#8211; name: \u66f4\u65b0\u7cfb\u7edf\u8f6f\u4ef6\u5305<br \/>\n      apt:<br \/>\n        upgrade: dist<br \/>\n        update_cache: yes<br \/>\n    &#8211; name: \u786e\u8ba4\u7cfb\u7edf\u66f4\u65b0\u72b6\u6001<br \/>\n      shell: dpkg &#8211;get-selections | grep hold<br \/>\n      register: output<br \/>\n      failed_when: output is not failed<br \/>\n      changed_when: output is changed<\/p>\n<p> \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d&#xff0c;Playbook\u5305\u542b\u4e86\u4e09\u4e2a\u4efb\u52a1&#xff1a;\u5b89\u88c5  aptitude  \u3001\u66f4\u65b0\u6240\u6709\u8f6f\u4ef6\u5305\u4ee5\u53ca\u786e\u8ba4\u7cfb\u7edf\u66f4\u65b0\u72b6\u6001\u3002 <\/p>\n<h4> 5.2.2 \u6267\u884cPlaybook\u8fdb\u884c\u7cfb\u7edf\u66f4\u65b0 <\/h4>\n<p> \u7f16\u5199\u5b8cPlaybook\u540e&#xff0c;\u5c31\u53ef\u4ee5\u901a\u8fc7  ansible-playbook  \u547d\u4ee4\u6765\u6267\u884c\u66f4\u65b0\u64cd\u4f5c\u3002\u6267\u884c\u547d\u4ee4\u901a\u5e38\u5728\u63a7\u5236\u8282\u70b9\u4e0a\u8fd0\u884c&#xff0c;\u5982\u4e0b\u6240\u793a&#xff1a; <\/p>\n<p>ansible-playbook -i inventory_file \/path\/to\/your playbook.yml<\/p>\n<p> \u5176\u4e2d&#xff0c;  inventory_file  \u662f\u670d\u52a1\u5668\u7684\u6e05\u5355\u6587\u4ef6&#xff0c;  playbook.yml  \u662f\u524d\u9762\u521b\u5efa\u7684\u66f4\u65b0\u811a\u672c\u6587\u4ef6\u3002\u6267\u884c\u8be5\u547d\u4ee4\u540e&#xff0c;Ansible\u4f1a\u81ea\u52a8\u5728\u6240\u6709\u6307\u5b9a\u7684\u670d\u52a1\u5668\u4e0a\u6267\u884cPlaybook\u4e2d\u7684\u4efb\u52a1\u3002 <\/p>\n<p> \u901a\u8fc7\u8fd9\u79cd\u81ea\u52a8\u5316\u65b9\u5f0f&#xff0c;\u53ef\u4ee5\u5728\u4fdd\u6301\u9ad8\u53ef\u9760\u6027\u548c\u9ad8\u6548\u6027\u7684\u540c\u65f6\u5b8c\u6210\u670d\u52a1\u5668\u66f4\u65b0\u4efb\u52a1\u3002\u6b64\u5916&#xff0c;Ansible\u7684\u5e42\u7b49\u6027\u4fdd\u8bc1\u4e86\u5982\u679cPlaybook\u5df2\u7ecf\u6267\u884c\u4e14\u7cfb\u7edf\u5904\u4e8e\u671f\u671b\u72b6\u6001&#xff0c;\u518d\u6b21\u6267\u884c\u76f8\u540c\u7684Playbook\u4e0d\u4f1a\u5bfc\u81f4\u7cfb\u7edf\u72b6\u6001\u4e0d\u4e00\u81f4\u3002 <\/p>\n<p> \u8bf7\u6ce8\u610f&#xff0c;\u5b9e\u9645\u64cd\u4f5c\u4e2d&#xff0c;\u670d\u52a1\u5668\u66f4\u65b0\u5e94\u9075\u5faa\u5177\u4f53\u7684\u4e1a\u52a1\u9700\u6c42\u548c\u5b89\u5168\u7b56\u7565&#xff0c;\u5e76\u5728\u6d4b\u8bd5\u73af\u5883\u4e2d\u5145\u5206\u9a8c\u8bc1\u540e\u518d\u5e94\u7528\u5230\u751f\u4ea7\u73af\u5883\u4e2d\u3002 <\/p>\n<p>  \u672c\u6587\u8fd8\u6709\u914d\u5957\u7684\u7cbe\u54c1\u8d44\u6e90&#xff0c;\u70b9\u51fb\u83b7\u53d6  <img decoding=\"async\" alt=\"menu-r.4af5f7ec.gif\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729191225-68891d190640f.gif\" style=\"width:16px;margin-left:4px;vertical-align:text-bottom;cursor:text\" \/> <\/p>\n<p> \u7b80\u4ecb&#xff1a;Ansible\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u81ea\u52a8\u5316\u5de5\u5177&#xff0c;\u7528\u4e8e\u914d\u7f6e\u7ba1\u7406\u3001\u5e94\u7528\u90e8\u7f72\u4ee5\u53ca\u4efb\u52a1\u6267\u884c\u3002\u57fa\u4e8eAgentless\u67b6\u6784&#xff0c;Ansible\u5229\u7528SSH\u534f\u8bae\u8fdc\u7a0b\u7ba1\u7406\u670d\u52a1\u5668&#xff0c;\u7b80\u5316\u4e86\u7cfb\u7edf\u7ba1\u7406\u5458\u5de5\u4f5c\u3002\u672c\u6587\u6df1\u5165\u63a2\u8ba8\u4e86Ansible\u7684\u7ec4\u6210\u90e8\u5206\u548c\u4e3b\u8981\u64cd\u4f5c&#xff0c;\u5305\u62ec\u63a7\u5236\u8282\u70b9\u3001\u88ab\u7ba1\u7406\u8282\u70b9\u3001Inventory\u3001Playbook\u3001Gathering Facts\u3001Tasks\u3001Handlers\u548cRoles&#xff0c;\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528Ansible\u6267\u884c\u670d\u52a1\u5668\u66f4\u65b0\u3002\u7279\u522b\u5f3a\u8c03\u4e86\u4f7f\u7528ansible-playbook\u6765\u6267\u884c\u5305\u542b\u66f4\u65b0\u6307\u4ee4\u7684\u811a\u672c\u548cansible\u6a21\u5757&#xff0c;\u4ee5\u53ca\u5982\u4f55\u6839\u636e\u73af\u5883\u914d\u7f6eInventory\u6765\u8fd0\u884c\u66f4\u65b0playbook\u3002\u201dansible-update_servers\u201d\u9879\u76ee\u5c55\u793a\u4e86\u5982\u4f55\u81ea\u52a8\u5316\u670d\u52a1\u5668\u66f4\u65b0\u6d41\u7a0b&#xff0c;\u5c55\u793a\u4e86Ansible\u5728IT\u57fa\u7840\u8bbe\u65bd\u81ea\u52a8\u5316\u4e2d\u7684\u5e94\u7528\u3002 <\/p>\n<p>   \u672c\u6587\u8fd8\u6709\u914d\u5957\u7684\u7cbe\u54c1\u8d44\u6e90&#xff0c;\u70b9\u51fb\u83b7\u53d6   <img decoding=\"async\" alt=\"menu-r.4af5f7ec.gif\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729191225-68891d190640f.gif\" style=\"width:16px;margin-left:4px;vertical-align:text-bottom;cursor:text\" \/>  <\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb969\u6b21\uff0c\u70b9\u8d5e9\u6b21\uff0c\u6536\u85cf17\u6b21\u3002\u81ea\u52a8\u5316\u662f\u73b0\u4ee3IT\u8fd0\u7ef4\u7684\u6807\u914d\u4e4b\u4e00\uff0c\u800cAnsible\u5c31\u662f\u8be5\u9886\u57df\u4e2d\u7684\u4e00\u9897\u8000\u773c\u660e\u661f\u3002\u5b83\u57fa\u4e8ePython\u5f00\u53d1\uff0c\u80fd\u591f\u5b9e\u73b0IaC\uff08Infrastructure as Code\uff09\uff0c\u901a\u8fc7\u7b80\u5355\u7684\u914d\u7f6e\u5373\u53ef\u5b9e\u73b0\u590d\u6742\u7684\u7cfb\u7edf\u7ba1\u7406\u4efb\u52a1\u3002Ansible\u4e0d\u9700\u8981\u5728\u76ee\u6807\u4e3b\u673a\u4e0a\u5b89\u88c5\u4ee3\u7406\uff0c\u5b83\u5229\u7528SSH\u534f\u8bae\u8fdb\u884c\u901a\u4fe1\uff0c\u7ed3\u5408YAML\u8bed\u8a00\u7f16\u5199\u7684Playbook\uff0c\u53ef\u4ee5\u8f7b\u677e\u90e8\u7f72\u5e94\u7528\u3001\u914d\u7f6e\u7cfb\u7edf\u3001\u6267\u884c\u4efb\u52a1\u3002<\/p>\n","protected":false},"author":2,"featured_media":46452,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"topic":[],"class_list":["post-46454","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Ansible\u81ea\u52a8\u5316\u670d\u52a1\u5668\u66f4\u65b0\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/46454.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ansible\u81ea\u52a8\u5316\u670d\u52a1\u5668\u66f4\u65b0\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb969\u6b21\uff0c\u70b9\u8d5e9\u6b21\uff0c\u6536\u85cf17\u6b21\u3002\u81ea\u52a8\u5316\u662f\u73b0\u4ee3IT\u8fd0\u7ef4\u7684\u6807\u914d\u4e4b\u4e00\uff0c\u800cAnsible\u5c31\u662f\u8be5\u9886\u57df\u4e2d\u7684\u4e00\u9897\u8000\u773c\u660e\u661f\u3002\u5b83\u57fa\u4e8ePython\u5f00\u53d1\uff0c\u80fd\u591f\u5b9e\u73b0IaC\uff08Infrastructure as Code\uff09\uff0c\u901a\u8fc7\u7b80\u5355\u7684\u914d\u7f6e\u5373\u53ef\u5b9e\u73b0\u590d\u6742\u7684\u7cfb\u7edf\u7ba1\u7406\u4efb\u52a1\u3002Ansible\u4e0d\u9700\u8981\u5728\u76ee\u6807\u4e3b\u673a\u4e0a\u5b89\u88c5\u4ee3\u7406\uff0c\u5b83\u5229\u7528SSH\u534f\u8bae\u8fdb\u884c\u901a\u4fe1\uff0c\u7ed3\u5408YAML\u8bed\u8a00\u7f16\u5199\u7684Playbook\uff0c\u53ef\u4ee5\u8f7b\u677e\u90e8\u7f72\u5e94\u7528\u3001\u914d\u7f6e\u7cfb\u7edf\u3001\u6267\u884c\u4efb\u52a1\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/46454.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-29T19:12:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729191225-68891d190640f.gif\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/46454.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/46454.html\",\"name\":\"Ansible\u81ea\u52a8\u5316\u670d\u52a1\u5668\u66f4\u65b0\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-29T19:12:26+00:00\",\"dateModified\":\"2025-07-29T19:12:26+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/46454.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/46454.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/46454.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ansible\u81ea\u52a8\u5316\u670d\u52a1\u5668\u66f4\u65b0\u6307\u5357\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Ansible\u81ea\u52a8\u5316\u670d\u52a1\u5668\u66f4\u65b0\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/46454.html","og_locale":"zh_CN","og_type":"article","og_title":"Ansible\u81ea\u52a8\u5316\u670d\u52a1\u5668\u66f4\u65b0\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb969\u6b21\uff0c\u70b9\u8d5e9\u6b21\uff0c\u6536\u85cf17\u6b21\u3002\u81ea\u52a8\u5316\u662f\u73b0\u4ee3IT\u8fd0\u7ef4\u7684\u6807\u914d\u4e4b\u4e00\uff0c\u800cAnsible\u5c31\u662f\u8be5\u9886\u57df\u4e2d\u7684\u4e00\u9897\u8000\u773c\u660e\u661f\u3002\u5b83\u57fa\u4e8ePython\u5f00\u53d1\uff0c\u80fd\u591f\u5b9e\u73b0IaC\uff08Infrastructure as Code\uff09\uff0c\u901a\u8fc7\u7b80\u5355\u7684\u914d\u7f6e\u5373\u53ef\u5b9e\u73b0\u590d\u6742\u7684\u7cfb\u7edf\u7ba1\u7406\u4efb\u52a1\u3002Ansible\u4e0d\u9700\u8981\u5728\u76ee\u6807\u4e3b\u673a\u4e0a\u5b89\u88c5\u4ee3\u7406\uff0c\u5b83\u5229\u7528SSH\u534f\u8bae\u8fdb\u884c\u901a\u4fe1\uff0c\u7ed3\u5408YAML\u8bed\u8a00\u7f16\u5199\u7684Playbook\uff0c\u53ef\u4ee5\u8f7b\u677e\u90e8\u7f72\u5e94\u7528\u3001\u914d\u7f6e\u7cfb\u7edf\u3001\u6267\u884c\u4efb\u52a1\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/46454.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-29T19:12:26+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250729191225-68891d190640f.gif"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/46454.html","url":"https:\/\/www.wsisp.com\/helps\/46454.html","name":"Ansible\u81ea\u52a8\u5316\u670d\u52a1\u5668\u66f4\u65b0\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-29T19:12:26+00:00","dateModified":"2025-07-29T19:12:26+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/46454.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/46454.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/46454.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Ansible\u81ea\u52a8\u5316\u670d\u52a1\u5668\u66f4\u65b0\u6307\u5357"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/46454","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=46454"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/46454\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/46452"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=46454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=46454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=46454"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=46454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}