{"id":915,"date":"2022-07-13T17:43:42","date_gmt":"2022-07-13T09:43:42","guid":{"rendered":"http:\/\/www.rchen.top\/?p=915"},"modified":"2026-07-09T21:30:08","modified_gmt":"2026-07-09T13:30:08","slug":"ansible%e5%b7%a5%e5%85%b7%e9%83%a8%e7%bd%b2%e3%80%81%e9%85%8d%e7%bd%ae%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.rchen.top\/?p=915","title":{"rendered":"Ansible\u5de5\u5177\u90e8\u7f72\u4e0e\u914d\u7f6e\u4ecb\u7ecd"},"content":{"rendered":"<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\"><span style=\"font-weight: bold;\">1\u3001Ansible \u662f\u4ec0\u4e48\uff1f<\/span><\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\"><span lang=\"zh-CN\">Ansible\u662f\u4e00\u6b3e \u57fa\u4e8ePython\u5f00\u53d1\u7684\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177\uff0c\u96c6\u5408\u4e86\u4f17\u591a\u8fd0\u7ef4\u5de5\u5177\uff08puppet\u3001chef\u3001func\u3001fabric\uff09\u7684\u4f18\u70b9\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6279\u91cf\u7684\u7cfb\u7edf\u914d\u7f6e\u3001\u6279\u91cf\u7684\u7a0b\u5e8f\u90e8\u7f72\u3001\u6279\u91cf\u7684\u8fd0\u884c\u547d\u4ee4\u7b49\u529f\u80fd\uff0c\u7ed9\u96c6\u7fa4\u7ba1\u7406 \u5927\u5927\u51cf\u8f7b\u4e86\u5de5\u4f5c\u4eba\u5458\u7684\u5de5\u4f5c\u91cf\u63d0\u5347\u4e86\u6548\u7387\u3002<\/span><span lang=\"x-none\">Ansible \u5e95\u5c42\u57fa\u4e8e Pytho\u4ee5\u7b80\u5355\u8457\u79f0\uff0c\u914d\u7f6e\u6587\u4ef6\u7684\u683c\u5f0f\u4e5f\u4ee5 INI \u548c YAML \u4e3a\u4e3b\uff0c\u4e0e\u5176\u4ed6\u7684\u7ba1\u7406\u5de5\u5177\u76f8\u6bd4\uff0c\u5b66\u4e60\u6210\u672c\u8f83\u4f4e\uff0c\u5b66\u4e60\u66f2\u7ebf\u4e5f\u5f88\u5e73\u6ed1\uff0c\u65e0\u8bba\u662f\u57fa\u7840\u8fd0\u7ef4\u4eba\u5458\u8fd8\u662f\u8d44\u6df1\u8fd0\u7ef4\u5de5\u7a0b\u5e08\u90fd\u53ef\u4ee5\u8f83\u5feb\u7684\u4e0a\u624b\uff0c\u7a0d\u52a0\u7ec3\u4e60\u4fbf\u53ef\u4ee5\u719f\u7ec3\u638c\u63e1\u3002\u5982\u679c\u5177\u5907 Dev \u57fa\u7840\uff0c\u719f\u6089 Python \u4ee5\u53ca PHP \u7b49\u4e3b\u6d41\u7684\u8bed\u8a00\uff0c\u57fa\u4e8e Ansible \u5f00\u653e APL \u63a5\u53e3\u505a\u4e8c\u6b21\u5f00\u53d1\uff0c\u53ef\u4ee5\u7075\u6d3b\u6709\u6548\u7684\u53d1\u6325\u5176\u4ef7\u503c\uff0cAnsible \u81ea\u8eab\u4e5f\u5305\u62ec\u975e\u5e38\u4e30\u5bcc\u7684\u5185\u7f6e\u6a21\u5757\uff0c\u4ece windows \u7cfb\u7edf\u5230\u5f00\u6e90 Linux \u7cfb\u7edf\uff0c\u4ece\u6587\u4ef6\u540c\u6b65\u5230\u547d\u4ee4\u6267\u884c\uff0c\u4ece\u8f6f\u4ef6\u7684\u5b89\u5168\u5347\u7ea7\u5230\u914d\u7f6e\u7684\u7ef4\u62a4\u53d8\u66f4\uff0c\u7b49\u7b49\u3002<\/span><\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">Ansible\u662f\u57fa\u4e8e paramiko \u5f00\u53d1\u7684\uff0c\u5e76\u4e14\u57fa\u4e8e\u6a21\u5757\u5316\u5de5\u4f5c\uff0c\u672c\u8eab\u6ca1\u6709\u6279\u91cf\u90e8\u7f72\u7684\u80fd\u529b\u3002<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">\u771f\u6b63\u5177\u6709\u6279\u91cf\u90e8\u7f72\u7684\u662fAnsible\u6240\u8fd0\u884c\u7684\u6a21\u5757\uff0cAnsible\u53ea\u662f\u63d0\u4f9b\u4e00\u79cd\u6846\u67b6\u3002<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">Ansible\u4e0d\u9700\u8981\u5728\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u5b89\u88c5client\/agents\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u57fa\u4e8essh\u6765\u548c\u8fdc\u7a0b\u4e3b\u673a\u901a\u8baf\u7684\u3002<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">Ansible\u76ee\u524d\u5df2\u7ecf\u5df2\u7ecf\u88ab\u7ea2\u5e3d\u5b98\u65b9\u6536\u8d2d\uff0c\u662f\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177\u4e2d\u5927\u5bb6\u8ba4\u53ef\u5ea6\u6700\u9ad8\u7684\uff0c\u5e76\u4e14\u4e0a\u624b\u5bb9\u6613\uff0c\u5b66\u4e60\u7b80\u5355\u3002<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\"><span style=\"font-weight: bold;\">2\u3001Ansible \u662f\u5982\u4f55\u5de5\u4f5c\u7684<\/span><\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">Ansible \u6ca1\u6709\u5ba2\u6237\u7aef\uff0c\u56e0\u6b64\u5e95\u5c42\u901a\u4fe1\u4f9d\u8d56\u4e8e\u7cfb\u7edf\u8f6f\u4ef6\uff0cLinux \u7cfb\u7edf\u4e0b\u57fa\u4e8e OpenSSH \u901a\u4fe1 Windows \u7cfb\u7edf\u4e0b\u57fa\u4e8e PowerShell\uff0c\u7ba1\u7406\u7aef\u5fc5\u987b\u662f Linux \u7cfb\u7edf\uff0c\u4f7f\u7528\u8005\u8ba4\u8bc1\u901a\u8fc7\u540e\u518d\u7ba1\u7406\u8282\u70b9\u901a\u8fc7 Ansible \u5de5\u5177\u8c03\u7528\u5404\u5e94\u7528\u6a21\u5757\uff0c\u5c06\u6307\u4ee4\u63a8\u9001\u81f3\u88ab\u7ba1\u7406\u7aef\u6267\u884c\uff0c\u5e76\u5728\u6267\u884c\u5b8c\u6bd5\u540e\u81ea\u52a8\u5220\u9664\u4ea7\u751f\u7684\u4e34\u65f6\u6587\u4ef6\u3002Ansible \u5177\u4f53\u7684\u5de5\u4f5c\u673a\u5236\u5b98\u65b9\u6709\u4e13\u680f\u4ecb\u7ecd\uff1ahttps:\/\/www.ansible.com\/how-ansible-works<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\"><span style=\"font-weight: bold;\">3\u3001 Ansible \u7684\u5e94\u7528\u573a\u666f<\/span><\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">\u4ece\u8fd0\u7ef4\u64cd\u4f5c\u89d2\u5ea6\u5206\u4e3a\u4e24\u7c7b\uff1a<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">\u6587\u4ef6\u4f20\u8f93\uff1a\u6587\u4ef6\u7684\u672c\u5730\u4f20\u8f93\u548c\u5f02\u5730\u4f20\u8f93\uff0c\u6240\u6709\u6587\u4ef6\u7684\u7a7a\u95f4\u5f62\u6001\uff0c\u65f6\u95f4\u5f62\u6001\u53d8\u5316\u6784\u6210\u4e86\u6587\u4ef6\u4f20\u8f93\u7c7b\u7684\u64cd\u4f5c\u3002<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">\u547d\u4ee4\u6267\u884c\uff1a\u7ec8\u7aef\u6240\u6709\u7684\u64cd\u4f5c\u5bf9\u7cfb\u7edf\u6765\u8bb2\u90fd\u662f\u6307\u4ee4\u7684\u7ec4\u6210\uff0c\u6700\u7ec8\u8f6c\u6362\u4e3a\u57fa\u7840\u786c\u4ef6\u53ef\u63a5\u53d7\u7684\u7535\u4fe1\u53f7\u5b8c\u6210\u4efb\u52a1\u96c6\u3002\u5bf9\u8fd0\u7ef4\u64cd\u4f5c\u7684\u7528\u6237\u884c\u6765\u8bb2\uff0c\u9664\u6587\u4ef6\u4f20\u8f93\u4ee5\u5916\u7684\u5176\u4ed6\u64cd\u4f5c\u53ef\u79f0\u4e3a\u547d\u4ee4\u6267\u884c\u4ece\u81ea\u52a8\u5316\u5de5\u4f5c\u7c7b\u578b\u89d2\u5ea6\u5f52\u7c7b\u5982\u4e0b\uff1a<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\"><span style=\"font-weight: bold;\">1\uff09\u5e94\u7528\u90e8\u7f72<\/span><\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">\u73b0\u4eca\u7684\u5e94\u7528\u529f\u80fd\u8d8a\u6765\u8d8a\u5f3a\u5927\uff0c\u540c\u6b65\u5e94\u7528\u90e8\u7f72\u8fc7\u7a0b\u7684\u4f9d\u8d56\u548c\u89c4\u5219\u4e5f\u5f88\u590d\u6742\uff0c\u4f46\u662f\u5bf9\u5e94\u7528\u8fd0\u7ef4\u7684\u8981\u6c42\u6ca1\u6709\u964d\u4f4e\uff0c\u6709\u6548\u5feb\u901f\u6b63\u786e\u7684\u5e73\u6ed1\u7684\u5e94\u7528\u90e8\u7f72\u8981\u6c42\u5f3a\u70c8\uff0cAnsible \u5185\u7f6e\u7f51\u7edc\uff0c\u5e94\u7528\uff0c\u7cfb\u7edf\uff0c\u7b2c\u4e09\u65b9\u4e91\u5e73\u53f0\u6269\u5c55\u7b49\u5b8c\u5584\u7684\u529f\u80fd\u6a21\u5757\uff0c\u534f\u52a9\u8fd0\u7ef4\u5feb\u901f\u5b8c\u6210\u5e94\u7528\u7684\u5b89\u88c5\u3001\u5378\u8f7d\u3001\u5347\u7ea7\u3001\u542f\u505c\u3001\u914d\u7f6e\u7b49\u90e8\u7f72\u7c7b\u7684\u5de5\u4f5c\uff0c\u5373\u4f7f\u5bf9\u8de8\u5e73\u53f0\u6216\u77e5\u540d\u7684\u5546\u4e1a\u786c\u4ef6\u4e5f\u662f\u540c\u6837\u652f\u6301<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\"><span style=\"font-weight: bold;\">2\uff09\u914d\u7f6e\u7ba1\u7406<\/span><\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">\u914d\u7f6e\u7ba1\u7406\u662f\u901a\u8fc7\u6280\u672f\u6216\u884c\u653f\u624b\u6bb5\u5bf9\u8f6f\u4ef6\u4ea7\u54c1\u53ca\u5176\u5f00\u53d1\u8fc7\u7a0b\u548c\u751f\u547d\u5468\u671f\u8fdb\u884c\u63a7\u5236\u3001\u89c4\u8303\u7684\u4e00\u7cfb\u5217\u63aa\u65bd\uff0c\u914d\u7f6e\u7ba1\u7406\u7684\u76ee\u6807\u662f\u8bb0\u5f55\u8f6f\u4ef6\u4ea7\u54c1\u7684\u6f14\u5316\u8fc7\u7a0b\u3002\u786e\u4fdd\u8f6f\u4ef6\u5f00\u53d1\u8005\u5728\u8f6f\u4ef6\u751f\u547d\u5468\u671f\u4e2d\u5404\u4e2a\u9636\u6bb5\u90fd\u80fd\u5f97\u5230\u7cbe\u786e\u7684\u4ea7\u54c1\u914d\u7f6e\uff0c\u5728\u65e5\u76ca\u590d\u6742\u7684 IT \u73af\u5883\u548c\u7528\u6237\u7684\u9700\u6c42\u4e0b\uff0cAnsible \u5185\u7f6e File\u3001Template\u3001\u7ed3\u5408 Jinja\u3001Lineinfile \u7b49\u5185\u7f6e\u7684\u6a21\u5757\uff0c\u540c\u65f6\u65e0\u7f1d\u96c6\u5408 GitHub\u3001GitLab\u3001Git\u3001Jenkins \u7b49\u4e3b\u6d41\u7248\u672c\u63a7\u5236\u548c CI \u6301\u7eed\u96c6\u6210\u7684\u5de5\u5177\uff0c\u52a9\u7406\u914d\u7f6e\u7ba1\u7406\u81ea\u52a8\u5316<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">3\uff09\u6709\u6548\u4fdd\u8bc1 Tasks \u4efb\u52a1\u6d41\u6309\u65e2\u5b9a\u89c4\u5219\u548c\u987a\u5e8f\u5b8c\u6210\u4e8b\u5148\u5b9a\u5236\u7684\u76ee\u6807\u548c\u8ba1\u5212\uff0c\u540c\u65f6 Roles \u7f16\u6392\u65b9\u5f0f\u53c8\u80fd\u5728\u4e00\u5b9a\u7684\u7a0b\u5e8f\u4e0a\u4ece\u4e66\u5199\u4e60\u60ef\u548c\u4ee3\u7801\u5c42\u7f16\u6392\u4e0a\u4fdd\u8bc1\u6574\u4f53\u9879\u76ee\u7684\u53ef\u67b6\u6784\u6027\u548c\u89c4\u8303\u6027\uff0c\u534f\u52a9\u63a7\u5236\u9879\u76ee\u7ef4\u62a4\u6210\u672c\u4e0d\u81f4\u8fc7\u9ad8\u3002<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">\u5982\u4e0a\u573a\u666f\u9002\u7528\u4e8e\u7f51\u7edc\u7ba1\u7406\u5458\uff0c\u7cfb\u7edf\u8fd0\u7ef4\uff0c\u5e94\u7528\u8fd0\u7ef4\uff0c\u684c\u9762\u8fd0\u7ef4\uff0cDevOps\u3002\u57fa\u7840\u67b6\u6784\u8fd0\u7ef4\u7b49\u884c\u4e1a\uff0c\u5f00\u53d1\u4eba\u5458\u7ecf\u8fc7\u7b80\u5355\u7684\u4e86\u89e3\u5373\u53ef\u521d\u6b65\u4e0a\u624b\uff0c\u540c\u6837\u4e5f\u9002\u7528\u4e8e\u4e2d\u5927\u578b\u516c\u53f8\uff0c\u53ef\u4ee5\u6295\u5165\u4eba\u529b\uff0c\u7cbe\u529b\uff0c\u8d22\u529b\u5bf9 Ansible \u8fdb\u884c\u4e8c\u6b21\u5f00\u53d1\u7b49\u3002<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\"><span style=\"font-weight: bold;\">4\u3001Ansible\u7684\u7279\u70b9<\/span><\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">\u90e8\u7f72\u7b80\u5355\uff0c\u53ea\u9700\u5728\u4e3b\u63a7\u7aef\u90e8\u7f72Ansible\u73af\u5883\uff0c\u88ab\u63a7\u7aef\u65e0\u9700\u505a\u4efb\u4f55\u64cd\u4f5c\uff1b<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">\u9ed8\u8ba4\u4f7f\u7528SSH\u534f\u8bae\u5bf9\u8bbe\u5907\u8fdb\u884c\u7ba1\u7406\uff1b<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">\u6709\u5927\u91cf\u5e38\u89c4\u8fd0\u7ef4\u64cd\u4f5c\u6a21\u5757\uff0c\u53ef\u5b9e\u73b0\u65e5\u5e38\u7edd\u5927\u90e8\u5206\u64cd\u4f5c\uff1b<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">\u914d\u7f6e\u7b80\u5355\u3001\u529f\u80fd\u5f3a\u5927\u3001\u6269\u5c55\u6027\u5f3a\uff1b<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">\u652f\u6301API\u53ca\u81ea\u5b9a\u4e49\u6a21\u5757\uff0c\u53ef\u901a\u8fc7Python\u8f7b\u677e\u6269\u5c55\uff1b<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">\u901a\u8fc7Playbooks\u6765\u5b9a\u5236\u5f3a\u5927\u7684\u914d\u7f6e\u3001\u72b6\u6001\u7ba1\u7406\uff1b<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">\u8f7b\u91cf\u7ea7\uff0c\u65e0\u9700\u5728\u5ba2\u6237\u7aef\u5b89\u88c5agent\uff0c\u66f4\u65b0\u65f6\uff0c\u53ea\u9700\u5728\u64cd\u4f5c\u673a\u4e0a\u8fdb\u884c\u4e00\u6b21\u66f4\u65b0\u5373\u53ef\uff1b<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 12.0pt;\">\u63d0\u4f9b\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u3001\u64cd\u4f5c\u6027\u5f3a\u7684Web\u7ba1\u7406\u754c\u9762\u548cREST API\u63a5\u53e3\u2014\u2014AWX\u5e73\u53f0\u3002<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 20.0pt;\"><span lang=\"en-US\">a<\/span><span lang=\"zh-CN\">nsible\u5b89\u88c5\u914d\u7f6e\u4ecb\u7ecd<\/span><\/p>\n<p><b>1<\/b><b>\u3001\u57fa\u7840\u914d\u7f6e\uff1a<\/b><\/p>\n<p>ansible\u7684\u5b89\u88c5\u6765\u6e90\u4e8eepel\u4ed3\u5e93\uff0c\u56e0\u6b64\u5728\u5b89\u88c5\u524d\u9700\u786e\u4fdd\u5b89\u88c5\u4e86\u6b63\u786e\u7684epel\u6e90\u3002<\/p>\n<p>yum install -y epel-release<\/p>\n<p>yum install -y ansible<\/p>\n<p><b>2<\/b><b>\u3001\u5e38\u7528\u529f\u80fd\u6f14\u793a<\/b><\/p>\n<p>\u914d\u7f6e\u63a8\u9001\u4e3b\u673assh\u79d8\u94a5<\/p>\n<p>ansible\u7684\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<p>\/etc\/ansible\/ansible.cfg \u4e3b\u914d\u7f6e\u6587\u4ef6\uff0c\u914d\u7f6eansible\u5de5\u4f5c\u7279\u6027<\/p>\n<p>\/etc\/ansible\/hosts \u4e3b\u673a\u6e05\u5355<\/p>\n<p>\/etc\/ansible\/roles\/ \u5b58\u653e\u89d2\u8272\u7684\u76ee\u5f55<\/p>\n<p>[root@jumpserver ~]# cat \/etc\/ansible\/hosts<\/p>\n<p>[webapp]<\/p>\n<p>10.100.7.30<\/p>\n<p>10.100.7.31<\/p>\n<p>10.100.7.32<\/p>\n<p>10.100.7.33<\/p>\n<p>\u67e5\u8be2\u547d\u4ee4<\/p>\n<p>[root@jumpserver ~]# ansible webapp -m command -a &#8216;uptime&#8217;<\/p>\n<p>10.100.7.33 | CHANGED | rc=0 &gt;&gt;<\/p>\n<p>11:06:00 up 235 days, 19:23,\u00a0 1 user,\u00a0 load average: 0.00, 0.01, 0.05<\/p>\n<p>10.100.7.30 | CHANGED | rc=0 &gt;&gt;<\/p>\n<p>11:05:56 up 355 days,\u00a0 1:34,\u00a0 1 user,\u00a0 load average: 0.00, 0.01, 0.05<\/p>\n<p>10.100.7.31 | CHANGED | rc=0 &gt;&gt;<\/p>\n<p>11:05:57 up 266 days, 18:16,\u00a0 1 user,\u00a0 load average: 0.00, 0.01, 0.05<\/p>\n<p>10.100.7.32 | CHANGED | rc=0 &gt;&gt;<\/p>\n<p>11:06:01 up 201 days, 18:06,\u00a0 1 user,\u00a0 load average: 0.00, 0.01, 0.05<\/p>\n<p>\u8fde\u901a\u6027\u6d4b\u8bd5<\/p>\n<p>[root@jumpserver ~]# ansible all -m ping<\/p>\n<p>10.100.7.30 | SUCCESS =&gt; {<\/p>\n<p>&#8220;ansible_facts&#8221;: {<\/p>\n<p>&#8220;discovered_interpreter_python&#8221;: &#8220;\/usr\/bin\/python&#8221;<\/p>\n<p>},<\/p>\n<p>&#8220;changed&#8221;: false,<\/p>\n<p>&#8220;ping&#8221;: &#8220;pong&#8221;<\/p>\n<p>}<\/p>\n<p>10.100.7.31 | SUCCESS =&gt; {<\/p>\n<p>&#8220;ansible_facts&#8221;: {<\/p>\n<p>&#8220;discovered_interpreter_python&#8221;: &#8220;\/usr\/bin\/python&#8221;<\/p>\n<p>},<\/p>\n<p>&#8220;changed&#8221;: false,<\/p>\n<p>&#8220;ping&#8221;: &#8220;pong&#8221;<\/p>\n<p>}<\/p>\n<p><b>3<\/b><b>\u3001\u5e38\u7528\u6a21\u5757<\/b><\/p>\n<p>\u7528\u6765\u67e5\u770b\u8fdc\u7a0b\u4e3b\u673a\u7684\u4e00\u4e9b\u57fa\u672c\u4fe1\u606f<\/p>\n<p>ansible webservers -m setup<\/p>\n<p>fetch \u4ece\u4e3b\u673a\u4e2d\u83b7\u53d6\u6587\u4ef6<\/p>\n<p>ansible 192.168.128.172 -m fetch -a &#8216;src=\/root\/1.txt dest=\/root&#8217;<\/p>\n<p>\u8fdc\u7a0b\u6587\u4ef6\u7b26\u53f7\u94fe\u63a5\u521b\u5efa<\/p>\n<p>ansible webservers -m file -a &#8220;src=\/etc\/resolv.conf dest=\/tmp\/resolv.conf state=link&#8221;<\/p>\n<p>\u5c06\u672c\u5730\u6587\u4ef6\u201c\/etc\/ansible\/ansible.cfg\u201d\u590d\u5236\u5230\u8fdc\u7a0b\u670d\u52a1\u5668<\/p>\n<p>ansible webservers -m copy -a &#8220;src=\/etc\/ansible\/ansible.cfg dest=\/tmp\/ansible.cfg owner=root group=root mode=0644&#8221;<\/p>\n<p># \u8fdc\u7a0b\u6587\u4ef6\u4fe1\u606f\u67e5\u770b<\/p>\n<p>ansible webservers -m command -a &#8220;ls -al \/tmp\/ansible.cfg&#8221;<\/p>\n<p>\u6dfb\u52a0\u7528\u6237<\/p>\n<p>ansible webservers -m command -a &#8216;useradd abc&#8217;<\/p>\n<p>shell<\/p>\n<p>\u5c06\u521b\u5efa\u7684\u811a\u672c\u6587\u4ef6\u5206\u53d1\u5230\u8fdc\u7a0b<\/p>\n<p>ansible webservers -m copy -a &#8220;src=\/root\/a.sh dest=\/tmp\/a.sh owner=root group=root mode=0755&#8221;<\/p>\n<p>\u8fdc\u7a0b\u6267\u884c<\/p>\n<p>ansible webservers -m shell -a &#8220;\/tmp\/a.sh&#8221;<\/p>\n<p>\u6279\u91cf\u6dfb\u52a0\u7528\u6237\u5bc6\u7801<\/p>\n<p>ansible webservers -m shell -a &#8216;echo 123 |passwd &#8211;stdin abc&#8217;<\/p>\n<p>cron\u6a21\u5757\uff1a\u7ba1\u7406\u8ba1\u5212\u4efb\u52a1\u6761\u76ee<\/p>\n<p>\u793a\u4f8b\uff1a\u521b\u5efa\u4e00\u4e2a\u540c\u6b65\u65f6\u95f4\u7684\u8ba1\u5212\u4efb\u52a1\uff0c\u6bcf5\u5206\u949f\u540c\u6b65\u4e00\u4e0b\u670d\u52a1\u5668\u7684\u65f6\u95f4<\/p>\n<p>ansible all -m cron -a &#8220;minute=&#8217;*\/5&#8242; job=&#8217;\/usr\/sbin\/ntpdate windowstime &amp;&gt;\/dev\/null&#8217; name=&#8217;sync time'&#8221;<\/p>\n<p>\u5220\u9664<\/p>\n<p>ansible all -m cron -a &#8220;name=&#8217;sync time&#8217; state=absent&#8221;<\/p>\n<p>hostname\u6a21\u5757\uff1a\u7ba1\u7406\u4e3b\u673a\u540d<\/p>\n<p>\u83b7\u53d6\u4e3b\u673a\u540d<\/p>\n<p>ansible all -a &#8216;hostname&#8217;<\/p>\n<p>ansible 192.168.128.172 -m hostname -a &#8220;name=web_01&#8221;<\/p>\n<p>yum\u6a21\u5757<\/p>\n<p>yum\u5b89\u88c5vsftpd<\/p>\n<p>ansible all -m yum -a &#8216;name=vsftpd&#8217;<\/p>\n<p>\u5378\u8f7d<\/p>\n<p>ansible all -m yum -a &#8216;name=vsftpd state=absent&#8217;<\/p>\n<p>service\u6a21\u5757\uff1a\u670d\u52a1\u7ba1\u7406<\/p>\n<p>\u5f00\u542f\u4e3b\u673a\u7684httpd\u670d\u52a1<\/p>\n<p>ansible all -m service -a &#8220;name=httpd state=started enabled=true&#8221;<\/p>\n<p>group\u6a21\u5757\uff1a\u589e\u52a0\u6216\u5220\u9664\u7ec4<\/p>\n<p>\u793a\u4f8b\uff1a\u6dfb\u52a0\u4e00\u4e2a\u7ec4<\/p>\n<p>ansible all -m group -a &#8220;name=gansible system=true&#8221;<\/p>\n<p>user\u6a21\u5757 \u7528\u6237\u7ba1\u7406<\/p>\n<p>\u6dfb\u52a0\u4e00\u4e2a\u7cfb\u7edf\u7528\u6237<\/p>\n<p>[root@lvs_S ~]# ansible all -m user -a &#8220;name=ccc system=true&#8221;<\/p>\n<p style=\"margin: 0in; font-family: \u5b8b\u4f53; font-size: 20.0pt;\"><span lang=\"en-US\">a<\/span><span lang=\"zh-CN\">nsible playbook\u4ecb\u7ecd<\/span><\/p>\n<p>playbook\u662f\u7531\u4e00\u4e2a\u6216\u591a\u4e2a&#8221;play&#8221;\u7ec4\u6210\u7684\u5217\u8868<\/p>\n<p>play\u7684\u4e3b\u8981\u529f\u80fd\u5728\u4e8e\u5c06\u9884\u5b9a\u4e49\u7684\u4e00\u7ec4\u4e3b\u673a\uff0c\u88c5\u626e\u6210\u4e8b\u5148\u901a\u8fc7ansible\u4e2d\u7684task\u5b9a\u4e49\u597d\u7684\u89d2\u8272\u3002<\/p>\n<p>Task\u5b9e\u9645\u662f\u8c03\u7528ansible\u7684\u4e00\u4e2amodule\uff0c\u5c06\u591a\u4e2aplay\u7ec4\u7ec7\u5728\u4e00\u4e2aplaybook\u4e2d\uff0c<\/p>\n<p>\u5373\u53ef\u4ee5\u8ba9\u5b83\u4eec\u8054\u5408\u8d77\u6765\uff0c\u6309\u4e8b\u5148\u7f16\u6392\u7684\u673a\u5236\u6267\u884c\u9884\u5b9a\u4e49\u7684\u52a8\u4f5c<\/p>\n<p>Playbook\u91c7\u7528YAML\u8bed\u8a00\u7f16\u5199<\/p>\n<p><b>1<\/b><b>\u3001\u6838\u5fc3\u5143\u7d20<\/b><\/p>\n<p>Tasks\uff1a\u4efb\u52a1\uff0c\u7531\u6a21\u677f\u5b9a\u4e49\u7684\u64cd\u4f5c\u5217\u8868<\/p>\n<p>Variables\uff1a\u53d8\u91cf<\/p>\n<p>Templates\uff1a\u6a21\u677f\uff0c\u5373\u4f7f\u7528\u6a21\u677f\u8bed\u6cd5\u7684\u6587\u4ef6<\/p>\n<p>Handlers\uff1a\u5904\u7406\u5668 \uff0c\u5f53\u67d0\u6761\u4ef6\u6ee1\u8db3\u65f6\uff0c\u89e6\u53d1\u6267\u884c\u7684\u64cd\u4f5c<\/p>\n<p>Roles\uff1a\u89d2\u8272<\/p>\n<p><b>2<\/b><b>\u3001<\/b><b>h<\/b><b>osts\u548cusers\u4ecb\u7ecd<\/b><\/p>\n<p>&#8212;<br \/>\n&#8211; hosts: abc\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u6307\u5b9a\u4e3b\u673a\u7ec4\uff0c\u53ef\u4ee5\u662f\u4e00\u4e2a\u6216\u591a\u4e2a\u7ec4\u3002<br \/>\nremote_user: root\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u6307\u5b9a\u8fdc\u7a0b\u4e3b\u673a\u6267\u884c\u7684\u7528\u6237\u540d<\/p>\n<p>\u6307\u5b9a\u8fdc\u7a0b\u4e3b\u673asudo\u5207\u6362\u7528<br \/>\n# vim ping.yml<br \/>\n&#8212;<br \/>\n&#8211; hosts: abc<br \/>\nremote_user: root<br \/>\nbecome: yes\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #2.6\u7248\u672c\u4ee5\u540e\u7684\u53c2\u6570\uff0c\u4e4b\u524d\u662fsudo\uff0c\u610f\u601d\u4e3a\u5207\u6362\u7528\u6237\u8fd0\u884c<br \/>\nbecome_user: mysql\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u6307\u5b9asudo\u7528\u6237\u4e3amysql<br \/>\n\u6267\u884cplaybook<br \/>\n# ansible-playbook ping.yml -K<\/p>\n<p><b>3<\/b><b>\u3001<\/b><b>T<\/b><b>asks list \u548caction\u4ecb\u7ecd<\/b><\/p>\n<p>\u5e38\u7528\u547d\u4ee4<\/p>\n<p># ansible-playbook a.yml &#8211;syntax-check\u00a0\u00a0\u00a0 #\u68c0\u67e5yaml\u6587\u4ef6\u7684\u8bed\u6cd5\u662f\u5426\u6b63\u786e<br \/>\n# ansible-playbook a.yml &#8211;list-task\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u68c0\u67e5tasks\u4efb\u52a1<br \/>\n# ansible-playbook a.yml &#8211;list-hosts\u00a0\u00a0\u00a0\u00a0\u00a0 #\u68c0\u67e5\u751f\u6548\u7684\u4e3b\u673a<br \/>\n# ansible-playbook a.yml &#8211;start-at-task=&#8217;Copy Nginx.conf&#8217;\u00a0\u00a0\u00a0\u00a0 #\u6307\u5b9a\u4ece\u67d0\u4e2atask\u5f00\u59cb\u8fd0\u884c<\/p>\n<p>\u793a\u4f8b<br \/>\n# vim a.yml<br \/>\n&#8212;<br \/>\n&#8211; hosts: 192.168.200.129\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u6307\u5b9a\u4e3b\u673a<br \/>\nremote_user: root\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u6307\u5b9a\u5728\u88ab\u7ba1\u7406\u7684\u4e3b\u673a\u4e0a\u6267\u884c\u4efb\u52a1\u7684\u7528\u6237<br \/>\ntasks:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u4efb\u52a1\u5217\u8868\u2193<br \/>\n&#8211; name: disable selinux\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u4efb\u52a1\u540d\u5173\u95ed\u9632\u706b\u5899<br \/>\ncommand: &#8216;\/sbin\/setenforce 0&#8217;\u00a0\u00a0\u00a0 \/\/\u8c03\u7528command\u6a21\u5757 \u6267\u884c\u5173\u95ed\u9632\u706b\u5899\u547d\u4ee4<br \/>\n&#8211; name: start httpd\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u4efb\u52a1\u540d \u5f00\u542fhttpd<br \/>\nservice: name=httpd state=started\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u8c03\u7528service\u6a21\u5757 \u5f00\u542fhttpd \u670d\u52a1<\/p>\n<p># ansible-playbook a.yml &#8211;syntax-check\u00a0\u00a0\u00a0 #\u68c0\u67e5yaml\u6587\u4ef6\u7684\u8bed\u6cd5\u662f\u5426\u6b63\u786e<\/p>\n<p>playbook: test.yml #\u8868\u793a\u6b63\u786e\u8bed\u6cd5<\/p>\n<p># ansible-playbook a.yml\u00a0 #\u6267\u884cyml\u6587\u4ef6<\/p>\n<p>\u6ce8\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"373\" height=\"182\" class=\"alignnone wp-image-916 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-23.png\" \/><\/p>\n<p>play\u4e2d\u53ea\u8981\u6267\u884c\u547d\u4ee4\u7684\u8fd4\u56de\u503c\u4e0d\u4e3a0\uff0c\u5c31\u4f1a\u62a5\u9519\uff0ctasks\u505c\u6b62\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4e0b\u9762ignore_errors: True #\u5ffd\u7565\u9519\u8bef\uff0c\u5f3a\u5236\u8fd4\u56de\u6210\u529f<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"409\" height=\"151\" class=\"alignnone wp-image-917 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-24.png\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"477\" height=\"203\" class=\"alignnone wp-image-918 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-25.png\" \/><\/p>\n<p><b>4<\/b><b>\u3001<\/b><b>H<\/b><b>andlers\u4ecb\u7ecd<\/b><\/p>\n<p>andlers\u4e5f\u662f\u4e00\u4e9btask\u7684\u5217\u8868\uff0c\u548c\u4e00\u822c\u7684task\u5e76\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\u3002<\/p>\n<p>\u662f\u7531\u901a\u77e5\u8005\u8fdb\u884c\u7684notify\uff0c\u5982\u679c\u6ca1\u6709\u88abnotify\uff0c\u5219Handlers\u4e0d\u4f1a\u6267\u884c\uff0c\u5047\u5982\u88abnotify\u4e86\uff0c\u5219Handlers\u88ab\u6267\u884c<\/p>\n<p>\u4e0d\u7ba1\u6709\u591a\u5c11\u4e2a\u901a\u77e5\u8005\u8fdb\u884c\u4e86notify\uff0c\u7b49\u5230play\u4e2d\u7684\u6240\u6709task\u6267\u884c\u5b8c\u6210\u4e4b\u540e\uff0chandlers\u4e5f\u53ea\u4f1a\u88ab\u6267\u884c\u4e00\u6b21<\/p>\n<p>\u793a\u4f8b<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"473\" height=\"196\" class=\"alignnone wp-image-919 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-26.png\" \/><\/p>\n<p>\u5f15\u7528\u53d8\u91cf<\/p>\n<p>\u4f7f\u7528\uff1avars: \u6dfb\u52a0\u53d8\u91cf<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"426\" height=\"245\" class=\"alignnone wp-image-920 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-27.png\" \/><\/p>\n<p>\u76f4\u63a5\u5f15\u7528Ansible\u53d8\u91cf<\/p>\n<p>ansible_all_ipv4_addresses \u83b7\u53d6IP<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"441\" height=\"136\" class=\"alignnone wp-image-921 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-28.png\" \/><\/p>\n<p>\u5f15\u7528\u4e3b\u673a\u53d8\u91cf<\/p>\n<p>\u5728\u7ec4\u7684\u4e3b\u673a\u540e\u9762\u6dfb\u52a0\u53d8\u91cf<\/p>\n<p># vim \/etc\/ansible\/hosts<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"335\" height=\"78\" id=\"thepasted-8\" class=\"alignnone wp-image-922 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-29.png\" \/><\/p>\n<p># vim c.yml<br \/>\n# ansible-playbook c.yml\u00a0 \/\/\u6267\u884c\u8fd9\u4e2a\u5267\u672c<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"479\" height=\"124\" class=\"alignnone wp-image-923 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-30.png\" \/><\/p>\n<p>\u67e5\u770b\u8fd9\u4e2a\u751f\u6210\u7684\u6587\u4ef6<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"453\" height=\"61\" class=\"alignnone wp-image-924 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-31.png\" \/><\/p>\n<p>\u6761\u4ef6\u5224\u65ad<\/p>\n<p>when\u7684\u503c\u662f\u4e00\u4e2a\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u5982\u679c\u6761\u4ef6\u5224\u65ad\u6210\u7acb\uff0c\u8fd9\u4e2atask\u5c31\u6267\u884c\uff0c\u5982\u679c\u5224\u65ad\u4e0d\u6210\u7acb\uff0c\u5219task\u4e0d\u6267\u884c<\/p>\n<p>\u5982\u679c\u9700\u8981\u6839\u636e\u53d8\u91cf\u3001facts\uff08setup\uff09\u6216\u6b64\u524d\u4efb\u52a1\u7684\u6267\u884c\u7ed3\u679c\u6765\u4f5c\u4e3a\u67d0task\u6267\u884c\u4e0e\u5426\u7684\u524d\u63d0\u65f6\u8981\u7528\u5230\u6761\u4ef6\u6d4b\u8bd5\uff0c\u5728Playbook\u4e2d\u6761\u4ef6\u6d4b\u8bd5\u4f7f\u7528when\u5b50\u53e5\u3002<\/p>\n<p>\u5728task\u540e\u6dfb\u52a0when\u5b50\u53e5\u5373\u53ef\u4f7f\u7528\u6761\u4ef6\u6d4b\u8bd5\uff1awhen\u5b50\u53e5\u652f\u6301jinjia2\u8868\u8fbe\u5f0f\u6216\u8bed\u6cd5\uff0c\u4f8b\u5982\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"429\" height=\"191\" class=\"alignnone wp-image-925 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-32.png\" \/><\/p>\n<p>\u591a\u6761\u4ef6\u5224\u65ad<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"413\" height=\"177\" class=\"alignnone wp-image-926 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-33.png\" \/><\/p>\n<p>\u7ec4\u6761\u4ef6\u5224\u65ad<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"473\" height=\"175\" class=\"alignnone wp-image-927 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-34.png\" \/><\/p>\n<p>\u81ea\u5b9a\u4e49\u6761\u4ef6\u5224\u65ad<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"381\" height=\"222\" class=\"alignnone wp-image-928 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-35.png\" \/><\/p>\n<p>\u8fed\u4ee3<\/p>\n<p>\u6709\u9700\u8981\u91cd\u590d\u6027\u6267\u884c\u7684\u4efb\u52a1\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fed\u4ee3\u673a\u5236\u3002\u5176\u4f7f\u7528\u683c\u5f0f\u4e3a\u5c06\u9700\u8981\u8fed\u4ee3\u7684\u5185\u5bb9\u5b9a\u4e49\u4e3aitem\u53d8\u91cf\u5f15\u7528\uff0c\u5e76\u901a\u8fc7with_items\u8bed\u53e5\u6307\u660e\u8fed\u4ee3\u7684\u5143\u7d20\u5217\u8868\u5373\u53ef\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"425\" height=\"236\" class=\"alignnone wp-image-929 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-36.png\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"444\" height=\"191\" class=\"alignnone wp-image-930 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-37.png\" \/><\/p>\n<p><b>5<\/b><b>\u3001<\/b><b>T<\/b><b>emplates\u4ecb\u7ecd<\/b><\/p>\n<p>Jinja\u662f\u57fa\u4e8ePython\u7684\u6a21\u677f\u5f15\u64ce\u3002template\u7c7b\u662fJinja\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u770b\u4f5c\u4e00\u4e2a\u7f16\u8bd1\u8fc7\u7684\u6a21\u5757\u6587\u4ef6\uff0c\u7528\u6765\u751f\u4ea7\u76ee\u6807\u6587\u672c\uff0c\u4f20\u9012Python\u7684\u53d8\u91cf\u7ed9\u6a21\u677f\u53bb\u66ff\u6362\u6a21\u677f\u4e2d\u7684\u6807\u8bb0\u3002<br \/>\n# scp root@192.168.175.130:\/etc\/httpd\/conf\/httpd.conf .\/templates\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u590d\u5236\u88ab\u7ba1\u7406\u7aef\u7684\u914d\u7f6e\u6587\u4ef6\u5230\u672c\u5730<br \/>\n# vim templates\/httpd.conf\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u5728\u7ba1\u7406\u7aef\u8bb2\u914d\u7f6e\u6587\u4ef6\u8981\u4fee\u6539\u7684\u5730\u65b9\u5b9a\u4e49\u53d8\u91cf<br \/>\nListen {{http_port}}<br \/>\nServerName {{server_name}}<br \/>\nMaxClients {{access_num}}<\/p>\n<p>\u5728\/etc\/ansible\/hosts \u6dfb\u52a0\u53d8\u91cf<br \/>\n# vim \/etc\/ansible\/hosts<br \/>\n[abc]<br \/>\n192.168.200.129 http_port=192.168.200.129:80 access_num=100 server_name=&#8221;www.yun.com:80&#8243;<br \/>\n# vim apache.yml<br \/>\n# ansible-playbook apache.yml\u00a0\u00a0\u00a0 #\u7136\u540e\u6267\u884c\u811a\u672c\u00a0 \u7136\u540e\u53bbabc\u7ec4\u7684\u4e3b\u673a\u4e0a\u67e5\u770b\u4e0b\u914d\u7f6e\u6587\u4ef6\u662f\u5426\u5df2\u7ecf\u6539\u4e86<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"466\" height=\"226\" class=\"alignnone wp-image-931 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-38.png\" \/><\/p>\n<p>Tags\u4ecb\u7ecd<\/p>\n<p>\u5728\u4e00\u4e2aplaybook\u4e2d\uff0c\u6211\u4eec\u4e00\u822c\u4f1a\u5b9a\u4e49\u5f88\u591a\u4e2atask\uff0c\u5982\u679c\u6211\u4eec\u53ea\u60f3\u6267\u884c\u5176\u4e2d\u7684\u67d0\u4e00\u4e2atask\u6216\u591a\u4e2atask\u65f6\u5c31\u53ef\u4ee5\u4f7f\u7528tags\u6807\u7b7e\u529f\u80fd\u4e86<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"420\" height=\"168\" class=\"alignnone wp-image-932 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-39.png\" \/><\/p>\n<p># ansible-playbook hosts.yml &#8211;tags=&#8221;only&#8221;\u00a0\u00a0 \/\/\u53ea\u6267\u884c\u8fd9\u4e2a\u6807\u7b7e\u4e0a\u9762\u7684\u4efb\u52a1<\/p>\n<p>\u4e8b\u5b9e\u4e0a\uff0c\u4e0d\u5149\u53ef\u4ee5\u4e3a\u5355\u4e2a\u6216\u591a\u4e2atask\u6307\u5b9a\u540c\u4e00\u4e2atags\u3002playbook\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7279\u6b8a\u7684tags\u4e3aalways\u3002\u4f5c\u7528\u5c31\u662f\u5f53\u4f7f\u7528always\u5f53tags\u7684task\u65f6\uff0c\u65e0\u8bba\u6267\u884c\u54ea\u4e00\u4e2atags\u65f6\uff0c\u5b9a\u4e49\u6709always\u7684tags\u90fd\u4f1a\u6267\u884c\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"539\" height=\"231\" class=\"alignnone wp-image-933 size-full\" src=\"http:\/\/www.rchen.top\/wp-content\/uploads\/2022\/07\/Pasted-40.png\" \/><\/p>\n<p>\u6267\u884c\u4e0b\u9762\u547d\u4ee4<br \/>\n# ansible-playbook hosts.yml &#8211;tags=&#8221;only&#8221;\u00a0\u00a0\u00a0 \/\/\u53bb\u88ab\u7ba1\u7406\u4e3b\u673a\u4e0a\u67e5\u770b\u6587\u4ef6\u521b\u5efa\u60c5\u51b5 2\u4e2atags\u90fd\u4f1a\u6267\u884c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001Ansible \u662f\u4ec0\u4e48\uff1f Ansible\u662f\u4e00\u6b3e \u57fa\u4e8ePython\u5f00\u53d1\u7684\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177\uff0c\u96c6\u5408\u4e86\u4f17\u591a\u8fd0\u7ef4\u5de5\u5177\uff08 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":490,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[141,166],"tags":[167],"class_list":["post-915","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-141","category-5ci-cd","tag-cicd"],"_links":{"self":[{"href":"https:\/\/www.rchen.top\/index.php?rest_route=\/wp\/v2\/posts\/915","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rchen.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rchen.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rchen.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rchen.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=915"}],"version-history":[{"count":2,"href":"https:\/\/www.rchen.top\/index.php?rest_route=\/wp\/v2\/posts\/915\/revisions"}],"predecessor-version":[{"id":1814,"href":"https:\/\/www.rchen.top\/index.php?rest_route=\/wp\/v2\/posts\/915\/revisions\/1814"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.rchen.top\/index.php?rest_route=\/wp\/v2\/media\/490"}],"wp:attachment":[{"href":"https:\/\/www.rchen.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rchen.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rchen.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}