{"id":18978,"date":"2025-04-19T01:22:10","date_gmt":"2025-04-18T17:22:10","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/18978.html"},"modified":"2025-04-19T01:22:10","modified_gmt":"2025-04-18T17:22:10","slug":"ansible%e8%bf%90%e8%a1%8c%e5%8e%9f%e7%90%86%e6%8f%ad%e7%a7%98%ef%bc%9a%e5%a6%82%e4%bd%95%e7%94%a8yaml%e8%84%9a%e6%9c%ac%e6%8e%8c%e6%8e%a7%e6%95%b0%e6%9c%8d%e5%8a%a1%e5%99%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/18978.html","title":{"rendered":"Ansible\u8fd0\u884c\u539f\u7406\u63ed\u79d8\uff1a\u5982\u4f55\u7528YAML\u811a\u672c\u638c\u63a7\u6570\u670d\u52a1\u5668\uff1f"},"content":{"rendered":"<h2 id=\"YSvD-1741689433545\" style=\"text-align:left\">1 Ansible\u57fa\u672c\u67b6\u6784<\/h2>\n<h3 id=\"qqor-1741689433547\" style=\"text-align:left\">1.1 <span style=\"color:#000000\">Ansible\u5de5\u4f5c\u6a21\u5f0f<\/span><\/h3>\n<p>  <span style=\"color:#000000\">Ansible\u662f\u57fa\u4e8e\u6a21\u5757\u5de5\u4f5c\u7684&#xff0c;\u672c\u8eab\u6ca1\u6709\u6279\u91cf\u90e8\u7f72\u7684\u80fd\u529b&#xff0c;\u771f\u6b63\u5177\u6709\u6279\u91cf\u90e8\u7f72\u80fd\u529b\u7684\u662fAnsible\u6240\u8fd0\u884c\u7684\u6a21\u5757&#xff0c;Ansible\u53ea\u662f\u63d0\u4f9b\u4e00\u79cd\u6846\u67b6\u3002<\/span> <\/p>\n<p> <img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"603\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418172209-68028a4196d20.png\" width=\"1017\" \/> <\/p>\n<h3 id=\"VVaz-1741689433553\" style=\"text-align:left\">1.2 Ansible\u7ec4\u4ef6<\/h3>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\">Ansible&#xff1a;Ansible\u6838\u5fc3\u7a0b\u5e8f<\/li>\n<li style=\"text-align:left\">Play books&#xff1a;\u4efb\u52a1\u5267\u672c&#xff08;\u4efb\u52a1\u96c6&#xff09;&#xff0c;\u7f16\u6392\u5b9a\u4e49Ansible\u4efb\u52a1\u96c6\u7684\u914d\u7f6e\u6587\u4ef6&#xff0c;\u7531Ansible\u4f9d\u6b21\u6267\u884c\u591a\u4e2a\u4efb\u52a1&#xff0c;\u901a\u5e38\u662fJSON\u683c\u5f0f\u7684YML\u6587\u4ef6<\/li>\n<li style=\"text-align:left\">Host inventory&#xff1a;Ansible\u7ba1\u7406\u4e3b\u673a\u7684\u6e05\u5355&#xff0c;\u8bb0\u5f55\u7531Ansible\u7ba1\u7406\u7684\u4e3b\u673a\u4fe1\u606f&#xff0c;\u5305\u62ec\u7aef\u53e3\u3001\u5bc6\u7801\u3001IP\u7b49<\/li>\n<li style=\"text-align:left\">Modules&#xff1a;\u6838\u5fc3\u6a21\u5757&#xff0c;\u4e3b\u8981\u64cd\u4f5c\u662f\u901a\u8fc7\u8c03\u7528\u6838\u5fc3\u6a21\u5757\u6765\u5b8c\u6210\u7ba1\u7406\u4efb\u52a1<\/li>\n<li style=\"text-align:left\">CustomModules&#xff1a;\u81ea\u5b9a\u4e49\u6a21\u5757&#xff0c;\u5b8c\u6210\u6838\u5fc3\u6a21\u5757\u65e0\u6cd5\u5b8c\u6210\u7684\u529f\u80fd&#xff0c;\u652f\u6301\u591a\u79cd\u8bed\u8a00<\/li>\n<li style=\"text-align:left\">Connection plugins&#xff1a;\u57fa\u4e8e\u8fde\u63a5\u63d2\u4ef6\u8fde\u63a5\u5230\u5404\u4e2a\u4e3b\u673a\u4e0a&#xff0c;\u5373Ansible\u548cHost\u901a\u4fe1\u4f7f\u7528&#xff0c;\u9ed8\u8ba4\u662f\u4f7f\u7528SSH<\/li>\n<li style=\"text-align:left\">Plugins&#xff1a;\u6a21\u5757\u529f\u80fd\u7684\u8865\u5145&#xff0c;\u5982\u8fde\u63a5\u7c7b\u578b\u63d2\u4ef6\u3001\u5faa\u73af\u63d2\u4ef6\u3001\u53d8\u91cf\u63d2\u4ef6\u7b49&#xff0c;\u53ef\u501f\u52a9\u4e8e\u63d2\u4ef6\u5b8c\u6210\u66f4\u4e30\u5bcc\u7684\u529f\u80fd<\/li>\n<li style=\"text-align:left\">Playbooks&#xff1a;\u6279\u91cf\u7684\u547d\u4ee4\u6587\u4ef6<\/li>\n<\/ul>\n<p>   \u00a0 \u00a0 \u00a0 \u00a0\u7528\u6237\u8bf7\u6c42\u53d1\u9001\u7ed9Ansible\u6838\u5fc3\u6a21\u5757&#xff0c;Ansible\u6838\u5fc3\u6a21\u5757\u901a\u8fc7Host inventory\u6a21\u5757\u5bfb\u627e\u9700\u8981\u8fd0\u884c\u7684\u4e3b\u673a&#xff0c;\u7136\u540e\u901a\u8fc7Connection plugins\u8fde\u63a5\u8fdc\u7a0b\u7684\u4e3b\u673a&#xff0c;\u5e76\u53d1\u9001\u547d\u4ee4\u3002Ansible\u4f7f\u7528\u63d2\u4ef6\u6765\u8fde\u63a5\u6bcf\u4e00\u4e2a\u88ab\u63a7\u5236\u7aefHost&#xff0c;\u6b64\u5916&#xff0c;\u4e5f\u901a\u8fc7\u63d2\u4ef6\u6765\u8bb0\u5f55\u65e5\u5fd7\u7b49\u4fe1\u606f\u3002 <\/p>\n<h3 id=\"wXt2-1741689433574\" style=\"text-align:left\">1.3 Ansible\u751f\u6210\u7684\u4e3b\u8981\u6587\u4ef6<\/h3>\n<p>  \/etc\/ansible <\/p>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\">\/etc\/ansible\/ansible.cfg &#xff1a;\u914d\u7f6e\u6587\u4ef6<\/li>\n<li style=\"text-align:left\">\/etc\/ansible\/hosts &#xff1a;\u4e3b\u673a\u5e93&#xff08;host inventory&#xff09;&#xff0c;\u7ba1\u7406\u88ab\u76d1\u63a7\u7684\u4e3b\u673a<\/li>\n<li style=\"text-align:left\">\/usr\/bin\/ansible&#xff1a;\u4e3b\u7a0b\u5e8f<\/li>\n<li style=\"text-align:left\">\/usr\/bin\/ansible-doc&#xff1a;\u6587\u6863<\/li>\n<li style=\"text-align:left\">\/usr\/bin\/ansible-playbook&#xff1a;\u5267\u672c<\/li>\n<\/ul>\n<h2 id=\"8OfG-1741689433589\" style=\"text-align:left\">2 Ansible\u4efb\u52a1\u6267\u884c\u6a21\u5f0f<\/h2>\n<h3 id=\"FCAQ-1741689433591\" style=\"text-align:left\">2.1 Ansible\u4efb\u52a1\u6267\u884c\u6a21\u5f0f<\/h3>\n<p>  Ansible\u4efb\u52a1\u6267\u884c\u6a21\u5f0f\u5206\u4e3a2\u79cd&#xff1a; <\/p>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\">ad-hoc\u6a21\u5f0f&#xff08;\u70b9\u5bf9\u70b9\u6a21\u5757&#xff09;&#xff1a;\u4f7f\u7528\u5355\u4e2a\u6a21\u5757&#xff0c;\u652f\u6301\u6279\u91cf\u6267\u884c\u5355\u6761\u547d\u4ee4&#xff0c;\u76f8\u5f53\u4e0e\u5728bash\u4e2d\u6267\u884c\u4e00\u53e5Shell\u547d<\/li>\n<li style=\"text-align:left\">playbook\u6a21\u5f0f&#xff08;\u5267\u672c\u6a21\u5f0f&#xff09;&#xff1a;Ansible\u4e3b\u8981\u7684\u7ba1\u7406\u65b9\u5f0f&#xff0c;\u901a\u8fc7\u591a\u4e2atask\u7684\u96c6\u5408\u5b8c\u6210\u4e00\u7c7b\u529f\u80fd&#xff0c;\u53ef\u4ee5\u7406\u89e3\u4e3a\u591a\u4e2aad-hoc\u7684\u914d\u7f6e\u6587\u4ef6<\/li>\n<\/ul>\n<h3 id=\"LCKl-1741689433600\" style=\"text-align:left\">2.2 Ansible\u4efb\u52a1\u6267\u884c\u6d41\u7a0b<\/h3>\n<p> <img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"466\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418172209-68028a41d21ff.png\" width=\"610\" \/> <\/p>\n<h2 id=\"EMiB-1741689433603\" style=\"text-align:left\">3 Ansible\u5e94\u7528<\/h2>\n<h3 id=\"Xpcz-1741689433605\" style=\"text-align:left\">3.1 \u5b9a\u4e49\u4e3b\u673a\u5217\u8868&#xff08;Inventory&#xff09;<\/h3>\n<p>   \u00a0 \u00a0 \u00a0 \u00a0 Ansible\u4e3b\u8981\u529f\u80fd\u5728\u4e8e\u6279\u91cf\u4e3b\u673a\u64cd\u4f5c&#xff0c;\u4e3a\u4e86\u4fbf\u6377\u5730\u4f7f\u7528\u5176\u4e2d\u7684\u90e8\u5206\u4e3b\u673a&#xff0c;\u53ef\u4ee5\u5728inventory\u4e2d\u5c06\u4e3b\u673a\u8fdb\u884c\u5206\u7ec4\u547d\u540d\u3002\u9ed8\u8ba4\u7684inventory file\u4e3a\/etc\/ansible\/hosts\u3002inventory\u6587\u4ef6\u53ef\u4ee5\u6709\u591a\u4e2a&#xff0c;\u4e5f\u53ef\u4ee5\u901a\u8fc7Dynamic Inventory\u6765\u52a8\u6001\u751f\u6210\u3002 <\/p>\n<p>  <span style=\"color:#000000\">Inventory\u6587\u4ef6\u683c\u5f0f&#xff1a;<\/span> <\/p>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\"><span style=\"color:#000000\">Inventory\u6587\u4ef6\u9075\u5faaini\u6587\u4ef6\u98ce\u683c&#xff0c;\u4e2d\u62ec\u53f7[]\u4e2d\u7684\u5b57\u7b26\u4e3a\u7ec4\u540d<\/span><\/li>\n<li style=\"text-align:left\"><span style=\"color:#000000\">\u53ef\u4ee5\u5c06\u540c\u4e00\u4e2a\u4e3b\u673a\u540c\u65f6\u653e\u5230\u4e0d\u540c\u7684\u7ec4\u5f53\u4e2d<\/span><\/li>\n<li style=\"text-align:left\"><span style=\"color:#000000\">\u5982\u679c\u4e3b\u673a\u91c7\u7528\u7684\u662f\u975essh\u9ed8\u8ba4\u7aef\u53e3\u65f6&#xff0c;\u53ef\u4ee5\u5728\u4e3b\u673a\u540d\u79f0\u4e4b\u540e\u91c7\u7528\u5192\u53f7\u52a0\u5177\u4f53\u7aef\u53e3\u53f7\u6765\u6ce8\u660e<\/span><\/li>\n<li style=\"text-align:left\"><span style=\"color:#000000\">\u5982\u679c\u4e3b\u673a\u540d\u9075\u5faa\u76f8\u4f3c\u7684\u547d\u540d\u6a21\u5f0f&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684\u5f62\u5f0f\u6765\u8868\u793a\u4e3b\u673a\u540d<\/span><\/li>\n<li style=\"text-align:left\"><span style=\"color:#000000\">\u4e3b\u673a\u53d8\u91cf\u53ef\u4ee5\u5728Inventory\u4e2d\u5b9a\u4e49\u4e3b\u673a\u65f6\u8fdb\u884c\u6dfb\u52a0&#xff0c;\u4fbf\u4e8e\u5728playbook\u4e2d\u8fdb\u884c\u4f7f\u7528<\/span><\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;<br \/>\n1.Inventory\u6587\u4ef6\u9075\u5faaini\u6587\u4ef6\u98ce\u683c&#xff0c;\u4e2d\u62ec\u53f7[]\u4e2d\u7684\u5b57\u7b26\u4e3a\u7ec4\u540d\u53ef\u4ee5\u5c06\u540c\u4e00\u4e2a\u4e3b\u673a\u540c\u65f6\u653e\u5230\u4e0d\u540c\u7684\u7ec4\u5f53\u4e2d<br \/>\n[webservers]<br \/>\nnode1<\/p>\n<p>[dbservers]<br \/>\nnode1<\/p>\n<p>2.\u5982\u679c\u4e3b\u673a\u91c7\u7528\u7684\u662f\u975essh\u9ed8\u8ba4\u7aef\u53e3\u65f6&#xff0c;\u53ef\u4ee5\u5728\u4e3b\u673a\u540d\u79f0\u4e4b\u540e\u91c7\u7528\u5192\u53f7\u52a0\u5177\u4f53\u7aef\u53e3\u53f7\u6765\u6ce8\u660e<br \/>\n[webservers]<br \/>\nnode1:2022<\/p>\n<p>[dbservers]<br \/>\nnode1<\/p>\n<p>2.\u5982\u679c\u4e3b\u673a\u540d\u9075\u5faa\u76f8\u4f3c\u7684\u547d\u540d\u6a21\u5f0f&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684\u5f62\u5f0f\u6765\u8868\u793a\u4e3b\u673a\u540d<br \/>\n[webservers]<br \/>\nnode[1:3]<\/p>\n<p>[dbservers]<br \/>\nnode[1:2]<\/p>\n<p>3.\u4e3b\u673a\u53d8\u91cf\u53ef\u4ee5\u5728Inventory\u4e2d\u5b9a\u4e49\u4e3b\u673a\u65f6\u8fdb\u884c\u6dfb\u52a0&#xff0c;\u4fbf\u4e8e\u5728playbook\u4e2d\u8fdb\u884c\u4f7f\u7528<br \/>\n[webservers]<br \/>\nnode1 http_port&#061;80 maxRequestsPerChild&#061;808<br \/>\nnode2 http_port&#061;8080 maxRequestsPerChild&#061;909 <\/p>\n<p>\u00a0Inventory\u53c2\u6570&#xff1a;Ansible\u5728\u57fa\u4e8essh\u8fde\u63a5Inventory\u4e2d\u6307\u5b9a\u7684\u8fdc\u7a0b\u4e3b\u673a\u65f6&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u53c2\u6570\u6765\u6307\u5b9a\u5176\u4ea4\u4e92\u65b9\u5f0f&#xff0c;\u5e38\u7528\u53c2\u6570\u5982\u4e0b&#xff1a;<\/p>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\">ansible_ssh_host&#xff1a;\u8fdc\u7a0b\u4e3b\u673a<\/li>\n<li style=\"text-align:left\">ansible_ssh_user&#xff1a;\u6307\u5b9a\u8fdc\u7a0b\u4e3b\u673assh\u7aef\u53e3<\/li>\n<li style=\"text-align:left\">ansible_ssh_pass&#xff1a;\u8fde\u63a5\u8fdc\u7a0b\u4e3b\u673a\u4f7f\u7528\u7684\u5bc6\u7801<\/li>\n<li style=\"text-align:left\">ansible_sudo_pass&#xff1a;sudo\u5bc6\u7801&#xff0c;\u5efa\u8bae\u4f7f\u7528&#8211;ask-sudo-pass\u547d\u4ee4<\/li>\n<li style=\"text-align:left\">ansible_connection&#xff1a;\u6307\u5b9a\u8fde\u63a5\u7c7b\u578b&#xff0c;\u7c7b\u578b\u5305\u62ec&#xff1a;local\u3001ssh\u3001paramiko<\/li>\n<li style=\"text-align:left\">ansible_shell_type&#xff1a;\u6307\u5b9a\u8fde\u63a5\u5bf9\u7aef\u7684shell\u7c7b\u578b&#xff0c;\u9ed8\u8ba4\u652f\u6301sh<\/li>\n<li style=\"text-align:left\">ansible_ssh_private_key_file&#xff1a;ssh\u8fde\u63a5\u4f7f\u7528\u7684\u79c1\u94a5<\/li>\n<li style=\"text-align:left\">ansible_python_interpreter&#xff1a;\u6307\u5b9a\u5bf9\u7aef\u4f7f\u7528\u7684python\u7f16\u8bd1\u5668\u8def\u5f84<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;<br \/>\n\u6839\u636e\u5b9a\u4e49\u7684Inventory\u8c03\u7528ping\u68c0\u6d4b\u7f51\u7edc\u662f\u5426\u53ef\u8fbe&#xff1a;<br \/>\n\u68c0\u6d4bInventory\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u4e3b\u673a&#xff1a;ansible all -m ping<\/p>\n<p>[root&#064;node2 ~]# ansible all -m ping<br \/>\n192.168.10.32 | SUCCESS &#061;&gt; {<br \/>\n    &#034;ansible_facts&#034;: {<br \/>\n        &#034;discovered_interpreter_python&#034;: &#034;\/usr\/bin\/python&#034;<br \/>\n    },<br \/>\n    &#034;changed&#034;: false,<br \/>\n    &#034;ping&#034;: &#034;pong&#034;<br \/>\n}<br \/>\n192.168.10.30 | SUCCESS &#061;&gt; {<br \/>\n    &#034;ansible_facts&#034;: {<br \/>\n        &#034;discovered_interpreter_python&#034;: &#034;\/usr\/bin\/python&#034;<br \/>\n    },<br \/>\n    &#034;changed&#034;: false,<br \/>\n    &#034;ping&#034;: &#034;pong&#034;<br \/>\n}<br \/>\n192.168.10.31 | SUCCESS &#061;&gt; {<br \/>\n    &#034;ansible_facts&#034;: {<br \/>\n        &#034;discovered_interpreter_python&#034;: &#034;\/usr\/bin\/python&#034;<br \/>\n    },<br \/>\n    &#034;changed&#034;: false,<br \/>\n    &#034;ping&#034;: &#034;pong&#034;<br \/>\n}<br \/>\n[root&#064;node2 ~]# <\/p>\n<p>\u68c0\u6d4bInventory\u4e2d\u5b9a\u4e49\u7684dbservers\u7ec4\u7684\u4e3b\u673a&#xff1a;ansible dbservers -m ping<br \/>\n[root&#064;node2 ~]# ansible dbservers -m ping<br \/>\n192.168.10.30 | SUCCESS &#061;&gt; {<br \/>\n    &#034;ansible_facts&#034;: {<br \/>\n        &#034;discovered_interpreter_python&#034;: &#034;\/usr\/bin\/python&#034;<br \/>\n    },<br \/>\n    &#034;changed&#034;: false,<br \/>\n    &#034;ping&#034;: &#034;pong&#034;<br \/>\n}<br \/>\n[root&#064;node2 ~]# <\/p>\n<p>\u68c0\u6d4b\u6307\u5b9a\u5355\u53f0\u4e3b\u673a&#xff1a;ansible 192.168.10.30 -m ping<br \/>\n[root&#064;node2 ~]# ansible 192.168.10.30 -m ping<br \/>\n192.168.10.30 | SUCCESS &#061;&gt; {<br \/>\n    &#034;ansible_facts&#034;: {<br \/>\n        &#034;discovered_interpreter_python&#034;: &#034;\/usr\/bin\/python&#034;<br \/>\n    },<br \/>\n    &#034;changed&#034;: false,<br \/>\n    &#034;ping&#034;: &#034;pong&#034;<br \/>\n}<br \/>\n[root&#064;node2 ~]#  <\/p>\n<h3 id=\"MqaA-1741689578475\">3.2 \u57fa\u4e8ead-hoc\u6a21\u5f0f\u8fd0\u884c<\/h3>\n<p>Ansible\u901a\u8fc7ssh\u5b9e\u73b0\u76ee\u6807\u4e3b\u673a\u7684\u914d\u7f6e\u7ba1\u7406\u3001\u5e94\u7528\u90e8\u7f72\u3001\u4efb\u52a1\u6267\u884c\u7b49\u64cd\u4f5c&#xff0c;\u6545\u800c\u9700\u8981\u4e8b\u5148\u914d\u7f6eAnsible\u7aef\u80fd\u57fa\u4e8e\u79d8\u94a5\u8ba4\u8bc1\u7684\u65b9\u5f0f\u8fde\u63a5\u5404\u88ab\u7ba1\u7406\u8282\u70b9\u3002<\/p>\n<p>\u57fa\u672c\u8bed\u6cd5&#xff1a;<br \/>\nansible &lt;pattern&gt; -m &lt;module_name&gt; -a &lt;module_arguments&gt; [options]<\/p>\n<p>\u89e3\u91ca&#xff1a;<br \/>\n&lt;pattern&gt;&#xff1a;\u76ee\u6807\u4e3b\u673a\u6216\u4e3b\u673a\u7ec4&#xff0c;\u53ef\u4ee5\u662fInventory\u4e2d\u7684\u4e3b\u673a\u540d\u3001IP\u5730\u5740\u3001\u4e3b\u673a\u7ec4\u6216\u5173\u952e\u5b57&#xff08;\u5982all&#xff09;<br \/>\n-m &lt;module_name&gt;&#xff1a;\u6307\u5b9a\u8981\u4f7f\u7528\u7684\u6a21\u5757&#xff08;\u5982ping\u3001shell\u3001copy\u7b49&#xff09;<br \/>\n-a &lt;module_arguments&gt;&#xff1a;\u4f20\u9012\u7ed9\u6a21\u5757\u7684\u53c2\u6570<br \/>\n[options]&#xff1a;\u53ef\u9009\u53c2\u6570&#xff0c;\u7528\u4e8e\u63a7\u5236Ansible\u884c\u4e3a <\/p>\n<h4 id=\"p7uX-1741689611005\">3.2.1 \u5e38\u7528\u9009\u9879<\/h4>\n<table>\n<tbody>\n<tr>\n<td>\n<p>\u6a21\u5757<\/p>\n<\/td>\n<td>\n<p>\u63cf\u8ff0<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>-i<\/p>\n<\/td>\n<td>\n<p>\u6307\u5b9a\u81ea\u5b9a\u4e49\u7684Inventory\u6587\u4ef6<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>&#8211;private-key<\/p>\n<\/td>\n<td>\n<p>\u6307\u5b9aSSH\u79c1\u94a5\u6587\u4ef6<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>-u<\/p>\n<\/td>\n<td>\n<p>\u6307\u5b9aSSH\u7528\u6237<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>-k<\/p>\n<\/td>\n<td>\n<p>\u63d0\u793a\u8f93\u5165SSH\u5bc6\u7801<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>-K<\/p>\n<\/td>\n<td>\n<p>\u63d0\u793a\u8f93\u5165become&#xff08;\u63d0\u6743&#xff09;\u5bc6\u7801&#xff08;\u5982sudo\u5bc6\u7801&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>-b<\/p>\n<\/td>\n<td>\n<p>\u4f7f\u7528become&#xff08;\u63d0\u6743&#xff09;\u6267\u884c\u4efb\u52a1&#xff08;\u5982sudo&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>&#8211;become-user<\/p>\n<\/td>\n<td>\n<p>\u6307\u5b9a\u63d0\u6743\u540e\u7684\u7528\u6237&#xff08;\u9ed8\u8ba4\u4e3aroot&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>-v<\/p>\n<\/td>\n<td>\n<p>\u8f93\u51fa\u8be6\u7ec6\u4fe1\u606f&#xff08;-vvv\u6216-vvvv\u53ef\u589e\u52a0\u8c03\u8bd5\u4fe1\u606f&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>&#8211;check<\/p>\n<\/td>\n<td>\n<p>\u6a21\u62df\u8fd0\u884c&#xff08;\u4e0d\u5b9e\u9645\u6267\u884c\u4efb\u4f55\u66f4\u6539&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>&#8211;diff<\/p>\n<\/td>\n<td>\n<p>\u663e\u793a\u6587\u4ef6\u7684\u5dee\u5f02&#xff08;\u9002\u7528\u4e8e\u6587\u4ef6\u76f8\u5173\u6a21\u5757&#xff0c;\u5982copy\u3001template&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>&#8211;ask-pass<\/p>\n<\/td>\n<td>\n<p>\u63d0\u793a\u8f93\u5165SSH\u5bc6\u7801<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>&#8211;limit<\/p>\n<\/td>\n<td>\n<p>\u9650\u5236\u8fd0\u884c\u7684\u4e3b\u673a\u8303\u56f4&#xff08;\u5982limit webservers\u53ea\u5bf9webservers\u7ec4\u7684\u4e3b\u673a\u751f\u6548&#xff09;<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u793a\u4f8b&#xff1a;\u4f7f\u7528shell\u6a21\u5757\u68c0\u67e5\/etc\/passwd\u6587\u4ef6\u4e2d\u662f\u5426\u5305\u542b root<br \/>\nansible all -m shell -a &#039;grep root \/etc\/passwd&#039;<\/p>\n<p>[root&#064;node2 ~]# ansible all -m shell -a &#039;grep root \/etc\/passwd&#039;<br \/>\n192.168.10.30 | CHANGED | rc&#061;0 &gt;&gt;<br \/>\nroot:x:0:0:root:\/root:\/bin\/bash<br \/>\noperator:x:11:0:operator:\/root:\/sbin\/nologin<br \/>\n192.168.10.32 | CHANGED | rc&#061;0 &gt;&gt;<br \/>\nroot:x:0:0:root:\/root:\/bin\/bash<br \/>\noperator:x:11:0:operator:\/root:\/sbin\/nologin<br \/>\n192.168.10.31 | CHANGED | rc&#061;0 &gt;&gt;<br \/>\nroot:x:0:0:root:\/root:\/bin\/bash<br \/>\noperator:x:11:0:operator:\/root:\/sbin\/nologin<br \/>\n[root&#064;node2 ~]#  <\/p>\n<h4 id=\"PoZA-1741689638580\">3.2.2 \u5e38\u7528\u6a21\u5757<\/h4>\n<table>\n<tbody>\n<tr>\n<td>\n<p>\u9009\u9879<\/p>\n<\/td>\n<td>\n<p>\u89e3\u91ca<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>ping<\/p>\n<\/td>\n<td>\n<p>\u76ee\u6807\u4e3b\u673a\u8fde\u901a\u6027\u6d4b\u8bd5<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>command<\/p>\n<\/td>\n<td>\n<p>\u5728\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u6267\u884c\u547d\u4ee4&#xff0c;\u4e0d\u652f\u6301\u7ba1\u9053<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>shell<\/p>\n<\/td>\n<td>\n<p>\u5728\u76ee\u6807\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u8c03\u7528shell\u89e3\u91ca\u5668&#xff0c;\u652f\u6301\u7ba1\u9053\u547d\u4ee4<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>copy<\/p>\n<\/td>\n<td>\n<p>\u590d\u5236\u6587\u4ef6\u5230\u76ee\u6807\u8fdc\u7a0b\u670d\u52a1\u5668\u4e3b\u673a\u4e0a&#xff0c;\u652f\u6301\u8bbe\u5b9a\u5185\u5bb9\u548c\u4fee\u6539\u6743\u9650<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>file<\/p>\n<\/td>\n<td>\n<p>\u521b\u5efa\u6587\u4ef6\u3001\u94fe\u63a5\u6587\u4ef6\u4ee5\u53ca\u5220\u9664\u6587\u4ef6\u7b49\u64cd\u4f5c<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>yum<\/p>\n<\/td>\n<td>\n<p>\u670d\u52a1\u5b89\u88c5<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>fetch<\/p>\n<\/td>\n<td>\n<p>\u4ece\u8fdc\u7a0b\u76ee\u6807\u4e3b\u673a\u590d\u5236\u6587\u4ef6\u5230\u672c\u5730<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>cron<\/p>\n<\/td>\n<td>\n<p>crontab\u8ba1\u5212\u4efb\u52a1\u7ba1\u7406<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>service<\/p>\n<\/td>\n<td>\n<p>\u670d\u52a1\u7ba1\u7406<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>user<\/p>\n<\/td>\n<td>\n<p>\u7528\u6237\u7ba1\u7406<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>group<\/p>\n<\/td>\n<td>\n<p>\u7528\u6237\u7ec4\u7ba1\u7406<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>setup<\/p>\n<\/td>\n<td>\n<p>\u4e3b\u8981\u662f\u6536\u96c6\u4fe1\u606f<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>script<\/p>\n<\/td>\n<td>\n<p>\u5c06\u672c\u5730\u811a\u672c\u5728\u76ee\u6807\u8fdc\u7aef\u670d\u52a1\u5668\u4e3b\u673a\u4e0a\u8fd0\u884c<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u793a\u4f8b&#xff1a;\u4f7f\u7528file\u6a21\u5757\u5728\u76ee\u6807\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u521b\u5efa\u6587\u4ef6<br \/>\nansible all -m file -a &#034;path&#061;\/tmp\/tmp.txt state&#061;touch&#034;<\/p>\n<p>[root&#064;node2 ~]# ansible all -m file -a &#034;path&#061;\/tmp\/tmp.txt state&#061;touch&#034;<br \/>\n192.168.10.31 | CHANGED &#061;&gt; {<br \/>\n    &#034;ansible_facts&#034;: {<br \/>\n        &#034;discovered_interpreter_python&#034;: &#034;\/usr\/bin\/python&#034;<br \/>\n    },<br \/>\n    &#034;changed&#034;: true,<br \/>\n    &#034;dest&#034;: &#034;\/tmp\/tmp.txt&#034;,<br \/>\n    &#034;gid&#034;: 0,<br \/>\n    &#034;group&#034;: &#034;root&#034;,<br \/>\n    &#034;mode&#034;: &#034;0644&#034;,<br \/>\n    &#034;owner&#034;: &#034;root&#034;,<br \/>\n    &#034;secontext&#034;: &#034;unconfined_u:object_r:user_tmp_t:s0&#034;,<br \/>\n    &#034;size&#034;: 0,<br \/>\n    &#034;state&#034;: &#034;file&#034;,<br \/>\n    &#034;uid&#034;: 0<br \/>\n}<br \/>\n192.168.10.32 | CHANGED &#061;&gt; {<br \/>\n    &#034;ansible_facts&#034;: {<br \/>\n        &#034;discovered_interpreter_python&#034;: &#034;\/usr\/bin\/python&#034;<br \/>\n    },<br \/>\n    &#034;changed&#034;: true,<br \/>\n    &#034;dest&#034;: &#034;\/tmp\/tmp.txt&#034;,<br \/>\n    &#034;gid&#034;: 0,<br \/>\n    &#034;group&#034;: &#034;root&#034;,<br \/>\n    &#034;mode&#034;: &#034;0644&#034;,<br \/>\n    &#034;owner&#034;: &#034;root&#034;,<br \/>\n    &#034;size&#034;: 0,<br \/>\n    &#034;state&#034;: &#034;file&#034;,<br \/>\n    &#034;uid&#034;: 0<br \/>\n}<br \/>\n192.168.10.30 | CHANGED &#061;&gt; {<br \/>\n    &#034;ansible_facts&#034;: {<br \/>\n        &#034;discovered_interpreter_python&#034;: &#034;\/usr\/bin\/python&#034;<br \/>\n    },<br \/>\n    &#034;changed&#034;: true,<br \/>\n    &#034;dest&#034;: &#034;\/tmp\/tmp.txt&#034;,<br \/>\n    &#034;gid&#034;: 0,<br \/>\n    &#034;group&#034;: &#034;root&#034;,<br \/>\n    &#034;mode&#034;: &#034;0644&#034;,<br \/>\n    &#034;owner&#034;: &#034;root&#034;,<br \/>\n    &#034;size&#034;: 0,<br \/>\n    &#034;state&#034;: &#034;file&#034;,<br \/>\n    &#034;uid&#034;: 0<br \/>\n} <\/p>\n<h3 id=\"QyhF-1741689679752\" style=\"text-align:left\">3.3 \u57fa\u4e8eplaybook\u6a21\u5f0f\u6267\u884c<\/h3>\n<p>   Playbook\u6a21\u5f0f\u662f\u4e00\u79cd\u901a\u8fc7\u7f16\u5199YAML\u683c\u5f0f\u7684\u811a\u672c\u6587\u4ef6&#xff08;\u79f0\u4e3a playbook&#xff09;\u6765\u5b9a\u4e49\u548c\u6267\u884c\u81ea\u52a8\u5316\u4efb\u52a1\u7684\u65b9\u5f0f&#xff0c;playbook\u662f\u7531\u4e00\u4e2a\u6216\u8005\u591a\u4e2aplay\u7ec4\u6210\u7684\u5217\u8868&#xff0c;play\u4e3b\u8981\u529f\u80fd\u662f\u5c06\u4e8b\u5148\u5f52\u5e76\u4e3a\u4e00\u7ec4\u7684\u4e3b\u673a\u88c5\u626e\u6210\u4e8b\u5148\u901a\u8fc7task\u5b9a\u4e49\u597d\u89d2\u8272&#xff0c;task\u5c31\u662f\u8c03\u7528\u7684AnsiBle\u7684\u4e00\u4e2a\u6a21\u5757&#xff08;module&#xff09;\u3002 <\/p>\n<p>  playbook\u7684\u7ec4\u6210\u90e8\u5206&#xff1a; <\/p>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\">hosts&#xff1a;\u7528\u4e8e\u6307\u5b9a\u8981\u6267\u884c\u7684\u6307\u5b9a\u4efb\u52a1\u7684\u4e3b\u673a\u7ec4&#xff0c;\u5176\u53ef\u4ee5\u662f\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u7531\u5192\u53f7\u7ec4\u6210\u7684\u4e3b\u673a\u7ec4<\/li>\n<li style=\"text-align:left\">user&#xff1a;\u6307\u5b9a\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u6267\u884c\u4efb\u52a1\u7684\u7528\u6237&#xff0c;\u4e5f\u53ef\u4ee5\u81ea\u5b9asudo\u7684\u7528\u6237<\/li>\n<li style=\"text-align:left\">variable&#xff1a;\u7528\u4e8e\u5b9a\u4e49playbook\u8fd0\u884c\u65f6\u4f7f\u7528\u7684\u53d8\u91cf<\/li>\n<li style=\"text-align:left\">tasks&#xff1a;\u7528\u4e8e\u5b9a\u4e49\u5728\u8fdc\u7a0b\u76ee\u6807\u4e3b\u673a\u4e0a\u6267\u884c\u7684\u4efb\u52a1\u5217\u8868&#xff0c;\u5305\u62ec<\/li>\n<\/ul>\n<p>   \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 1&#xff09;name&#xff1a;\u4efb\u52a1\u7684\u540d\u79f0&#xff0c;\u5efa\u8bae\u63cf\u8ff0\u4efb\u52a1\u6267\u884c\u6b65\u9aa4 <\/p>\n<p>   \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 2&#xff09;module:options&#xff1a;\u8c03\u7528Ansible\u7684\u6a21\u5757module\u548c\u4f20\u5165\u7684\u53c2\u6570 <\/p>\n<ul style=\"margin-left:0;margin-right:0\">\n<li style=\"text-align:left\">handler&#xff1a;\u7528\u4e8e\u5b9a\u4e49task\u5b8c\u6210\u540e\u9700\u8981\u8c03\u7528\u7684\u4efb\u52a1&#xff0c;\u5305\u62ec<\/li>\n<\/ul>\n<p>   \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 1&#xff09;notify&#xff1a;\u5728\u6bcf\u4e2apaly\u7684\u6700\u540e\u89e6\u53d1&#xff0c;\u8c03\u7528\u5728handler\u4e2d\u5b9a\u4e49\u7684\u64cd\u4f5c <\/p>\n<p>   \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 2&#xff09;handler&#xff1a;task\u7684\u5217\u8868 <\/p>\n<p>playbook\u793a\u4f8b&#xff1a;<br \/>\n&#8212;<br \/>\n&#8211; hosts: web  # \u76ee\u6807\u4e3b\u673a\u7ec4<br \/>\n  become: yes  # \u4f7f\u7528\u63d0\u6743&#xff08;\u5982 sudo&#xff09;<br \/>\n  tasks:<br \/>\n    &#8211; name: Ensure Apache is installed<br \/>\n      yum:<br \/>\n        name: httpd<br \/>\n        state: present<\/p>\n<p>    &#8211; name: Ensure Apache is running<br \/>\n      service:<br \/>\n        name: httpd<br \/>\n        state: started <\/p>\n<h4 id=\"Nz0T-1741689815841\" style=\"text-align:left\">3.3.1 playbook\u6838\u5fc3\u7ec4\u4ef6<\/h4>\n<p>  1. hosts\u00a0 <\/p>\n<p> \u4f5c\u7528&#xff1a;\u6307\u5b9a\u76ee\u6807\u4e3b\u673a\u6216\u4e3b\u673a\u7ec4&#xff08;\u5728Inventory&#xff09;\u4e2d\u5b9a\u4e49<br \/>\n\u793a\u4f8b&#xff1a;<br \/>\nhosts: webserver <\/p>\n<p>  2. tasks\u00a0 <\/p>\n<p> \u4f5c\u7528&#xff1a;\u5b9a\u4e49\u8981\u6267\u884c\u7684\u4efb\u52a1\u5217\u8868<br \/>\n\u6bcf\u4e2a\u4efb\u52a1\u5305\u542b\u5185\u5bb9&#xff1a;<br \/>\nname&#xff1a;\u4efb\u52a1\u63cf\u8ff0&#xff08;\u53ef\u9009&#xff0c;\u63a8\u8350\u4f7f\u7528&#xff09;<br \/>\n\u6a21\u5757\u540d\u79f0&#xff08;\u5982yum\u3001copy\u3001service\u7b49&#xff09;<br \/>\n\u6a21\u5757\u53c2\u6570<\/p>\n<p>\u793a\u4f8b&#xff1a;<br \/>\ntasks:<br \/>\n  &#8211; name: Ensure Apache is installed<br \/>\n    yum:<br \/>\n      name: httpd<br \/>\n      state: present <\/p>\n<p>3. vars\u00a0<\/p>\n<p> \u4f5c\u7528&#xff1a;\u5b9a\u4e49\u53d8\u91cf<br \/>\n\u793a\u4f8b&#xff1a;<br \/>\nvars:<br \/>\n  http_port: 80 <\/p>\n<p>4. handlers<\/p>\n<p> \u4f5c\u7528&#xff1a;\u5b9a\u4e49\u5904\u7406\u5668&#xff0c;\u901a\u5e38\u7528\u4e8e\u91cd\u542f\u670d\u52a1<br \/>\n\u793a\u4f8b&#xff1a;<br \/>\nhandlers:<br \/>\n  &#8211; name: Restart Apache<br \/>\n    service:<br \/>\n      name: httpd<br \/>\n      state: restarted <\/p>\n<p>  5.\u00a0 become <\/p>\n<p> \u4f5c\u7528&#xff1a;\u662f\u5426\u4f7f\u7528\u63d0\u6743&#xff08;\u5982sudo&#xff09;<br \/>\n\u793a\u4f8b&#xff1a;<br \/>\nbecome: yes <\/p>\n<h4 id=\"jVb2-1741689975016\">3.3.2 playbook\u7684\u6267\u884c<\/h4>\n<p> ansible-playbook playbook.yml <\/p>\n<p>\u5e38\u7528\u9009\u9879&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<p>\u9009\u9879<\/p>\n<\/td>\n<td>\n<p>\u63cf\u8ff0<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>-i<\/p>\n<\/td>\n<td>\n<p>\u6307\u5b9a\u81ea\u5b9a\u4e49\u7684Inventory\u6587\u4ef6<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>&#8211;limit<\/p>\n<\/td>\n<td>\n<p>\u9650\u5236\u8fd0\u884c\u7684\u4e3b\u673a\u8303\u56f4<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>&#8211;check<\/p>\n<\/td>\n<td>\n<p>\u6a21\u62df\u8fd0\u884c&#xff08;\u4e0d\u5b9e\u9645\u6267\u884c\u4efb\u4f55\u66f4\u6539&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>&#8211;diff<\/p>\n<\/td>\n<td>\n<p>\u663e\u793a\u6587\u4ef6\u7684\u5dee\u5f02&#xff08;\u9002\u7528\u4e8e\u6587\u4ef6\u76f8\u5173\u6a21\u5757&#xff0c;\u5982copy\u3001template&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>&#8211;tags<\/p>\n<\/td>\n<td>\n<p>\u53ea\u8fd0\u884c\u6307\u5b9a\u6807\u7b7e\u7684\u4efb\u52a1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>&#8211;skip-tags<\/p>\n<\/td>\n<td>\n<p>\u8df3\u8fc7\u6307\u5b9a\u6807\u7b7e\u7684\u4efb\u52a1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>-v<\/p>\n<\/td>\n<td>\n<p>\u8f93\u51fa\u8be6\u7ec6\u4fe1\u606f&#xff08;-vvv\u6216-vvvv\u53ef\u589e\u52a0\u8c03\u8bd5\u4fe1\u606f&#xff09;<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"6dJn-1741690012029\" style=\"text-align:left\">3.3.3 playbook\u9ad8\u7ea7\u7528\u6cd5<\/h4>\n<p>1.\u6761\u4ef6\u5224\u65ad<\/p>\n<p> \u4f7f\u7528when\u5173\u952e\u5b57\u6839\u636e\u6761\u4ef6\u6267\u884c\u4efb\u52a1<br \/>\n\u793a\u4f8b&#xff1a;<br \/>\ntasks:<br \/>\n  &#8211; name: Install Apache on CentOS<br \/>\n    yum:<br \/>\n      name: httpd<br \/>\n      state: present<br \/>\n    when: ansible_os_family &#061;&#061; &#034;RedHat&#034; <\/p>\n<p>2. \u5faa\u73af<\/p>\n<p> \u4f7f\u7528loop\u5173\u952e\u5b57\u5b9e\u73b0\u5faa\u73af<br \/>\n\u793a\u4f8b&#xff1a;<br \/>\ntasks:<br \/>\n  &#8211; name: Ensure users are present<br \/>\n    user:<br \/>\n      name: &#034;{{ item }}&#034;<br \/>\n      state: present<br \/>\n    loop:<br \/>\n      &#8211; alice <\/p>\n<p>3. \u6a21\u7248<\/p>\n<p> \u4f7f\u7528template\u6a21\u5757\u751f\u6210\u52a8\u6001\u914d\u7f6e\u6587\u4ef6<br \/>\n\u793a\u4f8b&#xff1a;<br \/>\ntasks:<br \/>\n  &#8211; name: Configure Apache<br \/>\n    template:<br \/>\n      src: templates\/httpd.conf.j2<br \/>\n      dest: \/etc\/httpd\/httpd.conf <\/p>\n<p>4. \u89d2\u8272<\/p>\n<p> \u5c06Playbook\u5206\u89e3\u4e3a\u53ef\u91cd\u7528\u7684\u89d2\u8272<br \/>\n\u793a\u4f8b&#xff1a;<br \/>\nroles:<br \/>\n  &#8211; common<br \/>\n  &#8211; webserver <\/p>\n<h4 id=\"NcEI-1741690093031\" style=\"background-color:transparent\">3.3.4 \u6848\u4f8b&#xff1a;\u5b89\u88c5httpd<\/h4>\n<p>1.\u521b\u5efaplaybook\u76f8\u5173\u76ee\u5f55<\/p>\n<p> [root&#064;node2 ansible]# mkdir apache-playbook<br \/>\n[root&#064;node2 ansible]# cd apache-playbook<br \/>\n[root&#064;node2 ansible]# mkdir files<br \/>\n[root&#064;node2 ansible]# mkdir templates <\/p>\n<p>2.\u7f16\u8f91Inventory\u6587\u4ef6&#xff0c;\u589e\u52a0\u4e3b\u673a\u7ec4web<\/p>\n<p> [root&#064;node2 ansible]# echo &#034;[web]&#034; &gt;&gt;\/etc\/ansible\/hosts<\/p>\n<p>[root&#064;node2 ansible]# echo &#034;192.168.10.32&#034; &gt;&gt;\/etc\/ansible\/hosts<br \/>\n[root&#064;node2 ansible]#  <\/p>\n<p>3.\u521b\u5efaplalybook.yml\u6587\u4ef6&#xff0c;\u589e\u52a0\u5982\u4e0b\u5185\u5bb9<\/p>\n<p> &#8212;<br \/>\n&#8211; hosts: web  # \u76ee\u6807\u4e3b\u673a\u7ec4<br \/>\n  become: yes  # \u4f7f\u7528\u63d0\u6743&#xff08;\u5982 sudo&#xff09;<br \/>\n  vars:<br \/>\n    http_port: 80  # \u5b9a\u4e49 HTTP \u7aef\u53e3<br \/>\n    html_content: &#034;Hello, Ansible!&#034;  # \u5b9a\u4e49 HTML \u9875\u9762\u5185\u5bb9<\/p>\n<p>  tasks:<br \/>\n    # 1. \u5b89\u88c5 Apache<br \/>\n    &#8211; name: Ensure Apache is installed<br \/>\n      yum:<br \/>\n        name: httpd<br \/>\n        state: present<\/p>\n<p>    # 2. \u914d\u7f6e Apache<br \/>\n    &#8211; name: Configure Apache<br \/>\n      template:<br \/>\n        src: templates\/httpd.conf.j2  # \u6a21\u677f\u6587\u4ef6<br \/>\n        dest: \/etc\/httpd\/conf\/httpd.conf  # \u76ee\u6807\u914d\u7f6e\u6587\u4ef6<br \/>\n      notify: Restart Apache  # \u89e6\u53d1\u5904\u7406\u5668<\/p>\n<p>    # 3. \u90e8\u7f72 HTML \u9875\u9762<br \/>\n    &#8211; name: Deploy HTML page<br \/>\n      copy:<br \/>\n        content: &#034;{{ html_content }}&#034;<br \/>\n        dest: \/var\/www\/html\/index.html<\/p>\n<p>    # 4. \u542f\u52a8 Apache \u5e76\u8bbe\u7f6e\u5f00\u673a\u81ea\u542f<br \/>\n    &#8211; name: Ensure Apache is running and enabled<br \/>\n      service:<br \/>\n        name: httpd<br \/>\n        state: started<br \/>\n        enabled: yes<\/p>\n<p>  handlers:<br \/>\n    # \u5b9a\u4e49\u5904\u7406\u5668&#xff0c;\u7528\u4e8e\u91cd\u542f Apache<br \/>\n    &#8211; name: Restart Apache<br \/>\n      service:<br \/>\n        name: httpd<br \/>\n        state: restarted <\/p>\n<p>4.\u521b\u5efa\u6a21\u7248\u6587\u4ef6&#xff0c;\u5728templates\u76ee\u5f55\u4e0b\u521b\u5efahttpd.conf.j2\u6587\u4ef6&#xff0c;\u5e76\u589e\u52a0\u5982\u4e0b\u5185\u5bb9<\/p>\n<p> # {{ ansible_managed }} &#8211; DO NOT EDIT MANUALLY<\/p>\n<p>Listen {{ http_port }}<\/p>\n<p>&lt;VirtualHost *:{{ http_port }}&gt;<br \/>\n    DocumentRoot &#034;\/var\/www\/html&#034;<br \/>\n    ServerAdmin webmaster&#064;localhost<br \/>\n    ErrorLog \/var\/log\/httpd\/error.log<br \/>\n    CustomLog \/var\/log\/httpd\/access.log combined<br \/>\n&lt;\/VirtualHost&gt; <\/p>\n<p>5.\u8fd0\u884cplaybook<\/p>\n<p> ansible-playbook -i \/etc\/ansible\/hosts playbook.yml<br \/>\n [root&#064;node2 apache-playbook]# ansible-playbook -i \/etc\/ansible\/hosts playbook.yml<\/p>\n<p>PLAY [web] **********************************************************************************************************************************************************************<\/p>\n<p>TASK [Gathering Facts] **********************************************************************************************************************************************************<br \/>\nok: [192.168.10.32]<\/p>\n<p>TASK [Ensure Apache is installed] ***********************************************************************************************************************************************<br \/>\nok: [192.168.10.32]<\/p>\n<p>TASK [Configure Apache] *********************************************************************************************************************************************************<br \/>\nok: [192.168.10.32]<\/p>\n<p>TASK [Deploy HTML page] *********************************************************************************************************************************************************<br \/>\nok: [192.168.10.32]<\/p>\n<p>TASK [Ensure Apache is running and enabled] *************************************************************************************************************************************<br \/>\nok: [192.168.10.32]<\/p>\n<p>PLAY RECAP **********************************************************************************************************************************************************************<br \/>\n192.168.10.32              : ok&#061;5    changed&#061;0    unreachable&#061;0    failed&#061;0    skipped&#061;0    rescued&#061;0    ignored&#061;0   <\/p>\n<p>[root&#064;node2 apache-playbook]#  <\/p>\n<p>6.\u9a8c\u8bc1\u7ed3\u679c<\/p>\n<p> [root&#064;node2 apache-playbook]# curl http:\/\/192.168.10.30<br \/>\nHello, Ansible!<br \/>\n[root&#064;node2 apache-playbook]#  <\/p>\n<p>\u00a0<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.5k\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf15\u6b21\u3002Ansible\u4e3b\u8981\u529f\u80fd\u5728\u4e8e\u6279\u91cf\u4e3b\u673a\u64cd\u4f5c\uff0c\u4e3a\u4e86\u4fbf\u6377\u5730\u4f7f\u7528\u5176\u4e2d\u7684\u90e8\u5206\u4e3b\u673a\uff0c\u53ef\u4ee5\u5728inventory\u4e2d\u5c06\u4e3b\u673a\u8fdb\u884c\u5206\u7ec4\u547d\u540d\u3002\u9ed8\u8ba4\u7684inventory file\u4e3a\/etc\/ansible\/hosts\u3002inventory\u6587\u4ef6\u53ef\u4ee5\u6709\u591a\u4e2a\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7Dynamic Inventory\u6765\u52a8\u6001\u751f\u6210\u3002<\/p>\n","protected":false},"author":2,"featured_media":18976,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1064,131,43,1452,1451,44],"topic":[],"class_list":["post-18978","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-ansible","tag-linux-","tag-43","tag-1452","tag-1451","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Ansible\u8fd0\u884c\u539f\u7406\u63ed\u79d8\uff1a\u5982\u4f55\u7528YAML\u811a\u672c\u638c\u63a7\u6570\u670d\u52a1\u5668\uff1f - \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\/18978.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ansible\u8fd0\u884c\u539f\u7406\u63ed\u79d8\uff1a\u5982\u4f55\u7528YAML\u811a\u672c\u638c\u63a7\u6570\u670d\u52a1\u5668\uff1f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.5k\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf15\u6b21\u3002Ansible\u4e3b\u8981\u529f\u80fd\u5728\u4e8e\u6279\u91cf\u4e3b\u673a\u64cd\u4f5c\uff0c\u4e3a\u4e86\u4fbf\u6377\u5730\u4f7f\u7528\u5176\u4e2d\u7684\u90e8\u5206\u4e3b\u673a\uff0c\u53ef\u4ee5\u5728inventory\u4e2d\u5c06\u4e3b\u673a\u8fdb\u884c\u5206\u7ec4\u547d\u540d\u3002\u9ed8\u8ba4\u7684inventory file\u4e3a\/etc\/ansible\/hosts\u3002inventory\u6587\u4ef6\u53ef\u4ee5\u6709\u591a\u4e2a\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7Dynamic Inventory\u6765\u52a8\u6001\u751f\u6210\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/18978.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T17:22:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418172209-68028a4196d20.png\" \/>\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\/18978.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/18978.html\",\"name\":\"Ansible\u8fd0\u884c\u539f\u7406\u63ed\u79d8\uff1a\u5982\u4f55\u7528YAML\u811a\u672c\u638c\u63a7\u6570\u670d\u52a1\u5668\uff1f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T17:22:10+00:00\",\"dateModified\":\"2025-04-18T17:22:10+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/18978.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/18978.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/18978.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ansible\u8fd0\u884c\u539f\u7406\u63ed\u79d8\uff1a\u5982\u4f55\u7528YAML\u811a\u672c\u638c\u63a7\u6570\u670d\u52a1\u5668\uff1f\"}]},{\"@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\u8fd0\u884c\u539f\u7406\u63ed\u79d8\uff1a\u5982\u4f55\u7528YAML\u811a\u672c\u638c\u63a7\u6570\u670d\u52a1\u5668\uff1f - \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\/18978.html","og_locale":"zh_CN","og_type":"article","og_title":"Ansible\u8fd0\u884c\u539f\u7406\u63ed\u79d8\uff1a\u5982\u4f55\u7528YAML\u811a\u672c\u638c\u63a7\u6570\u670d\u52a1\u5668\uff1f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.5k\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf15\u6b21\u3002Ansible\u4e3b\u8981\u529f\u80fd\u5728\u4e8e\u6279\u91cf\u4e3b\u673a\u64cd\u4f5c\uff0c\u4e3a\u4e86\u4fbf\u6377\u5730\u4f7f\u7528\u5176\u4e2d\u7684\u90e8\u5206\u4e3b\u673a\uff0c\u53ef\u4ee5\u5728inventory\u4e2d\u5c06\u4e3b\u673a\u8fdb\u884c\u5206\u7ec4\u547d\u540d\u3002\u9ed8\u8ba4\u7684inventory file\u4e3a\/etc\/ansible\/hosts\u3002inventory\u6587\u4ef6\u53ef\u4ee5\u6709\u591a\u4e2a\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7Dynamic Inventory\u6765\u52a8\u6001\u751f\u6210\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/18978.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T17:22:10+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418172209-68028a4196d20.png"}],"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\/18978.html","url":"https:\/\/www.wsisp.com\/helps\/18978.html","name":"Ansible\u8fd0\u884c\u539f\u7406\u63ed\u79d8\uff1a\u5982\u4f55\u7528YAML\u811a\u672c\u638c\u63a7\u6570\u670d\u52a1\u5668\uff1f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T17:22:10+00:00","dateModified":"2025-04-18T17:22:10+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/18978.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/18978.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/18978.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Ansible\u8fd0\u884c\u539f\u7406\u63ed\u79d8\uff1a\u5982\u4f55\u7528YAML\u811a\u672c\u638c\u63a7\u6570\u670d\u52a1\u5668\uff1f"}]},{"@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\/18978","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=18978"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/18978\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/18976"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=18978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=18978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=18978"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=18978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}