{"id":776,"date":"2018-04-17T14:15:55","date_gmt":"2018-04-17T06:15:55","guid":{"rendered":"https:\/\/blog.jsjs.org\/?p=776"},"modified":"2018-04-17T14:15:55","modified_gmt":"2018-04-17T06:15:55","slug":"%e4%bc%81%e4%b8%9a%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4ansible","status":"publish","type":"post","link":"https:\/\/blog.jsjs.org\/?p=776","title":{"rendered":"\u4f01\u4e1a\u81ea\u52a8\u5316\u8fd0\u7ef4ansible"},"content":{"rendered":"<p>https:\/\/blog.csdn.net\/itlinuxp\/article\/details\/79561184<\/p>\n<h1 id=\"\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177ansible\">\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177ansible<\/h1>\n<ul>\n<li>\u8fd0\u7ef4\u81ea\u52a8\u5316\u53d1\u5c55\u5386\u7a0b\u53ca\u6280\u672f\u5e94\u7528<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180112\/ad6Iid0Ikl.png\" alt=\"mark\" \/><\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180112\/8kAL59lcl3.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<h4 id=\"\u4e91\u8ba1\u7b97\u5de5\u7a0b\u5e08\u6838\u5fc3\u804c\u80fd\">\u4e91\u8ba1\u7b97\u5de5\u7a0b\u5e08\u6838\u5fc3\u804c\u80fd<\/h4>\n<ul>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180112\/5lgjhGi21H.png\" alt=\"mark\" \/><\/li>\n<li>Linux\u8fd0\u7ef4\u5de5\u7a0b\u5e08\u804c\u80fd\u5212\u5206<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180112\/EK3dj8h0DG.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<h2 id=\"\u81ea\u52a8\u5316\u52a8\u7ef4\u5e94\u7528\u573a\u666f\"><a name=\"t1\"><\/a>\u81ea\u52a8\u5316\u52a8\u7ef4\u5e94\u7528\u573a\u666f<\/h2>\n<ul>\n<li>\u6587\u4ef6\u4f20\u8f93<\/li>\n<li>\u547d\u4ee4\u6267\u884c\n<ul>\n<li>\u5e94\u7528\u90e8\u7f72<\/li>\n<li>\u914d\u7f6e\u7ba1\u7406<\/li>\n<li>\u4efb\u52a1\u6d41\u7f16\u6392<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3 id=\"\u4f01\u4e1a\u5b9e\u9645\u5e94\u7528\u573a\u666f\u5206\u6790\"><a name=\"t2\"><\/a>\u4f01\u4e1a\u5b9e\u9645\u5e94\u7528\u573a\u666f\u5206\u6790<\/h3>\n<blockquote><p>1 Dev\u5f00\u53d1\u73af\u5883<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\u4f7f\u7528\u8005\uff1a\u7a0b\u5e8f\u5458\n\u529f\u80fd\uff1a\u7a0b\u5e8f\u5458\u5f00\u53d1\u8f6f\u4ef6\uff0c\u6d4b\u8bd5BUG\u7684\u73af\u5883\n\u7ba1\u7406\u8005\uff1a\u7a0b\u5e8f\u5458<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<\/ul>\n<blockquote><p>2 \u6d4b\u8bd5\u73af\u5883<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\u4f7f\u7528\u8005\uff1aQA\u6d4b\u8bd5\u5de5\u7a0b\u5e08\n\u529f\u80fd\uff1a\u6d4b\u8bd5\u7ecf\u8fc7Dev\u73af\u5883\u6d4b\u8bd5\u901a\u8fc7\u7684\u8f6f\u4ef6\u7684\u529f\u80fd\n\u7ba1\u7406\u8005\uff1a\u8fd0\u7ef4\n\u8bf4\u660e\uff1a\u6d4b\u8bd5\u73af\u5883\u5f80\u5f80\u6709\u591a\u5957,\u6d4b\u8bd5\u73af\u5883\u6ee1\u8db3\u6d4b\u8bd5\u529f\u80fd\u5373\u53ef\uff0c\u4e0d\u5b9c\u8fc7\u591a\n\uff08<span class=\"hljs-number\">1<\/span>\uff09\u6d4b\u8bd5\u4eba\u5458\u5e0c\u671b\u6d4b\u8bd5\u73af\u5883\u6709\u591a\u5957,\u516c\u53f8\u7684\u4ea7\u54c1\u591a\u4ea7\u54c1\u7ebf\u5e76\u53d1\uff0c\u5373\u591a\u4e2a\u7248\u672c\uff0c\u610f\u5473\u7740\u591a\u4e2a\u7248\u672c\u540c\u6b65\u6d4b\u8bd5\n\uff08<span class=\"hljs-number\">2<\/span>\uff09\u901a\u5e38\u6d4b\u8bd5\u73af\u5883\u6709\u591a\u5c11\u5957\u548c\u4ea7\u54c1\u7ebf\u6570\u91cf\u4fdd\u6301\u4e00\u6837<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<\/ul>\n<blockquote><p>3 \u53d1\u5e03\u73af\u5883\uff1a\u4ee3\u7801\u53d1\u5e03\u673a\uff0c\u6709\u4e9b\u516c\u53f8\u4e3a\u5821\u5792\u673a\uff08\u5b89\u5168\u5c4f\u969c\uff09<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\u4f7f\u7528\u8005\uff1a\u8fd0\u7ef4\n\u529f\u80fd\uff1a\u53d1\u5e03\u4ee3\u7801\u81f3\u751f\u4ea7\u73af\u5883\n\u7ba1\u7406\u8005\uff1a\u8fd0\u7ef4\uff08\u6709\u7ecf\u9a8c\uff09\n\u53d1\u5e03\u673a\uff1a\u5f80\u5f80\u9700\u8981\u6709<span class=\"hljs-number\">2<\/span>\u53f0\uff08\u4e3b\u5907\uff09<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<\/ul>\n<blockquote><p>4 \u751f\u4ea7\u73af\u5883<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\u4f7f\u7528\u8005\uff1a\u8fd0\u7ef4\uff0c\u5c11\u6570\u60c5\u51b5\u5f00\u653e\u6743\u9650\u7ed9\u6838\u5fc3\u5f00\u53d1\u4eba\u5458\uff0c\u6781\u5c11\u6570\u516c\u53f8\u5c06\u6743\u9650\u5b8c\u5168\u5f00\u653e\u7ed9\u5f00\u53d1\u4eba\u5458\u5e76\u5176\u7ef4\u62a4\n\u529f\u80fd\uff1a\u5bf9\u7528\u6237\u63d0\u4f9b\u516c\u53f8\u4ea7\u54c1\u7684\u670d\u52a1\n\u7ba1\u7406\u8005\uff1a\u53ea\u80fd\u662f\u8fd0\u7ef4\n\u751f\u4ea7\u73af\u5883\u670d\u52a1\u5668\u6570\u91cf\uff1a\u4e00\u822c\u6bd4\u8f83\u591a\uff0c\u4e14\u5e94\u7528\u975e\u5e38\u91cd\u8981\u3002\u5f80\u5f80\u9700\u8981\u81ea\u52a8\u5de5\u5177\u534f\u52a9\u90e8\u7f72\u914d\u7f6e\u5e94\u7528<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<\/ul>\n<blockquote><p>5 \u7070\u5ea6\u73af\u5883\uff08\u751f\u4ea7\u73af\u5883\u7684\u4e00\u90e8\u5206\uff09<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\u4f7f\u7528\u8005\uff1a\u8fd0\u7ef4\n\u529f\u80fd\uff1a\u5728\u5168\u91cf\u53d1\u5e03\u4ee3\u7801\u524d\u5c06\u4ee3\u7801\u7684\u529f\u80fd\u9762\u5411\u5c11\u91cf\u7cbe\u51c6\u7528\u6237\u53d1\u5e03\u7684\u73af\u5883,\u53ef\u57fa\u4e8e\u4e3b\u673a\u6216\u7528\u6237\u6267\u884c\u7070\u5ea6\u53d1\u5e03\n\u6848\u4f8b\uff1a\u5171<span class=\"hljs-number\">100<\/span>\u53f0\u751f\u4ea7\u670d\u52a1\u5668\uff0c\u5148\u53d1\u5e03\u5176\u4e2d\u7684<span class=\"hljs-number\">10<\/span>\u53f0\u670d\u52a1\u5668\uff0c\u8fd9<span class=\"hljs-number\">10<\/span>\u53f0\u670d\u52a1\u5668\u5c31\u662f\u7070\u5ea6\u670d\u52a1\u5668\n\u7ba1\u7406\u8005\uff1a\u8fd0\u7ef4\n\u7070\u5ea6\u73af\u5883\uff1a\u5f80\u5f80\u8be5\u7248\u672c\u529f\u80fd\u53d8\u66f4\u8f83\u5927\uff0c\u4e3a\u4fdd\u9669\u8d77\u89c1\u7279\u610f\u5148\u8ba9\u4e00\u90e8\u5206\u7528\u6237\u4f18\u5316\u4f53\u9a8c\u8be5\u529f\u80fd\uff0c\u5f85\u8fd9\u90e8\u5206\u7528\u6237\u4f7f\u7528\u6ca1\u6709\u91cd\u5927\u95ee\u9898\u7684\u65f6\u5019\uff0c\u518d\u5168\u91cf\u53d1\u5e03\u81f3\u6240\u6709\u670d\u52a1\u5668<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<\/ul>\n<h3 id=\"\u7a0b\u5e8f\u53d1\u5e03\"><a name=\"t3\"><\/a>\u7a0b\u5e8f\u53d1\u5e03<\/h3>\n<ul>\n<li>\u9884\u53d1\u5e03\u9a8c\u8bc1\n<ul>\n<li>\u65b0\u7248\u672c\u7684\u4ee3\u7801\u5148\u53d1\u5e03\u5230\u670d\u52a1\u5668\uff08\u8ddf\u7ebf\u4e0a\u73af\u5883\u914d\u7f6e\u5b8c\u5168\u76f8\u540c\uff0c\u53ea\u662f\u672a\u63a5\u5165\u5230\u8c03\u5ea6\u5668\uff09<\/li>\n<\/ul>\n<\/li>\n<li>\u7a0b\u5e8f\u53d1\u5e03\uff1a\n<ul>\n<li>\u4e0d\u80fd\u5bfc\u81f4\u7cfb\u7edf\u6545\u969c\u6216\u9020\u6210\u7cfb\u7edf\u5b8c\u5168\u4e0d\u53ef\u7528<\/li>\n<li>\u4e0d\u80fd\u5f71\u54cd\u7528\u6237\u4f53\u9a8c<\/li>\n<\/ul>\n<\/li>\n<li>\u7070\u5ea6\u53d1\u5e03\uff1a<\/li>\n<li>\u53d1\u5e03\u8def\u5f84\uff1a<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\u628a\u539f\u6765\u7684\u65e7\u7248\u672c\u4fdd\u7559\n\/webapp\/tuangou-<span class=\"hljs-number\">1.1<\/span>\n\/webapp\/tuangou\n\/webapp\/tuangou-<span class=\"hljs-number\">1.2<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<\/ul>\n<ul>\n<li>\u53d1\u5e03\u8fc7\u7a0b\uff1a\u5728\u8c03\u5ea6\u5668\u4e0a\u4e0b\u7ebf\u4e00\u6279\u4e3b\u673a(\u6807\u8bb0\u4e3amaintanance\u72b6\u6001) \u2013&gt; \u5173\u95ed\u670d\u52a1 \u2013&gt; \u90e8\u7f72\u65b0\u7248\u672c\u7684\u5e94\u7528\u7a0b\u5e8f \u2013&gt; \u542f\u52a8\u670d\u52a1 \u2013&gt; \u5728\u8c03\u5ea6\u5668\u4e0a\u542f\u7528\u8fd9\u4e00\u6279\u670d\u52a1\u5668<\/li>\n<li>\u81ea\u52a8\u5316\u7070\u5ea6\u53d1\u5e03\uff1a\u811a\u672c\u3001\u53d1\u5e03\u5e73\u53f0<\/li>\n<\/ul>\n<h2 id=\"\u5e38\u7528\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177\"><a name=\"t4\"><\/a>\u5e38\u7528\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177<\/h2>\n<ul>\n<li>Ansible:python,Agentless,\u4e2d\u5c0f\u578b\u5e94\u7528\u73af\u5883\uff08\u4e0d\u9700\u8981\u5728\u5ba2\u6237\u7aef\u5b89\u88c5\u4ee3\u7406\u7a0b\u5e8f\uff0c\u57fa\u4e8essh\u6765\u7ba1\u7406\uff0c300-500\u53f0\u670d\u52a1\u5668\uff09<\/li>\n<li>Saltstack:python\uff0c\u4e00\u822c\u9700\u90e8\u7f72agent\uff0c\u6267\u884c\u6548\u7387\u66f4\u9ad8<\/li>\n<li>Puppet:ruby, \u529f\u80fd\u5f3a\u5927,\u914d\u7f6e\u590d\u6742\uff0c\u91cd\u578b,\u9002\u5408\u5927\u578b\u73af\u5883<\/li>\n<li>Fabric\uff1apython\uff0cagentless<\/li>\n<li>Chef: ruby,\u56fd\u5185\u5e94\u7528\u5c11<\/li>\n<li>Cfengine<\/li>\n<li>func<\/li>\n<\/ul>\n<h2 id=\"\u4f01\u4e1a\u7ea7\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177\u5e94\u7528\u5b9e\u6218ansible\"><a name=\"t5\"><\/a>\u4f01\u4e1a\u7ea7\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177\u5e94\u7528\u5b9e\u6218ansible<\/h2>\n<ul>\n<li>\u516c\u53f8\u8ba1\u5212\u5728\u5e74\u5e95\u505a\u4e00\u6b21\u5927\u578b\u5e02\u573a\u4fc3\u9500\u6d3b\u52a8\uff0c\u5168\u9762\u51b2\u523a\u4e0b\u4ea4\u6613\u989d\uff0c\u4e3a\u660e\u5e74\u7684\u4e0a\u5e02\u505a\u51c6\u5907\u3002\u516c\u53f8\u8981\u6c42\u5404\u4e1a\u52a1\u7ec4\u5bf9\u5e74\u5e95\u5927\u4fc3\u505a\u51c6\u5907\uff0c\u8fd0\u7ef4\u90e8\u8981\u6c42\u6240\u6709\u4e1a\u52a1\u5bb9\u91cf\u8fdb\u884c\u4e09\u500d\u7684\u6269\u5bb9\uff0c\u5e76\u642d\u5efa\u51fa\u591a\u5957\u73af\u5883\u53ef\u4ee5\u5171\u5f00\u53d1\u548c\u6d4b\u8bd5\u4eba\u5458\u505a\u6d4b\u8bd5\uff0c\u8fd0\u7ef4\u8001\u5927\u4e3a\u4e86\u5728\u5e74\u5e95\u6709\u6240\u8868\u73b0\uff0c\u8981\u6c42\u8fd0\u7ef4\u90e8\u95e8\u540c\u5b66\u5c3d\u5feb\u5b9e\u73b0\uff0c\u5f53\u4f60\u63a5\u5230\u8fd9\u4e2a\u4efb\u52a1\u65f6\uff0c\u6709\u6ca1\u6709\u66f4\u5feb\u7684\u89e3\u51b3\u65b9\u6848?<\/li>\n<\/ul>\n<h3 id=\"ansible\u53d1\u5c55\u53f2\"><a name=\"t6\"><\/a>Ansible\u53d1\u5c55\u53f2<\/h3>\n<ul>\n<li>Ansible\n<ul>\n<li>\u521b\u59cb\u4eba\uff0cMichael DeHaan\uff08 Cobbler \u4e0e Func \u7684\u4f5c\u8005\uff09<\/li>\n<li>2012-03-09\uff0c\u53d1\u5e030.0.1\u7248\uff0c\u7ea2\u5e3d\u6536\u8d2d<\/li>\n<li>2015-10-17\uff0cRed Hat\u5ba3\u5e03\u6536\u8d2d<\/li>\n<\/ul>\n<\/li>\n<li>\u540c\u7c7b\u81ea\u52a8\u5316\u5de5\u5177 GitHub\u5173\u6ce8\u7a0b\u5ea6\uff08 2016- &#8211; 07- &#8211; 10\uff09<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180112\/463BIkeeKL.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<h2 id=\"\u7279\u6027\"><a name=\"t7\"><\/a>\u7279\u6027<\/h2>\n<ul>\n<li>\u6a21\u5757\u5316\uff1a\u8c03\u7528\u7279\u5b9a\u7684\u6a21\u5757\uff0c\u5b8c\u6210\u7279\u5b9a\u4efb\u52a1<\/li>\n<li>\u6709Paramiko\uff0cPyYAML\uff0cJinja2\uff08\u6a21\u677f\u8bed\u8a00\uff09\u4e09\u4e2a\u5173\u952e\u6a21\u5757<\/li>\n<li>\u652f\u6301\u81ea\u5b9a\u4e49\u6a21\u5757<\/li>\n<li>\u57fa\u4e8ePython\u8bed\u8a00\u5b9e\u73b0<\/li>\n<li>\u90e8\u7f72\u7b80\u5355\uff0c\u57fa\u4e8epython\u548cSSH(\u9ed8\u8ba4\u5df2\u5b89\u88c5)\uff0cagentless<\/li>\n<li>\u5b89\u5168\uff0c\u57fa\u4e8eOpenSSH<\/li>\n<li>\u652f\u6301playbook\u7f16\u6392\u4efb\u52a1<\/li>\n<li>\u5e42\u7b49\u6027\uff1a\u4e00\u4e2a\u4efb\u52a1\u6267\u884c1\u904d\u548c\u6267\u884cn\u904d\u6548\u679c\u4e00\u6837\uff0c\u4e0d\u56e0\u91cd\u590d\u6267\u884c\u5e26\u6765\u610f\u5916\u60c5\u51b5<\/li>\n<li>\u65e0\u9700\u4ee3\u7406\u4e0d\u4f9d\u8d56PKI\uff08\u65e0\u9700ssl\uff09<\/li>\n<li>\u53ef\u4f7f\u7528\u4efb\u4f55\u7f16\u7a0b\u8bed\u8a00\u5199\u6a21\u5757<\/li>\n<li>YAML\u683c\u5f0f\uff0c\u7f16\u6392\u4efb\u52a1\uff0c\u652f\u6301\u4e30\u5bcc\u7684\u6570\u636e\u7ed3\u6784<\/li>\n<li>\u8f83\u5f3a\u5927\u7684\u591a\u5c42\u89e3\u51b3\u65b9\u6848<\/li>\n<\/ul>\n<h3 id=\"ansible\u67b6\u6784\"><a name=\"t8\"><\/a>ansible\u67b6\u6784<\/h3>\n<p>&#8211;<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180112\/FFJgG64FLk.png\" alt=\"mark\" \/><br \/>\n&#8211; ansible\u662f\u57fa\u4e8ekey\u7684\uff0c\u6240\u4ee5\u9700\u8981\u5c06\u4e3b\u673a\u95f4\u5b9e\u73b0key\u9a8c\u8bc1\u3002\u5f53\u7136\u7528\u5bc6\u7801\u4e5f\u53ef\u4ee5\uff0c\u53ea\u662f\u6bd4\u8f83\u9ebb\u70e6<\/p>\n<h3 id=\"ansible\u5de5\u4f5c\u539f\u7406\"><a name=\"t9\"><\/a>Ansible\u5de5\u4f5c\u539f\u7406<\/h3>\n<ul>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180112\/JBbfja3m3B.png\" alt=\"mark\" \/>\n<ol>\n<li>\u76f4\u63a5\u4f7f\u7528ansible\u547d\u4ee4\u6765\u6267\u884c\u4e00\u6761\u547d\u4ee4<\/li>\n<li>\u5f53\u751f\u4ea7\u73af\u5883\u7a33\u5b9a\u540e\u53ef\u4ee5\u7528ansible\u7f16\u5199playboot\u811a\u672c\u6765\u6267\u884c<\/li>\n<li>\u53ef\u4ee5\u4f7f\u7528\u516c\u6709\u4e91\u6216\u79c1\u6709\u4e91<\/li>\n<li>\u53ef\u4ee5\u4f7f\u7528CMDB\uff0c\u914d\u7f6e\u7ba1\u7406\u6570\u636e\u5e93<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h3 id=\"ansible\u4e3b\u8981\u7ec4\u6210\u90e8\u5206\"><a name=\"t10\"><\/a>Ansible\u4e3b\u8981\u7ec4\u6210\u90e8\u5206<\/h3>\n<ul>\n<li>ANSIBLE PLAYBOOKS\uff1a\u4efb\u52a1\u5267\u672c\uff08\u4efb\u52a1\u96c6\uff09\uff0c\u7f16\u6392\u5b9a\u4e49Ansible\u4efb\u52a1\u96c6\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u7531Ansible\u987a\u5e8f\u4f9d\u6b21\u6267\u884c\uff0c\u901a\u5e38\u662fJSON\u683c\u5f0f\u7684YML\u6587\u4ef6<\/li>\n<li>INVENTORY\uff1aAnsible\u7ba1\u7406\u4e3b\u673a\u7684\u6e05\u5355\/etc\/anaible\/hosts<\/li>\n<li>MODULES\uff1aAnsible\u6267\u884c\u547d\u4ee4\u7684\u529f\u80fd\u6a21\u5757\uff0c\u591a\u6570\u4e3a\u5185\u7f6e\u7684\u6838\u5fc3\u6a21\u5757\uff0c\u4e5f\u53ef\u81ea\u5b9a\u4e49<\/li>\n<li>PLUGINS\uff1a\u6a21\u5757\u529f\u80fd\u7684\u8865\u5145\uff0c\u5982\u8fde\u63a5\u7c7b\u578b\u63d2\u4ef6\u3001\u5faa\u73af\u63d2\u4ef6\u3001\u53d8\u91cf\u63d2\u4ef6\u3001\u8fc7\u6ee4\u63d2\u4ef6\u7b49\uff0c\u8be5\u529f\u80fd\u4e0d\u5e38\u7528<\/li>\n<li>API\uff1a\u4f9b\u7b2c\u4e09\u65b9\u7a0b\u5e8f\u8c03\u7528\u7684\u5e94\u7528\u7a0b\u5e8f\u7f16\u7a0b\u63a5\u53e3<\/li>\n<li>ANSIBLE\uff1a\u7ec4\u5408INVENTORY\u3001API\u3001MODULES\u3001PLUGINS\u7684\u7eff\u6846\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u662fansible\u547d\u4ee4\u5de5\u5177\uff0c\u5176\u4e3a\u6838\u5fc3\u6267\u884c\u5de5\u5177<\/li>\n<li>Ansible\u547d\u4ee4\u6267\u884c\u6765\u6e90\uff1a\n<ul>\n<li>USER\uff0c\u666e\u901a\u7528\u6237\uff0c\u5373SYSTEM ADMINISTRATOR<\/li>\n<li>CMDB\uff08\u8d44\u4ea7\u7ba1\u7406\u7cfb\u7edf\uff09 API \u8c03\u7528<\/li>\n<li>PUBLIC\/PRIVATE CLOUD API\u8c03\u7528<\/li>\n<li>USER-&gt; Ansible Playbook -&gt; Ansibile<\/li>\n<\/ul>\n<\/li>\n<li>\u5229\u7528ansible\u5b9e\u73b0\u7ba1\u7406\u7684\u65b9\u5f0f\uff1a\n<ul>\n<li>Ad-Hoc \u5373ansible\u547d\u4ee4\uff0c\u4e3b\u8981\u7528\u4e8e\u4e34\u65f6\u547d\u4ee4\u4f7f\u7528\u573a\u666f<\/li>\n<li>Ansible-playbook \u4e3b\u8981\u7528\u4e8e\u957f\u671f\u89c4\u5212\u597d\u7684\uff0c\u5927\u578b\u9879\u76ee\u7684\u573a\u666f\uff0c\u9700\u8981\u6709\u524d\u63d0\u7684\u89c4\u5212<br \/>\n-Ansible-playbook\uff08\u5267\u672c\uff09\u6267\u884c\u8fc7\u7a0b\uff1a<\/li>\n<li>\u5c06\u5df2\u6709\u7f16\u6392\u597d\u7684\u4efb\u52a1\u96c6\u5199\u5165Ansible-Playbook<\/li>\n<li>\u901a\u8fc7ansible-playbook\u547d\u4ee4\u5206\u62c6\u4efb\u52a1\u96c6\u81f3\u9010\u6761ansible\u547d\u4ee4\uff0c\u6309\u9884\u5b9a\u89c4\u5219\u9010\u6761\u6267\u884c<\/li>\n<\/ul>\n<\/li>\n<li>Ansible\u4e3b\u8981\u64cd\u4f5c\u5bf9\u8c61\uff1a\n<ul>\n<li>HOSTS\u4e3b\u673a<\/li>\n<li>NETWORKING\u7f51\u7edc\u8bbe\u5907<\/li>\n<\/ul>\n<\/li>\n<li>\u6ce8\u610f\u4e8b\u9879\n<ul>\n<li>\u6267\u884cansible\u7684\u4e3b\u673a\u4e00\u822c\u79f0\u4e3a\u4e3b\u63a7\u7aef\uff0c\u4e2d\u63a7\uff0cmaster\u6216\u5821\u5792\u673a<\/li>\n<li>\u4e3b\u63a7\u7aefPython\u7248\u672c\u9700\u89812.6\u6216\u4ee5\u4e0a<\/li>\n<li>\u88ab\u63a7\u7aefPython\u7248\u672c\u5c0f\u4e8e2.4\u9700\u8981\u5b89\u88c5python-simplejson<\/li>\n<li>\u88ab\u63a7\u7aef\u5982\u5f00\u542fSELinux\u9700\u8981\u5b89\u88c5libselinux-python<\/li>\n<li>windows\u4e0d\u80fd\u505a\u4e3a\u4e3b\u63a7\u7aef<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2 id=\"\u5b89\u88c5\"><a name=\"t11\"><\/a>\u5b89\u88c5<\/h2>\n<blockquote><p>rpm\u5305\u5b89\u88c5: EPEL\u6e90<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">yum install ansible\nansible\u4e0d\u662f\u4e00\u4e2a\u670d\u52a1\/etc\/hosts\u662f\u6700\u91cd\u8981\u7684\u6587\u4ef6\nansible\u662f\u4e00\u4e2a\u7ba1\u7406\u7aef<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<\/ul>\n<blockquote><p>\u7f16\u8bd1\u5b89\u88c5<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">yum -y install python-jinja2 PyYAML python-paramiko\npython-babel python-crypto\ntar xf ansible-<span class=\"hljs-number\">1.5<\/span>.<span class=\"hljs-number\">4<\/span>.tar.gz\n<span class=\"hljs-built_in\">cd<\/span> ansible-<span class=\"hljs-number\">1.5<\/span>.<span class=\"hljs-number\">4<\/span>\npython setup.py build\npython setup.py install\nmkdir \/etc\/ansible\ncp -r examples\/* \/etc\/ansible<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<\/ul>\n<blockquote><p>Git\u65b9\u5f0f<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">git clone git:\/\/github.com\/ansible\/ansible.git --\nrecursive<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<p>cd .\/ansible<br \/>\nsource .\/hacking\/env-setup<\/p>\n<blockquote><p>pip\u5b89\u88c5\uff1a pip\u662f\u5b89\u88c5Python\u5305\u7684\u7ba1\u7406\u5668\uff0c\u7c7b\u4f3cyum<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">yum install python-pip python-devel\nyum install gcc glibc-devel zibl-devel rpm-bulid openssl-devel\npip install --upgrade pip\npip install ansible --upgrade<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<\/ul>\n<ul>\n<li>\u786e\u8ba4\u5b89\u88c5\uff1a ansible \u2013version<\/li>\n<\/ul>\n<h3 id=\"\u76f8\u5173\u6587\u4ef6\"><a name=\"t12\"><\/a>\u76f8\u5173\u6587\u4ef6<\/h3>\n<blockquote><p>\u914d\u7f6e\u6587\u4ef6<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\/etc\/ansible\/ansible.cfg \u4e3b\u914d\u7f6e\u6587\u4ef6\uff0c\u914d\u7f6eansible\u5de5\u4f5c\u7279\u6027\n\/etc\/ansible\/hosts \u4e3b\u673a\u6e05\u5355\u88ab\u7ba1\u7406\u7aef\u7684\u4e3b\u673a\u5b58\u653e\uff0c\u5982\u679c\u5c06\u4e3b\u673a\u4e0d\u5199\u5165\u8fd9\u4e2a\u6587\u4ef6\u5219\u65e0\u6cd5\u7ba1\u7406\u8fd9\u4e2a\u4e3b\u673a\n\/etc\/ansible\/roles\/ \u5b58\u653e\u89d2\u8272\u7684\u76ee\u5f55<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<\/ul>\n<ul>\n<li>\u5982\u679c\u8981\u7ba1\u7406\u7684ip\u6ca1\u52a0\u5165\u5230hosts\u8fd9\u6587\u4ef6\u4e2d\u5219\u4f1a\u51fa\u73b0\u8fd9\u79cd\u9519\u8bef<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@centos7 yum.repos.d]<span class=\"hljs-comment\"># ansible 192.168.27.101 -m ping<\/span>\n [WARNING]: Could not match supplied host pattern, ignoring: all\n\n [WARNING]: provided hosts list is empty, only localhost is available\n\n [WARNING]: Could not match supplied host pattern, ignoring: <span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span>\n\n [WARNING]: No hosts matched, nothing to <span class=\"hljs-keyword\">do<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<\/ul>\n<ul>\n<li>\u5c06\u4e3b\u673a\u52a0\u5165\u5230hosts\u6587\u4ef6\u4e2d\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5c06\u8981\u7ba1\u7406\u7684\u4e3b\u673aip\u5199\u8fdb\u53bb\uff0c\u63a8\u8350\uff0c\u7528\u5206\u7c7b\u7684\u65b9\u5f0f<br \/>\n&#8211;<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/CAg4652ElI.png\" alt=\"mark\" \/><\/li>\n<li>\u5206\u7c7b\u7684\u65b9\u5f0f<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@centos7 yum.repos.d]<span class=\"hljs-comment\"># vim \/etc\/ansible\/hosts <\/span>\n<span class=\"hljs-comment\">#   - Blank lines are ignored<\/span>\n<span class=\"hljs-comment\">#   - Groups of hosts are delimited by [header] elements<\/span>\n<span class=\"hljs-comment\">#   - You can enter hostnames or ip addresses<\/span>\n<span class=\"hljs-comment\">#   - A hostname\/ip can be a member of multiple groups<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27<\/span>.[<span class=\"hljs-number\">101<\/span>:<span class=\"hljs-number\">103<\/span>]   \u4e5f\u53ef\u4ee5\u5199\u6210\u8fd9\u79cd\u683c\u5f0f\u8fd9\u8868\u793a<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span>\u5230<span class=\"hljs-number\">103<\/span>\u76f4\u63a5\u7684ip\n\n\n<span class=\"hljs-comment\"># Ex 1: Ungrouped hosts, specify before any group headers.<\/span>\n\n<span class=\"hljs-comment\">## green.example.com<\/span>\n<span class=\"hljs-comment\">## blue.example.com<\/span>\n<span class=\"hljs-comment\">## 192.168.100.1<\/span>\n<span class=\"hljs-comment\">## 192.168.100.10<\/span>\n\n<span class=\"hljs-comment\"># Ex 2: A collection of hosts belonging to the 'webservers' group<\/span>\n\n<span class=\"hljs-comment\">## [webservers]<\/span>\n<span class=\"hljs-comment\">## alpha.example.org<\/span>\n<span class=\"hljs-comment\">## beta.example.org<\/span>\n<span class=\"hljs-comment\">## 192.168.1.100<\/span>\n<span class=\"hljs-comment\">## 192.168.1.110<\/span>\n\n<span class=\"hljs-comment\"># If you have multiple hosts following a pattern you can specify<\/span>\n<span class=\"hljs-comment\"># them like this:<\/span>\n\n<span class=\"hljs-comment\">## www[001:006].example.com<\/span>\n\n<span class=\"hljs-comment\"># Ex 3: A collection of database servers in the 'dbservers' group<\/span>\n\n<span class=\"hljs-comment\">## [dbservers]<\/span>\n<span class=\"hljs-comment\">## <\/span>\n<span class=\"hljs-comment\">## db01.intranet.mydomain.net<\/span>\n<span class=\"hljs-comment\">## db02.intranet.mydomain.net<\/span>\n<span class=\"hljs-comment\">## 10.25.1.56<\/span>\n<span class=\"hljs-comment\">## 10.25.1.57<\/span>\n\n<span class=\"hljs-comment\"># Here's another example of host ranges, this time there are no<\/span>\n<span class=\"hljs-comment\"># leading 0s:<\/span>\n\n<span class=\"hljs-comment\">## db-[99:101]-node.example.com<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<li>20<\/li>\n<li>21<\/li>\n<li>22<\/li>\n<li>23<\/li>\n<li>24<\/li>\n<li>25<\/li>\n<li>26<\/li>\n<li>27<\/li>\n<li>28<\/li>\n<li>29<\/li>\n<li>30<\/li>\n<li>31<\/li>\n<li>32<\/li>\n<li>33<\/li>\n<li>34<\/li>\n<li>35<\/li>\n<li>36<\/li>\n<li>37<\/li>\n<li>38<\/li>\n<li>39<\/li>\n<li>40<\/li>\n<li>41<\/li>\n<\/ul>\n<ul>\n<li>\u7136\u540e\u5728\u6b21\u7528ping\u547d\u4ee4\u6765\u68c0\u67e5\u4e3b\u673a\u662f\u5426\u6d3b\u8dc3<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/hlAHJDj2IK.png\" alt=\"mark\" \/><\/li>\n<li>\u56e0\u4e3aansible\u662f\u57fa\u4e8eshh\u7684\uff0c\u7b2c\u4e00\u6b21\u94fe\u63a5\u9700\u8981\u8f93\u5165yes\/no\uff0c\u4e0d\u662f\u5f88\u65b9\u4fbf\uff0c\u60f3\u8981\u53bb\u6389\u8fd9\u4e2ayes\/no\u9700\u8981\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\/etc\/ansible\/ansible.cfg\u8fd9\u4e2a\u6587\u4ef6<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/agefcce25g.jpg\" alt=\"mark\" \/><\/li>\n<li>\u628ahost_key_checking = False\u8fd9\u4e00\u884c\u6ce8\u91ca\u53bb\u6389<\/li>\n<li>\u5728\u91cd\u65b0\u6267\u884c\u5c31\u4e0d\u4f1a\u63d0\u793a\u8fd9\u6837\u7b2c\u4e00\u6b21yes\u6216no<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/8BDK2m8aIm.png\" alt=\"mark\" \/><\/li>\n<li>\u4f46\u662f\u62a5\u9519\uff0c\u4e5f\u6ca1\u6709\u8ba9\u6211\u4eec\u8f93\u5165\u5bc6\u7801\uff0cansible\u9ed8\u8ba4\u662f\u57fa\u4e8ekey\u9a8c\u8bc1\u7684\uff0c\u5982\u679c\u60f3\u4f7f\u7528\u5bc6\u7801\u5728\u540e\u9762\u52a0-k<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/mDL6LB8A04.png\" alt=\"mark\" \/><\/li>\n<li>\u8fd9\u6837\u5c31\u6210\u529f\u4e86\uff0c\u5f53\u7136\u6211\u4eec\u4e5f\u53ef\u4ee5\u591a\u53f0\u4e3b\u673a\uff0c\u4e3b\u673aip\u4e4b\u95f4\u7528\u9017\u53f7\u9694\u5f00<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/C923faF6gL.png\" alt=\"mark\" \/><\/li>\n<li>\u5f53\u7136\u6211\u4eec\u53ea\u7528\u8f93\u5165\u4e00\u904d\u5bc6\u7801\u4e24\u4e2a\u4e3b\u673a\u90fd\u6210\u529f\u4e86\uff0c\u8bf4\u660e\u5982\u679c\u4e3b\u673a\u5bc6\u7801\u4e0d\u4e00\u6837\u4f1a\u62a5\u9519<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/0CfHL836mE.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<h3 id=\"\u7528\u5206\u7ec4\u7684\u65b9\u5f0f\u5c06\u88ab\u7ba1\u7406\u4e3b\u673a\u5199\u5982etcansibleansiblecfg\u6587\u4ef6\u4e2d\"><a name=\"t13\"><\/a>\u7528\u5206\u7ec4\u7684\u65b9\u5f0f\u5c06\u88ab\u7ba1\u7406\u4e3b\u673a\u5199\u5982\/etc\/ansible\/ansible.cfg\u6587\u4ef6\u4e2d<\/h3>\n<ul>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/a31iL9dId6.png\" alt=\"mark\" \/><\/li>\n<li>\u5206\u7ec4\u5c31\u662f\u5c06\u4e00\u7ec4\u4e3b\u673a\u5206\u6210\u4e00\u7ec4\uff0c\u5e76\u5728\u4e2d\u62ec\u53f7\u91cc\u53d6\u4e00\u4e2a\u7ec4\u540d\uff0c\u4e00\u4e2a\u4e3b\u673a\u53ef\u4ee5\u5728\u591a\u4e2a\u7ec4\u91cc\u3002\u8fd8\u6709\u4e00\u4e2a\u9ed8\u8ba4\u7ec4\u5c31\u662fall,\u8fd9\u4e2a\u7ec4\u8868\u793a\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u4e3b\u673a<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/5DCK1g1aiB.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<h3 id=\"\u521b\u5efakey\u9a8c\u8bc1\"><a name=\"t14\"><\/a>\u521b\u5efakey\u9a8c\u8bc1<\/h3>\n<ol>\n<li>\u7528ssh-keygen \u751f\u6210\u516c\u94a5\u79c1\u94a5\u5bf9\n<ul>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/90KEldkELk.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># cd ~<\/span>\n[root@ansible ~]<span class=\"hljs-comment\"># cd .ssh\/<\/span>\n[root@ansible .ssh]<span class=\"hljs-comment\"># ls<\/span>\nid_rsa  id_rsa.pub  known_hosts\n[root@ansible .ssh]<span class=\"hljs-comment\"># <\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<\/ul>\n<ol>\n<li>\u7528ssh-copy-id \u8981\u8fde\u63a5\u7684ip\u4e3b\u673a\n<ul>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/3H8c7I8G3C.png\" alt=\"mark\" \/><\/li>\n<li>\u5b8c\u6210key\u9a8c\u8bc1\u540e\uff0c\u5c31\u4e0d\u7528\u52a0-k\u9009\u9879\u4e86<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/9803E80Bdh.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3 id=\"\u7531\u4e8eansible\u9ed8\u8ba4\u662f\u6ca1\u6709\u542f\u7528\u65e5\u5fd7\u7684\u6240\u4ee5\u6211\u4eec\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u542f\u7528\u914d\u7f6e\u6587\u4ef6\"><a name=\"t15\"><\/a>\u7531\u4e8eansible\u9ed8\u8ba4\u662f\u6ca1\u6709\u542f\u7528\u65e5\u5fd7\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u542f\u7528\u914d\u7f6e\u6587\u4ef6<\/h3>\n<ul>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/Dg3b6mbk47.png\" alt=\"mark\" \/><br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/aDlA66474i.png\" alt=\"mark\" \/><\/li>\n<li>\u9ed8\u8ba4\u4e5f\u662f\u6ca1\u6709\u542f\u7528\u7684\uff0c\u628a\u6ce8\u91ca\u53bb\u6389\u542f\u7528\uff0c\u5f53\u7136\u4e5f\u53ef\u6539\u6210\u5176\u5b83\u8def\u5f84<\/li>\n<li>\u6267\u884c\u4ee5\u4e0bansible\u547d\u4ee4\uff0c\u7136\u540e\u770b\u4e00\u4e0b\u65e5\u5fd7\u6587\u4ef6<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/Aa3CkkcGJ2.png\" alt=\"mark\" \/><\/li>\n<li>\u8fd9\u91cc\u9762\u5b58\u653e\u7740\u547d\u4ee4\u7684\u6267\u884c\u7ed3\u679c<\/li>\n<\/ul>\n<blockquote><p>\u7a0b\u5e8f<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\/usr\/bin\/ansible \u4e3b\u7a0b\u5e8f\uff0c\u4e34\u65f6\u547d\u4ee4\u6267\u884c\u5de5\u5177\n\/usr\/bin\/ansible-doc \u67e5\u770b\u914d\u7f6e\u6587\u6863\uff0c\u6a21\u5757\u529f\u80fd\u67e5\u770b\u5de5\u5177\n\/usr\/bin\/ansible-galaxy \u4e0b\u8f7d\/\u4e0a\u4f20\u4f18\u79c0\u4ee3\u7801\u6216Roles\u6a21\u5757\u7684\u5b98\u7f51\u5e73\u53f0\n\/usr\/bin\/ansible-playbook \u5b9a\u5236\u81ea\u52a8\u5316\u4efb\u52a1\uff0c\u7f16\u6392\u5267\u672c\u5de5\u5177\n\/usr\/bin\/ansible-pull \u8fdc\u7a0b\u6267\u884c\u547d\u4ee4\u7684\u5de5\u5177\n\/usr\/bin\/ansible-vault \u6587\u4ef6\u52a0\u5bc6\u5de5\u5177\n\/usr\/bin\/ansible-console \u57fa\u4e8eConsole\u754c\u9762\u4e0e\u7528\u6237\u4ea4\u4e92\u7684\u6267\u884c\u5de5\u5177<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<\/ul>\n<h3 id=\"\u4e3b\u673a\u6e05\u5355inventory\"><a name=\"t16\"><\/a>\u4e3b\u673a\u6e05\u5355inventory<\/h3>\n<ul>\n<li>Inventory \u4e3b\u673a\u6e05\u5355\n<ul>\n<li>ansible\u7684\u4e3b\u8981\u529f\u7528\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 file\u4e2d\u5c06\u5176\u5206\u7ec4\u547d\u540d<\/li>\n<\/ul>\n<\/li>\n<li>\u9ed8\u8ba4\u7684inventory file\u4e3a\/etc\/ansible\/hosts<\/li>\n<li>inventory file\u53ef\u4ee5\u6709\u591a\u4e2a\uff0c\u4e14\u4e5f\u53ef\u4ee5\u901a\u8fc7DynamicInventory\u6765\u52a8\u6001\u751f\u6210<\/li>\n<li>\/etc\/ansible\/hosts\u6587\u4ef6\u683c\u5f0f<\/li>\n<li>inventory\u6587\u4ef6\u9075\u5faaINI\u6587\u4ef6\u98ce\u683c\uff0c\u4e2d\u62ec\u53f7\u4e2d\u7684\u5b57\u7b26\u4e3a\u7ec4\u540d\u3002\u53ef\u4ee5\u5c06\u540c\u4e00\u4e2a\u4e3b\u673a\u540c\u65f6\u5f52\u5e76\u5230\u591a\u4e2a\u4e0d\u540c\u7684\u7ec4\u4e2d\uff1b\u6b64\u5916\uff0c\u5f53\u5982\u82e5\u76ee\u6807\u4e3b\u673a\u4f7f\u7528\u4e86\u975e\u9ed8\u8ba4\u7684SSH\u7aef\u53e3\uff0c\u8fd8\u53ef\u4ee5\u5728\u4e3b\u673a\u540d\u79f0\u4e4b\u540e\u4f7f\u7528\u5192\u53f7\u52a0\u7aef\u53e3\u53f7\u6765\u6807\u660e<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ntp.magedu.com\n[webservers]\nwww1.magedu.com:<span class=\"hljs-number\">2222<\/span>\nwww2.magedu.com\n[dbservers]\ndb1.magedu.com\ndb2.magedu.com\ndb3.magedu.com<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<\/ul>\n<ul>\n<li>\u5982\u679c\u4e3b\u673a\u540d\u79f0\u9075\u5faa\u76f8\u4f3c\u7684\u547d\u540d\u6a21\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684\u65b9\u5f0f\u6807\u8bc6\u5404\u4e3b\u673a<br \/>\n<blockquote><p>\u793a\u4f8b<\/p><\/blockquote>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[websrvs]\nwww[<span class=\"hljs-number\">01<\/span>:<span class=\"hljs-number\">100<\/span>].example.com\n[dbsrvs]\ndb-[a:f].example.com<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<\/ul>\n<h2 id=\"ansible\u914d\u7f6e\u6587\u4ef6\"><a name=\"t17\"><\/a>ansible\u914d\u7f6e\u6587\u4ef6<\/h2>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">Ansible \u914d\u7f6e\u6587\u4ef6\/etc\/ansible\/ansible.cfg \uff08\u4e00\u822c\u4fdd\u6301\u9ed8\u8ba4\uff09\n[defaults]\n<span class=\"hljs-comment\">#inventory = \/etc\/ansible\/hosts # \u4e3b\u673a\u5217\u8868\u914d\u7f6e\u6587\u4ef6<\/span>\n<span class=\"hljs-comment\">#library = \/usr\/share\/my_modules\/ # \u5e93\u6587\u4ef6\u5b58\u653e\u76ee\u5f55<\/span>\n<span class=\"hljs-comment\">#remote_tmp = $HOME\/.ansible\/tmp #\u4e34\u65f6py\u547d\u4ee4\u6587\u4ef6\u5b58\u653e\u5728\u8fdc\u7a0b\u4e3b\u673a\u76ee\u5f55\uff0c\u5c31\u662f\u5f53\u6211\u4eec\u6267\u884c\u547d\u4ee4\u662f\u76f8\u5f53\u4e8e\u628a\u547d\u4ee4\u590d\u5236\u5230\u8fdc\u7a0b\u4e3b\u673a\u4e2d\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2a\u4e34\u65f6\u6587\u4ef6\uff0c\u6267\u884c\u5b8c\u540e\u5220\u9664<\/span>\n<span class=\"hljs-comment\">#local_tmp = $HOME\/.ansible\/tmp # \u672c\u673a\u7684\u4e34\u65f6\u547d\u4ee4\u6267\u884c\u76ee\u5f55<\/span>\n<span class=\"hljs-comment\">#forks = 5 # \u9ed8\u8ba4\u5e76\u53d1\u6570\uff0c\u5982\u679c\u6709100\u53f0\u4e3b\u673a\u5c31\u4f1a5\u53f05\u53f0\u7684\u6267\u884c\uff0c\u5206\u6279\u6b21\u7684\uff0c\u4e5f\u53ef\u4ee5\u8c03100\uff0c\u8981\u6c42\u673a\u5668\u6027\u80fd\u597d<\/span>\n<span class=\"hljs-comment\">#sudo_user = root # \u9ed8\u8ba4sudo \u7528\u6237\uff0c\u9700\u8981\u5728\u88ab\u7ba1\u7406\u4e3b\u673a\u4e0a\u505asudo\u914d\u7f6e<\/span>\n<span class=\"hljs-comment\">#ask_sudo_pass = True #\u6bcf\u6b21\u6267\u884cansible\u547d\u4ee4\u662f\u5426\u8be2\u95eessh\u5bc6\u7801<\/span>\n<span class=\"hljs-comment\">#ask_pass = True<\/span>\n<span class=\"hljs-comment\">#remote_port = 22  \u56e0\u4e3a\u662f\u57fa\u4e8essh\uff0cssh\u9ed8\u8ba4\u662f22\u7aef\u53e3\uff0c\u5982\u679cssh\u7aef\u53e3\u53f7\u6539\u6210\u5176\u5b83\u7684\uff0c\u8fd9\u91cc\u4e5f\u662f\u8981\u6539\u7684<\/span>\n<span class=\"hljs-comment\">#host_key_checking = False # \u68c0\u67e5\u5bf9\u5e94\u670d\u52a1\u5668\u7684host_key\uff0c\u5efa\u8bae\u53d6\u6d88\u6ce8\u91ca<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<\/ul>\n<h2 id=\"ansible\u7cfb\u5217\u547d\u4ee4\"><a name=\"t18\"><\/a>ansible\u7cfb\u5217\u547d\u4ee4<\/h2>\n<ul>\n<li>Ansible\u7cfb\u5217\u547d\u4ee4<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ansible ansible-doc ansible-playbook ansible-vault\nansible-console ansible-galaxy ansible-pull<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<ul>\n<li>ansible-doc: \u663e\u793a\u6a21\u5757\u5e2e\u52a9<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bahs hljs haml has-numbering\">    ansible-doc [options] [module...]\n    -<span class=\"ruby\">a \u663e\u793a\u6240\u6709\u6a21\u5757\u7684\u6587\u6863\n<\/span>    -<span class=\"ruby\">l, --list \u5217\u51fa\u53ef\u7528\u6a21\u5757\n<\/span>    -<span class=\"ruby\">s, --snippet \u663e\u793a\u6307\u5b9a\u6a21\u5757\u7684playbook\u7247\u6bb5<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<\/ul>\n<blockquote><p>\u793a\u4f8b<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ansible-doc \u2013l \u5217\u51fa\u6240\u6709\u6a21\u5757\nansible-doc ping \u67e5\u770b\u6307\u5b9a\u6a21\u5757\u5e2e\u52a9\u7528\u6cd5\nansible-doc \u2013s ping \u67e5\u770b\u6307\u5b9a\u6a21\u5757\u5e2e\u52a9\u7528\u6cd5\u7b80\u8981\u4fe1\u606f<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<\/ul>\n<ul>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/4l3aLiFAHl.png\" alt=\"mark\" \/><\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/2EEa9HaEgc.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<h3 id=\"title\"><a name=\"t19\"><\/a><\/h3>\n<ul>\n<li>ansible\u901a\u8fc7ssh\u5b9e\u73b0\u914d\u7f6e\u7ba1\u7406\u3001\u5e94\u7528\u90e8\u7f72\u3001\u4efb\u52a1\u6267\u884c\u7b49\u529f\u80fd\uff0c\u5efa\u8bae\u914d\u7f6eansible\u7aef\u80fd\u57fa\u4e8e\u5bc6\u94a5\u8ba4\u8bc1\u7684\u65b9\u5f0f\u8054\u7cfb\u5404\u88ab\u7ba1\u7406\u8282\u70b9<\/li>\n<li>ansible [-m module_name] [-a args]<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">--version \u663e\u793a\u7248\u672c\n-m module \u6307\u5b9a\u6a21\u5757\uff0c\u9ed8\u8ba4\u4e3acommand\u6a21\u5757\n-v \u8be6\u7ec6\u8fc7\u7a0b \u2013vv -vvv\u66f4\u8be6\u7ec6\n--list-hosts \u663e\u793a\u4e3b\u673a\u5217\u8868\uff0c\u53ef\u7b80\u5199\u2014list\n-k, --ask-pass \u63d0\u793a\u8fde\u63a5\u5bc6\u7801\uff0c\u9ed8\u8ba4Key\u9a8c\u8bc1\n-K, --ask-become-pass \u63d0\u793a\u8f93\u5165<span class=\"hljs-built_in\">sudo<\/span>\n-C, --check \u68c0\u67e5\uff0c\u5e76\u4e0d\u6267\u884c\uff0c\u53ea\u662f\u6a21\u62df\u7684\u6267\u884c\u4e00\u6b21\uff0c\u5e76\u4e0d\u4f1a\u5728\u771f\u6b63\u7684\u4e3b\u673a\u4e0a\u6267\u884c\n-T, --timeout=TIMEOUT \u6267\u884c\u547d\u4ee4\u7684\u8d85\u65f6\u65f6\u95f4\uff0c\u9ed8\u8ba4<span class=\"hljs-number\">10<\/span>s\n-u, --user=REMOTE_USER \u6267\u884c\u8fdc\u7a0b\u6267\u884c\u7684\u7528\u6237\uff0c\u6307\u5b9a\u4ee5\u54ea\u4e2a\u7528\u6237\u6267\u884c\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\u5219\u9ed8\u8ba4\u662froot\n-b, --become \u4ee3\u66ff\u65e7\u7248\u7684<span class=\"hljs-built_in\">sudo<\/span> \u5207\u6362<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<\/ul>\n<ul>\n<li>\u663e\u793a\u4e3b\u673a\u5217\u8868<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/2CeK729dj2.png\" alt=\"mark\" \/><\/li>\n<li>\u6307\u5b9a\u7528\u6237<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/Lfeaf13aAc.png\" alt=\"mark\" \/><\/li>\n<li>\u4e5f\u53ef\u4ee5\u7528sudo,\u63d0\u524d\u662f\u8981\u628a\u5bf9\u7aef\u4e3b\u673a\u4e0a\u914d\u7f6e\u597dsudo\u6587\u4ef6\n<ol>\n<li>\u914d\u7f6esudo\u6587\u4ef6\uff0c\u8fd9\u91cc\u6211\u4eec\u76f4\u63a5\u628a\u7528\u6237\u52a0\u5165wheel\u7ec4\u4e2d\uff0c\u56e0\u4e3awheel\u7ec4\u4e2d\u5728sudo\u662f\u4e00\u4e2a\u7ba1\u7406\u5458\u7ec4\uff0c\u62e5\u6709root\u7684\u6743\u9650<\/li>\n<\/ol>\n<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/fhBAadB1bF.png\" alt=\"mark\" \/><\/li>\n<li>\u8fd9\u91cc\u7528command\u6a21\u5757\u662f\u547d\u4ee4\u6a21\u5757\uff0c\u52a0-a\u662f\u53ef\u4ee5\u540e\u9762\u8ddf\u53c2\u6570\uff0c\u4f46\u662f\u8fd9\u91cc\u62a5\u9519\uff0c\u8ba9\u6211\u4eec\u8f93\u5165sudo\u7684\u5bc6\u7801\u52a0\u4e0a\u5927K\u53c2\u6570<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/9E3dCEggfj.png\" alt=\"mark\" \/><\/li>\n<li>\u8fd9\u91cc\u4f1a\u8ba9\u8f93\u5165\u4e24\u8fb9\u53e3\u4ee4\uff0c\u4e00\u4e2a\u662fguo\u7528\u6237ssh\u7684\u53e3\u4ee4\uff0c\u4e00\u4e2asudo\u7684\u53e3\u4ee4\uff0c\u8fd9\u6837\u4f1a\u5f88\u9ebb\u70e6\uff0c\u6211\u4eec\u6539\u4e00\u4e0bsudo\u914d\u7f6e\u6587\u4ef6\u8bbe\u7f6e\u6210\u4e0d\u8f93\u5165\u6b64\u5bc6\u7801\uff0c\u5728\/etc\/sudoers<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/mmmHKjmFc5.png\" alt=\"mark\" \/><\/li>\n<li>\u8fd9\u6837\u5c31\u53ea\u7528\u8f93\u5165\u4e00\u4e2assh\u53e3\u4ee4\u4e86<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/Amc63ea1Jf.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<h3 id=\"\u5982\u4f55\u5c06guo\u7528\u6237\u4e5f\u8bbe\u7f6e\u6210key\u9a8c\u8bc1\u5462\"><a name=\"t20\"><\/a>\u5982\u4f55\u5c06guo\u7528\u6237\u4e5f\u8bbe\u7f6e\u6210key\u9a8c\u8bc1\u5462<\/h3>\n<ol>\n<li>\u7528\u547d\u4ee4ssh-copy-i guo@192.168.27.101<br \/>\n&#8211;<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/LJlk8l5Ikc.png\" alt=\"mark\" \/><\/p>\n<ul>\n<li>\u5728\u6267\u884c\u4ee5\u4e0b\u5c31\u4e0d\u7528\u8f93\u5165\u5bc6\u7801\u4e86<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/16DcEdFgkm.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">usermod <span class=\"hljs-operator\">-a<\/span>G wheel guo<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<\/ul>\n<h3 id=\"ansible\u7684host-pattern\"><a name=\"t21\"><\/a>ansible\u7684Host-pattern<\/h3>\n<ul>\n<li>ansible\u7684Host-pattern\n<ul>\n<li>\u5339\u914d\u4e3b\u673a\u7684\u5217\u8868<\/li>\n<li>All \uff1a\u8868\u793a\u6240\u6709Inventory\u4e2d\u7684\u6240\u6709\u4e3b\u673a\n<ul>\n<li>ansible all \u2013m ping<\/li>\n<\/ul>\n<\/li>\n<li>*:\u901a\u914d\u7b26\n<ul>\n<li>ansible \u201c*\u201d -m ping \u8fd9\u4e2a\u201d*\u201d\u53f7\u76f8\u5f53\u4e8eall\u5168\u90e8\u4e3b\u673a<br \/>\n\u201c`bash<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/fIhBJcJlKC.png\" alt=\"mark\" \/><\/li>\n<li>ansible 192.168.1.* -m ping<\/li>\n<li>ansible \u201c*srvs\u201d -m ping<\/li>\n<\/ul>\n<\/li>\n<li>\u6216\u5173\u7cfb\n<ul>\n<li>ansible \u201cwebsrvs:appsrvs\u201d -m ping \u8868\u793a\u8fd9\u4e24\u4e2a\u90fd\u6267\u884c\u53ef\u4ee5\u52a0\u591a\u4e2a<\/li>\n<li>ansible \u201c192.168.1.10:192.168.1.20\u201d -m ping<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/jJJGciLGBg.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\u903b\u8f91\u4e0e\n<ul>\n<li>ansible \u2018websrvs:&amp;dbsrvs\u2019 \u2013m ping \u5c31\u662f\u53d6\u4ea4\u96c6\n<ul>\n<li>\u5728websrvs\u7ec4\u5e76\u4e14\u5728dbsrvs\u7ec4\u4e2d\u7684\u4e3b\u673a<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/504ADIc6G9.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\u903b\u8f91\u975e\n<ul>\n<li>ansible \u2018websrvs:!dbsrvs\u2019 \u2013m ping<\/li>\n<li>\u5728websrvs\u7ec4\uff0c\u4f46\u4e0d\u5728dbsrvs\u7ec4\u4e2d\u7684\u4e3b\u673a<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/jdljhLeLAL.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<\/li>\n<li>\u7efc\u5408\u903b\u8f91\n<ul>\n<li>ansible \u2018websrvs:dbsrvs:&amp;appsrvs:!ftpsrvs\u2019 \u2013m ping<\/li>\n<\/ul>\n<\/li>\n<li>\u6b63\u5219\u8868\u8fbe\u5f0f\n<ul>\n<li>ansible \u201cwebsrvs:&amp;dbsrvs\u201d \u2013m ping<\/li>\n<li>ansible \u201c~(web|db).*.magedu.com\u201d \u2013m ping<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/74JabdLgB7.png\" alt=\"mark\" \/><\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/DkjE4CFEKg.png\" alt=\"mark\" \/>\n<ul>\n<li>\u8fd9\u91cc\u7684\u6ce2\u6d6a\u7b26\u8868\u793a\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u70b9\u5728\u6b63\u5219\u8868\u793a\u4efb\u610f\u5b57\u7b26\uff0c\u6240\u6709\u8981\u7528\u53cd\u5411\u5355\u5f15\u53f7\u8f6c\u4e49<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2 id=\"ansible\u547d\u4ee4\u6267\u884c\u8fc7\u7a0b\"><a name=\"t22\"><\/a>ansible\u547d\u4ee4\u6267\u884c\u8fc7\u7a0b<\/h2>\n<ul>\n<li>ansible\u547d\u4ee4\u6267\u884c\u8fc7\u7a0b\u53ef\u4ee5\u7528-vvv\u6765\u67e5\u770b\u6267\u884c\u8fc7\u7a0b<\/li>\n<li>\u53ef\u4ee5\u7528-vvv\u6765\u67e5\u770b\u547d\u4ee4\u6267\u884c\u7684\u8be6\u7ec6\u8fc7\u7a0b[root@ansible ~]# ansible \u2018~(web|db|ag)ser\u2019 -m command -a \u201cls \/root\u201d -vvv\n<ol>\n<li>\u52a0\u8f7d\u81ea\u5df1\u7684\u914d\u7f6e\u6587\u4ef6 \u9ed8\u8ba4\/etc\/ansible\/ansible.cfg<\/li>\n<li>\u52a0\u8f7d\u81ea\u5df1\u5bf9\u5e94\u7684\u6a21\u5757\u6587\u4ef6\uff0c\u5982command<\/li>\n<li>\u901a\u8fc7ansible\u5c06\u6a21\u5757\u6216\u547d\u4ee4\u751f\u6210\u5bf9\u5e94\u7684\u4e34\u65f6py\u6587\u4ef6\uff0c\u5e76\u5c06\u8be5 \u6587\u4ef6\u4f20\u8f93\u81f3\u8fdc\u7a0b\u670d\u52a1\u5668\u7684\u5bf9\u5e94\u6267\u884c\u7528\u6237$HOME\/.ansible\/tmp\/ansible-tmp-\u6570\u5b57\/XXX.PY\u6587\u4ef6<\/li>\n<li>\u7ed9\u6587\u4ef6+x\u6267\u884c<\/li>\n<li>\u6267\u884c\u5e76\u8fd4\u56de\u7ed3\u679c<\/li>\n<li>\u5220\u9664\u4e34\u65f6py\u6587\u4ef6\uff0csleep 0\u9000\u51fa<\/li>\n<\/ol>\n<\/li>\n<li>\u6267\u884c\u72b6\u6001\uff1a\n<ul>\n<li>\u989c\u8272\u7684\u5b9a\u4e49\u5728ansible\u7684\u914d\u7f6e\u6587\u4ef6\u91cc\u5b9a\u4e49\u7684<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/4fEcDfhhmm.png\" alt=\"mark\" \/><\/li>\n<li>\u7eff\u8272\uff1a\u6267\u884c\u6210\u529f\u5e76\u4e14\u4e0d\u9700\u8981\u505a\u6539\u53d8\u7684\u64cd\u4f5c<\/li>\n<li>\u9ec4\u8272\uff1a\u6267\u884c\u6210\u529f\u5e76\u4e14\u5bf9\u76ee\u6807\u4e3b\u673a\u505a\u53d8\u66f4<\/li>\n<li>\u7ea2\u8272\uff1a\u6267\u884c\u5931\u8d25<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3 id=\"ansible\u4f7f\u7528\u793a\u4f8b\"><a name=\"t23\"><\/a>ansible\u4f7f\u7528\u793a\u4f8b<\/h3>\n<ul>\n<li>\u4ee5wang\u7528\u6237\u6267\u884cping\u5b58\u6d3b\u68c0\u6d4b\n<ul>\n<li>ansible all -m ping -u wang -k<\/li>\n<\/ul>\n<\/li>\n<li>\u4ee5wang sudo\u81f3root\u6267\u884cping\u5b58\u6d3b\u68c0\u6d4b\n<ul>\n<li>ansible all -m ping -u wang \u2013b -k<\/li>\n<\/ul>\n<\/li>\n<li>\u4ee5wang sudo\u81f3mage\u7528\u6237\u6267\u884cping\u5b58\u6d3b\u68c0\u6d4b\n<ul>\n<li>ansible all -m ping -u wang \u2013b -k \u2013become-user mage<\/li>\n<\/ul>\n<\/li>\n<li>\u4ee5wang sudo\u81f3root\u7528\u6237\u6267\u884cls\n<ul>\n<li>ansible all -m command -u wang \u2013become-user=root -a\u2019ls \/root\u2019 -b \u2013k -K<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2 id=\"ansible\u5e38\u7528\u6a21\u5757\"><a name=\"t24\"><\/a>ansible\u5e38\u7528\u6a21\u5757<\/h2>\n<ul>\n<li>ping \uff1a\u63a2\u6d4b\u5bf9\u65b9\u4e3b\u673a\u662f\u5426\u5728\u5f00\u673a\uff0c\u8fd9\u91cc\u7684ping\u6a21\u5757\u5e76\u4e0d\u662f\u7528ICMP\u534f\u8bae\u7684<br \/>\n\u201cbash<br \/>\n\u5c06\u4e3b\u673a\u8bbe\u7f6e\u4e3a\u7981\u6b62ping<br \/>\n[root@localhost ~]# cat \/proc\/sys\/net\/ipv4\/icmp_echo_ignore_all<br \/>\n0<br \/>\n[root@localhost ~]# echo 1 &gt; \/proc\/sys\/net\/ipv4\/icmp_echo_ignore_all<br \/>\n\u73b0\u5728\u5728\u6d4b\u8fd9\u4e2a\u4e3b\u673a<br \/>\n[root@ansible .ssh]# ansible 192.168.27.101 -m ping<br \/>\n192.168.27.101 | SUCCESS =&gt; {<br \/>\n\u201cchanged\u201d: false,<br \/>\n\u201cping\u201d: \u201cpong\u201d<br \/>\n}<br \/>\n[root@ansible .ssh]# ping 192.168.27.101<br \/>\nPING 192.168.27.101 (192.168.27.101) 56(84) bytes of data.<br \/>\n^C<br \/>\n\u2014 192.168.27.101 ping statistics \u2014<br \/>\n6 packets transmitted, 0 received, 100% packet loss, time 5002ms<br \/>\n\u5982\u679c\u628assh\u670d\u52a1\u505c\u4e86\u4e5f\u662f\u53ef\u4ee5\u6267\u884c\u6210\u529f\u7684\uff0c\u53ea\u6709\u628a\u7f51\u7edc\u670d\u52a1\u505c\u4e86\u624d\u4f1a\u5931\u8d25<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"hljs bash has-numbering\">&gt; Command\uff1a\u5728\u8fdc\u7a0b\u4e3b\u673a\u6267\u884c\u547d\u4ee4\uff0c\u9ed8\u8ba4\u6a21\u5757\uff0c\u53ef\u5ffd\u7565-m\u9009\u9879\n<span class=\"hljs-operator\">-a<\/span>\u662f\u540e\u9762\u8981\u6267\u884c\u7684\u53c2\u6570\n- \u53c2\u6570\n    + chdir \u8fd0\u884ccommand\u547d\u4ee4\u524d\u5148<span class=\"hljs-built_in\">cd<\/span>\u5230\u8fd9\u4e2a\u76ee\u5f55\n    + creates \u5982\u679c\u8fd9\u4e2a\u53c2\u6570\u5bf9\u5e94\u7684\u6587\u4ef6\u5b58\u5728\uff0c\u5c31\u4e0d\u8fd0\u884ccommand\n    + executable \u5c06shell\u5207\u6362\u4e3acommand\u6267\u884c\uff0c\u8fd9\u91cc\u7684\u6240\u6709\u547d\u4ee4\u9700\u8981\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\n    + removes \u5982\u679c\u8fd9\u4e2a\u53c2\u6570\u5bf9\u5e94\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5c31\u4e0d\u8fd0\u884ccommand\n```bash\nansible srvs -m command <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'service vsftpd start'<\/span>\nansible srvs -m command <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'echo magedu |passwd --stdin wang'<\/span> \u4e0d\u6210\u529f\n\u6b64\u547d\u4ee4\u4e0d\u652f\u6301 <span class=\"hljs-variable\">$VARNAME<\/span> &lt; &gt; | ; &amp; \u7b49\uff0c\u7528shell\u6a21\u5757\u5b9e\u73b0\n\u7528chdir\u8fdb\u5165\u5230\u67d0\u4e2a\u76ee\u5f55\u6765\u6267\u884c\u53c2\u6570\n[root@ansible ~]<span class=\"hljs-comment\"># ansible 'dbser' -m command -a 'chdir=\/app\/ ls'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\n<span class=\"hljs-number\">123<\/span>\n\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.128<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\n<span class=\"hljs-number\">1<\/span>\naaa\naccess_log\nawk.txt\ndir\nf1.txt\nf2\nlost+found\npasswd\n[root@ansible ~]<span class=\"hljs-comment\"># ansible 'dbser' -m command -a 'chdir=\/app\/ creates=123 ls'  <\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\nskipped, since <span class=\"hljs-number\">123<\/span> exists  \u8fd9\u91cc\u5199\u7740<span class=\"hljs-number\">123<\/span>\u8fd9\u4e2a\u6587\u4ef6\u5b58\u5728\u5c31\u8df3\u8fc7\uff0c\u800c\u53e6\u4e00\u4e2a\u4e3b\u673a\u6ca1\u6709\u5bf9\u5e94\u7684\u6587\u4ef6\u6240\u6709\u6267\u884cls\n\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.128<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\n<span class=\"hljs-number\">1<\/span>\naaa\naccess_log\nawk.txt\ndir\nf1.txt\nf2\nlost+found\npasswd\n\n\n\n\n&lt;div class=<span class=\"hljs-string\">\"se-preview-section-delimiter\"<\/span>&gt;&lt;\/div&gt;\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<li>20<\/li>\n<li>21<\/li>\n<li>22<\/li>\n<li>23<\/li>\n<li>24<\/li>\n<li>25<\/li>\n<li>26<\/li>\n<li>27<\/li>\n<li>28<\/li>\n<li>29<\/li>\n<li>30<\/li>\n<li>31<\/li>\n<li>32<\/li>\n<li>33<\/li>\n<li>34<\/li>\n<li>35<\/li>\n<li>36<\/li>\n<li>37<\/li>\n<li>38<\/li>\n<li>39<\/li>\n<li>40<\/li>\n<li>41<\/li>\n<li>42<\/li>\n<li>43<\/li>\n<li>44<\/li>\n<li>45<\/li>\n<li>46<\/li>\n<\/ul>\n<ul>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/b00jkGdmi1.png\" alt=\"mark\" \/><\/li>\n<li>\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u4e0d\u6267\u884c<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/KE5Ajkk0m8.png\" alt=\"mark\" \/><\/p>\n<blockquote><p>Shell\uff1a\u548ccommand\u76f8\u4f3c\uff0c\u7528shell\u6267\u884c\u547d\u4ee4<\/p><\/blockquote>\n<\/li>\n<li>\u5e38\u7528\u53c2\u6570\n<ul>\n<li>chdir \u8ddfcommand\u4e00\u6837\u7684\uff0c\u8fd0\u884cshell\u4e4b\u524dcd\u5230\u67d0\u4e2a\u76ee\u5f55<\/li>\n<li>creates \u8ddfcommand\u4e00\u6837\u7684\uff0c\u5982\u679c\u67d0\u4e2a\u6587\u4ef6\u5b58\u5728\u5219\u4e0d\u8fd0\u884cshell<\/li>\n<li>remove \u8ddfcommand\u4e00\u6837\u7684\uff0c\u5982\u679c\u67d0\u4e2a\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u4e0d\u8fd0\u884cshell<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ansible srv -m shell <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'echo magedu |passwd \u2013stdin wang'<\/span>\n\u8c03\u7528bash\u6267\u884c\u547d\u4ee4 \u7c7b\u4f3c cat \/tmp\/stanley.md | awk -F<span class=\"hljs-string\">'|'<\/span><span class=\"hljs-string\">'{print $1,$2}'<\/span> &amp;&gt; \/tmp\/example.txt \u8fd9\u4e9b\u590d\u6742\u547d\u4ee4\uff0c\u5373\u4f7f\u4f7f\u7528shell\u4e5f\u53ef\u80fd\u4f1a\u5931\u8d25\uff0c\u89e3\u51b3\u529e\u6cd5\uff1a\u5199\u5230\u811a\u672c\u65f6\uff0ccopy\u5230\u8fdc\u7a0b\uff0c\u6267\u884c\uff0c\u518d\u628a\u9700\u8981\u7684\u7ed3\u679c\u62c9\u56de\u6267\u884c\u547d\u4ee4\u7684\u673a\u5668\n\u67e5\u770b\u4e3b\u673a\u540d\n[root@ansible ~]<span class=\"hljs-comment\"># ansible 'dbser' -m shell -a  'echo $HOSTNAME'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\n<span class=\"hljs-number\">102<\/span>\n\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.128<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\ncentos6.magedu.com\n\u4f53\u9a8cshell\u548ccommand\u7684\u533a\u522b,\u5148<span class=\"hljs-built_in\">cd<\/span>\u5230\u67d0\u4e2a\u9700\u8981\u7f16\u8bd1\u7684\u76ee\u5f55\uff0c\u6267\u884ccondifgure\u7136\u540e,\u7f16\u8bd1\uff0c\u7136\u540e\u5b89\u88c5\u3002\n ansible -i hosts all -m shell <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">\".\/configure &amp;&amp; make &amp;&amp; make insatll\"<\/span> chdir=\/xxx\/yyy\/\n shell\u4e5f\u652f\u6301\u6761\u4ef6\u5224\u65ad&amp;&amp;||\n [root@ansible ~]<span class=\"hljs-comment\"># ansible 'dbser' -m shell -a 'grep -q root \/etc\/passwd &amp;&amp; ls \/app'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\n<span class=\"hljs-number\">123<\/span>\n\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.128<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\n<span class=\"hljs-number\">1<\/span>\naaa\naccess_log\nawk.txt\ndir\nf1.txt\nf2\nlost+found\npasswd\n\n[root@ansible ~]<span class=\"hljs-comment\"># ansible 'dbser' -m shell -a 'grep -q rootsd \/etc\/passwd &amp;&amp; ls \/app'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | FAILED | rc=<span class=\"hljs-number\">1<\/span> &gt;&gt;\nnon-zero <span class=\"hljs-keyword\">return<\/span> code   \u5982\u679c\u6ca1\u6709\u5219\u4f1a\u51fa\u73b0\u975e<span class=\"hljs-number\">0<\/span>\u9519\u8bef\u7801\n\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.128<\/span> | FAILED | rc=<span class=\"hljs-number\">1<\/span> &gt;&gt;\nnon-zero <span class=\"hljs-keyword\">return<\/span> code\n\n\n\n\n&lt;div class=<span class=\"hljs-string\">\"se-preview-section-delimiter\"<\/span>&gt;&lt;\/div&gt;\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<li>20<\/li>\n<li>21<\/li>\n<li>22<\/li>\n<li>23<\/li>\n<li>24<\/li>\n<li>25<\/li>\n<li>26<\/li>\n<li>27<\/li>\n<li>28<\/li>\n<li>29<\/li>\n<li>30<\/li>\n<li>31<\/li>\n<li>32<\/li>\n<li>33<\/li>\n<li>34<\/li>\n<li>35<\/li>\n<li>36<\/li>\n<li>37<\/li>\n<li>38<\/li>\n<li>39<\/li>\n<\/ul>\n<p>Script\uff1a\u8fd0\u884c\u811a\u672c<br \/>\n&#8211; \u76f8\u5f53\u4e8e\u5148\u628a\u811a\u672c\u4f20\u5230\u8fdc\u65b9\u8282\u70b9\uff0c\u7136\u540e\u5728\u6267\u884c<br \/>\n&#8211;\u00a0<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/a5J085KH0D.png\" alt=\"mark\" \/><\/p>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\"><span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">\"\/PATH\/TO\/SCRIPT_FILE\"<\/span>\nsnsible websrvs -m script <span class=\"hljs-operator\">-a<\/span> f1.sh\n\n\n\n\n&lt;div class=<span class=\"hljs-string\">\"se-preview-section-delimiter\"<\/span>&gt;&lt;\/div&gt;\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<\/ul>\n<blockquote><p>Copy:\u4ece\u670d\u52a1\u5668\u590d\u5236\u6587\u4ef6\u5230\u5ba2\u6237\u7aef<br \/>\n&#8211; \u5e38\u7528\u53c2\u6570<br \/>\n+ src<br \/>\n+ \u7528\u4e8e\u5b9a\u4f4dansible\u6267\u884c\u7684\u673a\u5668\u4e0a\u7684\u6587\u4ef6\uff0c\u9700\u8981\u7edd\u5bf9\u8def\u5f84\u3002\u5982\u679c\u62f7\u8d1d\u7684\u662f\u6587\u4ef6\u5939\uff0c\u90a3\u4e48\u6587\u4ef6\u5939\u4f1a\u6574\u4f53\u62f7\u8d1d\uff0c\u5982\u679c\u7ed3\u5c3e\u662f\u201d\/\u201d,\u90a3\u4e48\u53ea\u6709\u6587\u4ef6\u5939\u5185\u7684\u4e1c\u897f\u88ab\u8003\u8fc7\u53bb\u3002\u4e00\u5207\u7684\u611f\u89c9\u5f88\u50cfrsync\uff0c\u6e90\u5730\u5740<br \/>\n+ content<br \/>\n+ \u7528\u6765\u66ff\u4ee3src\uff0c\u7528\u4e8e\u5c06\u6307\u5b9a\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u62f7\u8d1d\u5230\u8fdc\u7a0b\u6587\u4ef6\u5185<br \/>\n+ dest<br \/>\n+ \u7528\u4e8e\u5b9a\u4f4d\u8fdc\u7a0b\u8282\u70b9\u4e0a\u7684\u6587\u4ef6\uff0c\u9700\u8981\u7edd\u5bf9\u8def\u5f84\u3002\u5982\u679csrc\u6307\u5411\u7684\u662f\u6587\u4ef6\u5939\uff0c\u8fd9\u4e2a\u53c2\u6570\u4e5f\u5fc5\u987b\u662f\u6307\u5411\u6587\u4ef6\u5939\uff0c\u76ee\u6807\u6587\u4ef6<br \/>\n+ backup<br \/>\n+ \u5907\u4efd\u8fdc\u7a0b\u8282\u70b9\u4e0a\u7684\u539f\u59cb\u6587\u4ef6\uff0c\u5728\u62f7\u8d1d\u4e4b\u524d\u3002\u5982\u679c\u53d1\u751f\u4ec0\u4e48\u610f\u5916\uff0c\u539f\u59cb\u6587\u4ef6\u8fd8\u80fd\u4f7f\u7528\u3002<br \/>\n+ directory_mode<br \/>\n+ \u8fd9\u4e2a\u53c2\u6570\u53ea\u80fd\u7528\u4e8e\u62f7\u8d1d\u6587\u4ef6\u5939\u65f6\u5019\uff0c\u8fd9\u4e2a\u8bbe\u5b9a\u540e\uff0c\u6587\u4ef6\u5939\u5185\u65b0\u5efa\u7684\u6587\u4ef6\u4f1a\u88ab\u62f7\u8d1d\u3002\u800c\u8001\u65e7\u7684\u4e0d\u4f1a\u88ab\u62f7\u8d1d<br \/>\n+ follow<br \/>\n+ \u5f53\u62f7\u8d1d\u7684\u6587\u4ef6\u5939\u5185\u6709link\u5b58\u5728\u7684\u65f6\u5019\uff0c\u90a3\u4e48\u62f7\u8d1d\u8fc7\u53bb\u7684\u4e5f\u4f1a\u6709link<br \/>\n+ force<br \/>\n+ \u9ed8\u8ba4\u4e3ayes,\u4f1a\u8986\u76d6\u8fdc\u7a0b\u7684\u5185\u5bb9\u4e0d\u4e00\u6837\u7684\u6587\u4ef6\uff08\u53ef\u80fd\u6587\u4ef6\u540d\u4e00\u6837\uff09\u3002\u5982\u679c\u662fno\uff0c\u5c31\u4e0d\u4f1a\u62f7\u8d1d\u6587\u4ef6\uff0c\u5982\u679c\u8fdc\u7a0b\u6709\u8fd9\u4e2a\u6587\u4ef6<br \/>\n+ group<br \/>\n+ \u8bbe\u5b9a\u4e00\u4e2a\u7fa4\u7ec4\u62e5\u6709\u62f7\u8d1d\u5230\u8fdc\u7a0b\u8282\u70b9\u7684\u6587\u4ef6\u6743\u9650<br \/>\n+ mode<br \/>\n+ \u7b49\u540c\u4e8echmod\uff0c\u53c2\u6570\u53ef\u4ee5\u4e3a\u201cu+rwx or u=rw,g=r,o=r\u201d<br \/>\n+ owner<br \/>\n+ \u8bbe\u5b9a\u4e00\u4e2a\u7528\u6237\u62e5\u6709\u62f7\u8d1d\u5230\u8fdc\u7a0b\u8282\u70b9\u7684\u6587\u4ef6\u6743\u9650<br \/>\n&#8211; \u628a\u590d\u5236selinux\u914d\u7f6e\u6587\u4ef6<br \/>\n&#8211;\u00a0<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/CAdda6Ag3k.png\" alt=\"mark\" \/><br \/>\n&#8211;\u00a0<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/eFDLI1JC4a.png\" alt=\"mark\" \/><br \/>\n&#8211; \u770b\u4e00\u4e0b\u5907\u4efd\u6587\u4ef6<br \/>\n&#8211;\u00a0<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/8dA9mFlckf.png\" alt=\"mark\" \/><br \/>\n&#8211; \u4e5f\u53ef\u4ee5\u6539\u6743\u9650\uff0c\u6240\u5c5e\u7ec4\uff0c\u6216\u6240\u6709\u8005<br \/>\n&#8211;\u00a0<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/Dj88afcEgc.png\" alt=\"mark\" \/><br \/>\n&#8211;\u00a0<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/3e5E76d3d4.png\" alt=\"mark\" \/><\/p><\/blockquote>\n<h5 id=\"\u4e5f\u53ef\u4ee5\u628a\u5185\u5bb9\u590d\u5236\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u4e2d\">\u4e5f\u53ef\u4ee5\u628a\u5185\u5bb9\u590d\u5236\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u4e2d<\/h5>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m copy -a 'content=\"df-h\\nhhhhhhhhhhh\\nls\\n\" dest=\/app\/f1.sh'<\/span>\n[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -a 'cat \/app\/f1.sh'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\ndf-h\nhhhhhhhhhhh\nls\n\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\ndf-h\nhhhhhhhhhhh\nls\n\n\n\n\n&lt;div class=<span class=\"hljs-string\">\"se-preview-section-delimiter\"<\/span>&gt;&lt;\/div&gt;\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ansible srv -m copy <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">\"src=\/root\/f1.sh dest=\/tmp\/f2.sh owner=wang mode=600 backup=yes\"<\/span>\n\u5982\u76ee\u6807\u5b58\u5728\uff0c\u9ed8\u8ba4\u8986\u76d6\uff0c\u6b64\u5904\u6307\u5b9a\u5148\u5907\u4efd\nansible srv -m copy <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">\"content='test content\\n' dest=\/tmp\/f1.txt\"<\/span> \u5229\u7528\u5185\u5bb9\uff0c\u76f4\u63a5\u751f\u6210\u76ee\u6807\u6587\u4ef6<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<\/ul>\n<blockquote><p>Cron\uff1a\u8ba1\u5212\u4efb\u52a1<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\u652f\u6301\u65f6\u95f4\uff1aminute\uff0chour\uff0cday\uff0cmonth\uff0cweekday\nansible srv -m cron <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">\"minute=*\/5 job='\/usr\/sbin\/ntpdate 172.16.0.1 &amp;&gt;\/dev\/null'\nname=Synctime\"<\/span> \u521b\u5efa\u4efb\u52a1\nansible srv -m cron <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'state=absent\nname=Synctime'<\/span> \u5220\u9664\u4efb\u52a1<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<\/ul>\n<p>\u8303\u4f8b1:\u6bcf\u4e94\u5206\u949f\u5468\u516d\u5468\u65e5\u6267\u884c<\/p>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m cron -a 'minute=*\/5 weekday=0,6 job=\"\/usr\/bin\/wall cront job\" name=\"test\"'<\/span>\nname\u5c31\u662f\u7ed9\u8fd9\u4e2a\u8ba1\u5212\u4efb\u52a1\u8d77\u4e2a\u540d\u5b57\njob\u8868\u793a\u8981\u6267\u884c\u7684\u547d\u4ee4\u6216\u64cd\u4f5c\u4f46\u662f\u547d\u4ee4\u8981\u5199\u7edd\u5bf9\u8def\u5f84\n[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -a 'crontab -l'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\n<span class=\"hljs-comment\">#Ansible: test<\/span>\n*\/<span class=\"hljs-number\">5<\/span> * * * <span class=\"hljs-number\">0<\/span>,<span class=\"hljs-number\">6<\/span> \/usr\/bin\/wall cront job<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<\/ul>\n<p>\u8303\u4f8b2\uff1a\u7981\u7528\u8303\u4f8b1\u7684\u8ba1\u5212\u4efb\u52a1<\/p>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m cron -a 'disabled=true job=\"\/usr\/bin\/wall cront job\" name=test ' <\/span>\n\u5fc5\u987b\u8981job\n[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -a 'crontab -l'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\n<span class=\"hljs-comment\">#Ansible: test<\/span>\n<span class=\"hljs-comment\">#* * * * * \/usr\/bin\/wall cront job<\/span>\n\u5f00\u542f\u662fdisabled=no\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<\/ul>\n<blockquote><p>Fetch:\u4ece\u5ba2\u6237\u7aef\u53d6\u6587\u4ef6\u81f3\u670d\u52a1\u5668\u7aef\uff0ccopy\u76f8\u53cd\uff0c\u76ee\u5f55\u53ef\u5148tar<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ansible srv -m fetch <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'src=\/root\/a.sh dest=\/data\/scripts'<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b2,\u5c06\u8fdc\u7a0b\u4e3b\u673a\u7684passwd\u6587\u4ef6\u590d\u5236\u5230\u672c\u673a\u4e0a<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m fetch -a 'src=\/etc\/passwd dest=\/app\/'<\/span>\n\u8fd9\u91cc\u7684src\u662f\u8fdc\u7a0b\u4e3b\u673a\u7684\u8def\u5f84\uff0cdest\u662f\u672c\u5730\u8def\u5f84\u6587\u4ef6\u8981\u590d\u5236\u5230\u54ea\u91cc\n\u5728\u672c\u673a\u4f1a\u6210\u4e00\u4e2a\u8fdc\u7a0b\u4e3b\u673aip\u540d\u5b57\u7684\u4e00\u4e2a\u76ee\u5f55\u91cc\u9762\u5b58\u653e\u7740\u6587\u4ef6\n[root@ansible ~]<span class=\"hljs-comment\"># tree \/app\/<\/span>\n\/app\/\n\u251c\u2500\u2500 <span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span>\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 etc\n\u2502\u00a0\u00a0     \u2514\u2500\u2500 passwd\n\u2514\u2500\u2500 <span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span>\n    \u2514\u2500\u2500 etc\n        \u2514\u2500\u2500 passwd<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b3.\u5c06\u65e5\u5fd7\u6587\u4ef6\u6253\u5305\u538b\u7f29\uff0c\u5e76\u590d\u5236\u5230\u672c\u5730\u4e3b\u673a\u4e0a<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m shell -a 'tar Jcf \/app\/log.tar.xz \/var\/log\/*.log' <\/span>\n[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m fetch -a 'src=\/app\/log.tar.xz dest=\/app\/' <\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<ul>\n<li>\u6709\u4e13\u95e8\u7684tar\u6a21\u5757\u53ef\u4ee5\u5c06\u6253\u5305\u5e76\u590d\u5236<\/li>\n<\/ul>\n<blockquote><p>File\uff1a\u8bbe\u7f6e\u6587\u4ef6\u5c5e\u6027\u548c\u7ba1\u7406\u6587\u4ef6<br \/>\n&#8211; file\u6a21\u5757\u5b83\u5305\u542b\u4e86\u6587\u4ef6\u3001\u6587\u4ef6\u5939\u3001\u8d85\u7ea7\u94fe\u63a5\u7c7b\u7684\u521b\u7acb\u3001\u62f7\u8d1d\u3001\u79fb\u52a8\u3001\u5220\u9664\u64cd\u4f5c\u3002<br \/>\n&#8211; \u5e38\u89c1\u53c2\u6570<br \/>\n+ follow<br \/>\n+ \u5982\u679c\u539f\u6765\u7684\u6587\u4ef6\u662flink\uff0c\u62f7\u8d1d\u540e\u4f9d\u65e7\u662flink<br \/>\n+ force<br \/>\n+ \u5f3a\u5236\u6267\u884c\uff0c\u6ca1\u8bf4\u7684<br \/>\n+ group<br \/>\n+ \u8bbe\u5b9a\u6240\u5c5e\u7ec4\u6743\u9650<br \/>\n+ mode<br \/>\n+ \u7b49\u540c\u4e8echmod\uff0c\u53c2\u6570\u53ef\u4ee5\u4e3a\u201cu+rwx or u=rw,g=r,o=r\u201d<br \/>\n+ owner<br \/>\n+ \u8bbe\u5b9a\u6587\u4ef6\u7684\u6240\u6709\u8005<br \/>\n+ path<br \/>\n+ \u76ee\u6807\u8def\u5f84\uff0c\u4e5f\u53ef\u4ee5\u7528dest,name\u4ee3\u66ff<br \/>\n+ src<br \/>\n+ \u5f85\u62f7\u8d1d\u6587\u4ef6\/\u6587\u4ef6\u5939\u7684\u539f\u59cb\u4f4d\u7f6e\u3002<br \/>\n+ state = ile\/link\/directory\/hard\/touch\/absent<br \/>\n+ file\u4ee3\u8868\u62f7\u8d1d\u540e\u662f\u6587\u4ef6\uff1blink\u4ee3\u8868\u6700\u7ec8\u662f\u4e2a\u8f6f\u94fe\u63a5\uff1bdirectory\u4ee3\u8868\u6587\u4ef6\u5939\uff1bhard\u4ee3\u8868\u786c\u94fe\u63a5\uff1btouch\u4ee3\u8868\u751f\u6210\u4e00\u4e2a\u7a7a\u6587\u4ef6\uff1babsent\u4ee3\u8868\u5220\u9664<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ansible srv -m file <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">\"path=\/root\/a.sh owner=wang mode=755\"<\/span>\nansible web -m file <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'src=\/app\/testfile dest=\/app\/testfile-link state=link'<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b1\uff1a\u521b\u5efa\u4e00\u4e2a\u7a7a\u6587\u4ef6<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m file -a 'path=\/app\/testfile state=touch'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">true<\/span>,\n    <span class=\"hljs-string\">\"dest\"<\/span>: <span class=\"hljs-string\">\"\/app\/testfile\"<\/span>,\n    <span class=\"hljs-string\">\"gid\"<\/span>: <span class=\"hljs-number\">0<\/span>,   \u7ec4id\n    <span class=\"hljs-string\">\"group\"<\/span>: <span class=\"hljs-string\">\"root\"<\/span>,  \u6240\u5c5e\u7ec4\n    <span class=\"hljs-string\">\"mode\"<\/span>: <span class=\"hljs-string\">\"0644\"<\/span>,   \u6587\u4ef6\u6743\u9650\n    <span class=\"hljs-string\">\"owner\"<\/span>: <span class=\"hljs-string\">\"root\"<\/span>,  \u6240\u6709\u8005\n    <span class=\"hljs-string\">\"secontext\"<\/span>: <span class=\"hljs-string\">\"unconfined_u:object_r:default_t:s0\"<\/span>,\n    <span class=\"hljs-string\">\"size\"<\/span>: <span class=\"hljs-number\">0<\/span>,  \u5927\u5c0f\n    <span class=\"hljs-string\">\"state\"<\/span>: <span class=\"hljs-string\">\"file\"<\/span>,\n    <span class=\"hljs-string\">\"uid\"<\/span>: <span class=\"hljs-number\">0<\/span>\n}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b2\uff1a\u628a\u8303\u4f8b1\u521b\u5efa\u7684\u6587\u4ef6\u521b\u5efa\u4e00\u4e2a\u8f6f\u8fde\u63a5<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m file -a 'src=\/app\/testfile path=\/app\/testlink state=link'<\/span>\n[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -a 'ls -l \/app\/'   ansible\u4e0d\u652f\u6301\u522b\u540d\u547d\u4ee4<\/span>\nlrwxrwxrwx. <span class=\"hljs-number\">1<\/span> root root   <span class=\"hljs-number\">13<\/span> Jan <span class=\"hljs-number\">14<\/span> <span class=\"hljs-number\">20<\/span>:<span class=\"hljs-number\">18<\/span> testlink -&gt; \/app\/testfile<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b3\uff1a\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u63a8\u8350\u7528file\u6a21\u5757\u56e0\u4e3a\u6bd4\u8f83\u7a33\u5b9a<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\u65b9\u6cd5\u4e00[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -a 'mkdir \/app\/mk1'<\/span>\n\u65b9\u6cd5\u4e8c[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m file -a 'path=\/app\/mk2 state=directory'<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b4\uff1a\u5220\u9664\u6587\u4ef6\u5939\u6216\u6587\u4ef6<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m file -a 'path=\/app\/mk1 state=absent'<\/span>\n[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m file -a 'path=\/app\/fstab state=absent'<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b5\uff1a\u6e05\u7a7a\u6574\u4e2a\u6587\u4ef6\u5939<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m shell -a 'rm -rf \/app\/*'<\/span>\n ansible cen7 -m file <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'path=\/app state=absent'<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<blockquote><p>Hostname\uff1a\u7ba1\u7406\u4e3b\u673a\u540d\u8fd9\u91cc\u6539\u540d\u662f\u6c38\u4e45\u6539\uff0c\u4f1a\u628a\u914d\u7f6e\u6587\u4ef6\u6539\u6389<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ansible node1 -m hostname <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">\"name=websrv\"<\/span>\nansible <span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> -m hostname <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'name=g101.com'<\/span> <\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<blockquote><p>Yum\uff1a\u7ba1\u7406\u5305<br \/>\n&#8211; \u5e38\u7528\u53c2\u6570<br \/>\n+ disable_gpg_check<br \/>\n+ \u5728\u5b89\u88c5\u5305\u524d\u68c0\u67e5\u5305\uff0c\u53ea\u4f1a\u5f71\u54cdstate\u53c2\u6570\u4e3apresent\u6216\u8005latest\u7684\u65f6\u5019<br \/>\n+ name<br \/>\n+ \u4f60\u9700\u8981\u5b89\u88c5\u7684\u5305\u7684\u540d\u5b57\uff0c\u4e5f\u80fd\u5982\u6b64\u4f7f\u7528name=python=2.7\u5b89\u88c5python2.7<br \/>\n+ state present\/latest\/absent<br \/>\n+ \u7528\u4e8e\u63cf\u8ff0\u5b89\u88c5\u5305\u6700\u7ec8\u72b6\u6001\uff0cpresent\/latest\u7528\u4e8e\u5b89\u88c5\u5305\uff0cabsent\u7528\u4e8eremove\u5b89\u88c5\u5305<br \/>\n+ update_cache<br \/>\n+ \u7528\u4e8e\u5b89\u88c5\u5305\u524d\u6267\u884c\u66f4\u65b0list,\u53ea\u4f1a\u5f71\u54cdstate\u53c2\u6570\u4e3apresent\/latest\u7684\u65f6\u5019<br \/>\n&#8211; \u8303\u4f8b1\uff1a\u5b89\u88c5\u4e00\u4e2a\u5305,\u505a\u8fd9\u4e9b\u4e00\u5b9a\u8981yum\u914d\u7f6e\u597d<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m yum -a 'name=tree state=present'<\/span>\nname\u662f\u8981\u5b89\u88c5\u7684\u5305\u540d\uff0c\n\u5982\u679c\u662f\u5b89\u88c5\u7684\u8bdd\u9ed8\u8ba4\u53ef\u4ee5\u4e0d\u5199state=present,\u9ed8\u8ba4\u662f\u5b89\u88c5\n\u5f53\u7136\u4e5f\u53ef\u4ee5\u7528\u547d\u4ee4\u6a21\u5757\u5b89\u88c5\u6216\u5378\u8f7d\u5305<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b2\uff1a\u53ef\u4ee5\u5b89\u88c5\u5305\u6700\u65b0\u7248<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m yum -a 'name=dstat state=latest'<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b3\uff1a\u4e5f\u53ef\u4ee5\u4e00\u6b21\u6027\u5b89\u88c5\u591a\u4e2a\u5305\uff0c\u5305\u4e4b\u95f4\u7528\u9017\u53f7\u9694\u5f00<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m yum -a 'name=httpd,vsftpd state=present'<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ansible srv -m yum <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'name=httpd state=latest'<\/span> \u5b89\u88c5\nansible srv -m yum <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'name=httpd state=absent'<\/span> \u5220\u9664<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<blockquote><p>Service\uff1a\u7ba1\u7406\u670d\u52a1<br \/>\n&#8211; \u5e38\u7528\u53c2\u6570<br \/>\n+ enabled<br \/>\n+ \u542f\u52a8os\u540e\u542f\u52a8\u5bf9\u5e94service\u7684\u9009\u9879\u3002\u4f7f\u7528service\u6a21\u5757\u7684\u65f6\u5019\uff0cenabled\u548cstate\u81f3\u5c11\u8981\u6709\u4e00\u4e2a\u88ab\u5b9a\u4e49\uff0c\u8bbe\u7f6e\u6210\u5f00\u673a\u542f\u52a8<br \/>\n+ name<br \/>\n+ \u9700\u8981\u8fdb\u884c\u64cd\u4f5c\u7684service\u540d\u5b57<br \/>\n+ state stared\/stoped\/restarted\/reloaded<br \/>\n+ service\u6700\u7ec8\u64cd\u4f5c\u540e\u7684\u72b6\u6001\u3002<br \/>\n&#8211; \u8303\u4f8b1\u542f\u52a8\u670d\u52a1\u5e76\u8bbe\u7f6e\u5f00\u673a\u542f\u52a8<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m service -a 'name=httpd state=started enabled=yes'<\/span>\n\u8fd9\u76f8\u5f53\u4e8e\u5148\u542f\u52a8\u670d\u52a1\uff0c\u7136\u540e\u5728\u628a\u670d\u52a1\u8bbe\u7f6e\u6210\u5f00\u673a\u542f\u52a8<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<ul>\n<li>ansible service \u6a21\u5757\u4e00\u6761\u547d\u4ee4\u53ea\u80fd\u652f\u6301\u4e00\u4e2a\u670d\u52a1\uff0cname\u53ea\u80fd\u5199\u4e00\u4e2a<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\"> ansible srv -m service <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'name=httpd state=stopped'<\/span>\n ansible srv -m service <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'name=httpd state=started'<\/span>\n ansible srv \u2013m service \u2013a <span class=\"hljs-string\">'name=httpd state=reloaded'<\/span>\n ansible srv -m service <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'name=httpd state=restarted'<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<\/ul>\n<blockquote><p>User\uff1a\u7ba1\u7406\u7528\u6237<br \/>\n&#8211; \u5e38\u7528\u53c2\u6570<br \/>\n+ home<br \/>\n+ \u6307\u5b9a\u7528\u6237\u7684\u5bb6\u76ee\u5f55<br \/>\n+ groups<br \/>\n+ \u7528\u6237\u7684\u6240\u5c5e\u7ec4\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u7528\u9017\u53f7\u5206\u9694<br \/>\n+ uid<br \/>\n+ \u6307\u5b9a\u7528\u6237uid<br \/>\n+ name<br \/>\n+ \u8981\u521b\u5efa\u7684\u7528\u6237\u540d<br \/>\n+ createhome<br \/>\n+ \u662f\u5426\u521b\u5efa\u5bb6\u76ee\u5f55 yes|no<br \/>\n+ system<br \/>\n+ \u662f\u5426\u4e3a\u7cfb\u7edf\u7528\u6237<br \/>\n+ remove<br \/>\n+ \u5f53state=absent\u65f6\uff0cremove=yes\u5219\u8868\u793a\u8fde\u540c\u5bb6\u76ee\u5f55\u4e00\u8d77\u5220\u9664\uff0c\u7b49\u4ef7\u4e8euserdel -r<br \/>\n+ state<br \/>\n+ \u662f\u521b\u5efa\u8fd8\u662f\u5220\u9664\uff0c\u9ed8\u8ba4\u662f\u521b\u5efa<br \/>\n+ shell<br \/>\n+ \u6307\u5b9a\u7528\u6237\u7684shell\u73af\u5883<br \/>\n+ password<br \/>\n+ \u6307\u5b9a\u7528\u6237\u7684\u5bc6\u7801<br \/>\n&#8211; \u8303\u4f8b1\uff1a\u521b\u5efa\u4e00\u4e2atest1\u7528\u6237\uff0cuid=2000,\u4e3b\u7ec4\u662fguo,\u9644\u5c5e\u7ec4\u662froot\uff0cbin.\u6307\u5b9a\u5bb6\u76ee\u5f55\u4e3a\u6839\u4e0b \u8fd8\u6709\u63cf\u8ff0<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m user -a 'name=test1 comment=\"test is user\" uid=2000 home=\/test group=guo groups=root,bin'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">true<\/span>,\n    <span class=\"hljs-string\">\"comment\"<\/span>: <span class=\"hljs-string\">\"test is user\"<\/span>,\n    <span class=\"hljs-string\">\"createhome\"<\/span>: <span class=\"hljs-literal\">true<\/span>,\n    <span class=\"hljs-string\">\"group\"<\/span>: <span class=\"hljs-number\">1000<\/span>,\n    <span class=\"hljs-string\">\"groups\"<\/span>: <span class=\"hljs-string\">\"root,bin\"<\/span>,\n    <span class=\"hljs-string\">\"home\"<\/span>: <span class=\"hljs-string\">\"\/test\"<\/span>,\n    <span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"test1\"<\/span>,\n    <span class=\"hljs-string\">\"shell\"<\/span>: <span class=\"hljs-string\">\"\/bin\/bash\"<\/span>,\n    <span class=\"hljs-string\">\"state\"<\/span>: <span class=\"hljs-string\">\"present\"<\/span>,\n    <span class=\"hljs-string\">\"system\"<\/span>: <span class=\"hljs-literal\">false<\/span>,\n    <span class=\"hljs-string\">\"uid\"<\/span>: <span class=\"hljs-number\">2000<\/span>\n}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b2\uff1a\u521b\u5efa\u4e00\u4e2a\u7cfb\u7edf\u7528\u6237,\u7cfb\u7edf\u7528\u6237\u662f\u6ca1\u6709\u5bb6\u76ee\u5f55\u7684\u6240\u4ee5\u8981\u52a0\u4e0acreatehome=no\u4e0d\u521b\u5efa\u5bb6\u76ee\u5f55\uff0c\u5982\u679c\u4e0d\u52a0\u8fd9\u4e2a\u9009\u9879\u5219\u662f\u9ed8\u8ba4\u521b\u5efa\u5bb6\u76ee\u5f55<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m user -a 'name=systemuser system=yes createhome=no' <\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">true<\/span>,\n    <span class=\"hljs-string\">\"comment\"<\/span>: <span class=\"hljs-string\">\"\"<\/span>,\n    <span class=\"hljs-string\">\"createhome\"<\/span>: <span class=\"hljs-literal\">false<\/span>,\n    <span class=\"hljs-string\">\"group\"<\/span>: <span class=\"hljs-number\">996<\/span>,\n    <span class=\"hljs-string\">\"home\"<\/span>: <span class=\"hljs-string\">\"\/home\/systemuser\"<\/span>,   \u867d\u7136\u8fd9\u91cc\u663e\u793a\u662f\u521b\u5efa\u7684\uff0c\u4f46\u662f\u4e8b\u5b9e\u662f\u6ca1\u6709\u521b\u5efa\n    <span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"systemuser\"<\/span>,\n    <span class=\"hljs-string\">\"shell\"<\/span>: <span class=\"hljs-string\">\"\/bin\/bash\"<\/span>,\n    <span class=\"hljs-string\">\"state\"<\/span>: <span class=\"hljs-string\">\"present\"<\/span>,\n    <span class=\"hljs-string\">\"system\"<\/span>: <span class=\"hljs-literal\">true<\/span>,\n    <span class=\"hljs-string\">\"uid\"<\/span>: <span class=\"hljs-number\">998<\/span>\n}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b3\uff1a\u5220\u9664\u4e00\u4e2a\u666e\u901a\u7528\u6237\uff0c\u5305\u62ec\u5bb6\u76ee\u5f55\u4ee5\u524d\u5220\u9664\u5f53state=absent\u65f6remove=yes\u5219\u8868\u793a\u8fde\u540c\u5bb6\u76ee\u5f55\u4e00\u8d77\u5220\u9664\uff0c\u5982\u679c\u53ea\u9009state=absent\u5219\u53ea\u5220\u9664\u7528\u6237\uff0c\u800c\u4e0d\u4f1a\u5220\u9664\u5bb6\u76ee\u5f55<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m user -a 'name=test1 state=absent remove=yes' <\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">true<\/span>,\n    <span class=\"hljs-string\">\"force\"<\/span>: <span class=\"hljs-literal\">false<\/span>,\n    <span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"test1\"<\/span>,\n    <span class=\"hljs-string\">\"remove\"<\/span>: <span class=\"hljs-literal\">true<\/span>,\n    <span class=\"hljs-string\">\"state\"<\/span>: <span class=\"hljs-string\">\"absent\"<\/span>\n}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ansible srv -m user <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'name=user1 comment=\"test user\u201d uid=2048 home=\/app\/user1 group=root'<\/span>\nansible srv -m user <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'name=sysuser1 system=yes home=\/app\/sysuser1 '<\/span>\nansible srv -m user <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">'name=user1 state=absent remove=yes'<\/span>\n\u5220\u9664\u7528\u6237\u53ca\u5bb6\u76ee\u5f55\u7b49\u6570\u636e<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<\/ul>\n<blockquote><p>Group\uff1a\u7ba1\u7406\u7ec4<br \/>\n+ \u548cuser\u53c2\u6570\u4e00\u6837<br \/>\n&#8211; \u8303\u4f8b1\uff1a\u521b\u5efa\u7ec4<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m group -a 'name=group1'<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b2\uff1a\u5220\u9664\u4e00\u4e2a\u7ec4<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible cen7 -m group -a 'name=group1 state=absent'<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\"> ansible srv -m group <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">\"name=testgroup system=yes\"<\/span>\n ansible srv -m group <span class=\"hljs-operator\">-a<\/span> <span class=\"hljs-string\">\"name=testgroup state=absent\"<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<h3 id=\"ansible\u7cfb\u5217\u547d\u4ee4-1\"><a name=\"t25\"><\/a>ansible\u7cfb\u5217\u547d\u4ee4<\/h3>\n<ul>\n<li>ansible-galaxy\n<ul>\n<li>\u8fde\u63a5\u00a0<a href=\"https:\/\/galaxy.ansible.com\/\" target=\"_blank\" rel=\"noopener\">https:\/\/galaxy.ansible.com<\/a>\u00a0\u4e0b\u8f7d\u76f8\u5e94\u7684roles\n<ul>\n<li>\u8fd9\u4e2a\u7f51\u7ad9\u662f\u5b98\u65b9\u7684\uff0c\u56fd\u5916\u7684\u4e00\u4e9b\u5927\u795e\u505a\u7684\u6bd4\u8f83\u597d\u7684playbook\uff0c\u548c\u4e00\u4e9b\u89d2\u8272\uff0c\u4f1a\u4f20\u5230\u8fd9\u91cc\uff0c\u53ef\u4ee5\u4e0b\u8f7d\u4f7f\u7528\u6216\u8005\u53c2\u8003<\/li>\n<\/ul>\n<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180114\/FBhaAGi10I.jpg\" alt=\"mark\" \/><\/li>\n<li>\u5217\u51fa\u6240\u6709\u5df2\u5b89\u88c5\u7684galaxy\n<ul>\n<li>ansible-galaxy list<\/li>\n<\/ul>\n<\/li>\n<li>\u5b89\u88c5galaxy\n<ul>\n<li>ansible-galaxy install geerlingguy.redis<\/li>\n<li>\u5c31\u5c06\u7f51\u7ad9\u4e0a\u7684\u89d2\u8272\u540d\u5199\u7740\u5c31\u53ef\u4ee5<br \/>\n<code>bash<br \/>\n[root@ansible ~]# ansible-galaxy install geerlingguy.nginx&lt;\/li&gt;<br \/>\n&lt;li&gt;downloading role 'nginx', owned by geerlingguy&lt;\/li&gt;<br \/>\n&lt;li&gt;downloading role from https:\/\/github.com\/geerlingguy\/ansible-role-nginx\/archive\/2.5.0.tar.gz&lt;\/li&gt;<br \/>\n&lt;li&gt;extracting geerlingguy.nginx to \/root\/.ansible\/roles\/geerlingguy.nginx \u8fd9\u91cc\u663e\u793a\u4e86\u4e0b\u8f7d\u7684\u4f4d\u7f6e&lt;\/li&gt;<br \/>\n&lt;li&gt;geerlingguy.nginx (2.5.0) was installed successfully<br \/>\n<\/code><\/li>\n<li>\u4e0b\u8f7d\u5b8c\u540e\u53ef\u4ee5\u7528ansible-galaxy list \u67e5\u770b<\/li>\n<li>\u8fdb\u5165\u76ee\u5f55\u770b\u4e00\u4e0b\u91cc\u9762\u7684\u5185\u5bb9<br \/>\n<code>bash<br \/>\n[root@ansible ~]# ls .ansible\/roles\/geerlingguy.nginx\/<br \/>\ndefaults handlers LICENSE meta README.md tasks templates tests vars<br \/>\n<\/code><\/li>\n<li>\u8fd9\u91cc\u90fd\u662fplaybook<\/li>\n<li>\u6700\u597d\u590d\u5236\u4e00\u4efd\u5e76\u6539\u540d\uff0c\u8fd9\u6837\u4fee\u6539\u5982\u679c\u51fa\u73b0\u91cd\u5927\u5931\u8bef\u53ef\u4ee5\u6709\u53c2\u8003<\/li>\n<\/ul>\n<\/li>\n<li>\u5220\u9664galaxy\n<ul>\n<li>ansible-galaxy remove geerlingguy.redis<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>ansible-pull\n<ul>\n<li>\u63a8\u9001\u547d\u4ee4\u81f3\u8fdc\u7a0b\uff0c\u6548\u7387\u65e0\u9650\u63d0\u5347\uff0c\u5bf9\u8fd0\u7ef4\u8981\u6c42\u8f83\u9ad8<\/li>\n<\/ul>\n<\/li>\n<li>Ansible-playbook\n<ul>\n<li>ansible-playbook hello.yml \u540e\u7f00\u63a8\u8350\u7528.yml\u5b83\u662f\u7528yml\u8bed\u8a00\u5199\u7684<\/li>\n<\/ul>\n<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/JB416jJAh6.png\" alt=\"mark\" \/><\/li>\n<li>\u8bed\u6cd5\uff0c\u5728\u524d\u9762\u8981\u52a0\u2014\u4e2a<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim hellow.yml<\/span>\n---\n- hosts: cen7\n  remote_user: root\n\n  tasks:\n    - name: test yml\n      command: \/usr\/bin\/wall <span class=\"hljs-string\">\"hellow word\"<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<\/ul>\n<ul>\n<li>ansible-playbook \u8fd0\u884c\u811a\u672c \u8981\u7528-C \u68c0\u67e5\u4e00\u4e0b<\/li>\n<li>\u8bed\u6cd5\u8981\u6c42\u5f88\u4e25\u683c<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u7a7a\u6587\u4ef6\u7684\u4e00\u4e2a\u811a\u672c<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim hellow.yml <\/span>\n---\n- hosts: cen7\n  remote_user: root\n\n  tasks:\n    - name: test yml\n      file: name=\/app\/test12 state=touch\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<\/ul>\n<ul>\n<li>Ansible-vault\n<ul>\n<li>\u529f\u80fd\uff1a\u7ba1\u7406\u52a0\u5bc6\u89e3\u5bc6yml\u6587\u4ef6<\/li>\n<li>ansible-vault [create|decrypt|edit|encrypt|rekey|view]<\/li>\n<li>ansible-vault encrypt hello.yml \u52a0\u5bc6<br \/>\n<code>bash<br \/>\n[root@ansible ansible]# ansible-vault encrypt hellow.yml<br \/>\nNew Vault password:<br \/>\nConfirm New Vault password:<br \/>\nEncryption successful<br \/>\n<\/code><\/li>\n<li>ansible-vault decrypt hello.yml \u89e3\u5bc6<\/li>\n<li>ansible-vault view hello.yml \u67e5\u770b<\/li>\n<li>ansible-vault edit hello.yml \u7f16\u8f91\u52a0\u5bc6\u6587\u4ef6<\/li>\n<li>ansible-vault rekey hello.yml \u4fee\u6539\u53e3\u4ee4<\/li>\n<li>ansible-vault create new.yml \u521b\u5efa\u65b0\u6587\u4ef6<\/li>\n<\/ul>\n<\/li>\n<li>Ansible-console\uff1a2.0+\u65b0\u589e\uff0c\u53ef\u4ea4\u4e92\u6267\u884c\u547d\u4ee4\uff0c\u652f\u6301tab\n<ul>\n<li>root@test (2)[f:10]\u00a0<span id=\"MathJax-Element-5-Frame\" class=\"MathJax\" style=\"margin: 0px; padding: 0px; font-weight: normal; box-sizing: border-box; display: inline; font-style: normal; line-height: normal; font-size: 16px; text-indent: 0px; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; outline: 0px; position: relative;\" tabindex=\"0\" role=\"presentation\" data-mathml=\"&lt;math xmlns=&quot;http:\/\/www.w3.org\/1998\/Math\/MathML&quot;&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x6267;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x884C;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x7528;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x6237;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;@&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x5F53;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x524D;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x64CD;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x4F5C;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x7684;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x4E3B;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x673A;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x7EC4;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;\/mo&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x5F53;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x524D;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x7EC4;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x7684;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x4E3B;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x673A;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x6570;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x91CF;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;\/mo&gt;&lt;mo stretchy=&quot;false&quot;&gt;[&lt;\/mo&gt;&lt;mi&gt;f&lt;\/mi&gt;&lt;mo&gt;:&lt;\/mo&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x5E76;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x53D1;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mrow class=&quot;MJX-TeXAtom-ORD&quot;&gt;&lt;mo&gt;&amp;#x6570;&lt;\/mo&gt;&lt;\/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;]&lt;\/mo&gt;&lt;\/math&gt;\"><span id=\"MathJax-Span-1\" class=\"math\"><span id=\"MathJax-Span-2\" class=\"mrow\"><span id=\"MathJax-Span-3\" class=\"texatom\"><span id=\"MathJax-Span-4\" class=\"mrow\"><span id=\"MathJax-Span-5\" class=\"mo\">\u6267<\/span><\/span><\/span><span id=\"MathJax-Span-6\" class=\"texatom\"><span id=\"MathJax-Span-7\" class=\"mrow\"><span id=\"MathJax-Span-8\" class=\"mo\">\u884c<\/span><\/span><\/span><span id=\"MathJax-Span-9\" class=\"texatom\"><span id=\"MathJax-Span-10\" class=\"mrow\"><span id=\"MathJax-Span-11\" class=\"mo\">\u7528<\/span><\/span><\/span><span id=\"MathJax-Span-12\" class=\"texatom\"><span id=\"MathJax-Span-13\" class=\"mrow\"><span id=\"MathJax-Span-14\" class=\"mo\">\u6237<\/span><\/span><\/span><span id=\"MathJax-Span-15\" class=\"texatom\"><span id=\"MathJax-Span-16\" class=\"mrow\"><span id=\"MathJax-Span-17\" class=\"mo\">@<\/span><\/span><\/span><span id=\"MathJax-Span-18\" class=\"texatom\"><span id=\"MathJax-Span-19\" class=\"mrow\"><span id=\"MathJax-Span-20\" class=\"mo\">\u5f53<\/span><\/span><\/span><span id=\"MathJax-Span-21\" class=\"texatom\"><span id=\"MathJax-Span-22\" class=\"mrow\"><span id=\"MathJax-Span-23\" class=\"mo\">\u524d<\/span><\/span><\/span><span id=\"MathJax-Span-24\" class=\"texatom\"><span id=\"MathJax-Span-25\" class=\"mrow\"><span id=\"MathJax-Span-26\" class=\"mo\">\u64cd<\/span><\/span><\/span><span id=\"MathJax-Span-27\" class=\"texatom\"><span id=\"MathJax-Span-28\" class=\"mrow\"><span id=\"MathJax-Span-29\" class=\"mo\">\u4f5c<\/span><\/span><\/span><span id=\"MathJax-Span-30\" class=\"texatom\"><span id=\"MathJax-Span-31\" class=\"mrow\"><span id=\"MathJax-Span-32\" class=\"mo\">\u7684<\/span><\/span><\/span><span id=\"MathJax-Span-33\" class=\"texatom\"><span id=\"MathJax-Span-34\" class=\"mrow\"><span id=\"MathJax-Span-35\" class=\"mo\">\u4e3b<\/span><\/span><\/span><span id=\"MathJax-Span-36\" class=\"texatom\"><span id=\"MathJax-Span-37\" class=\"mrow\"><span id=\"MathJax-Span-38\" class=\"mo\">\u673a<\/span><\/span><\/span><span id=\"MathJax-Span-39\" class=\"texatom\"><span id=\"MathJax-Span-40\" class=\"mrow\"><span id=\"MathJax-Span-41\" class=\"mo\">\u7ec4<\/span><\/span><\/span><span id=\"MathJax-Span-42\" class=\"mo\">(<\/span><span id=\"MathJax-Span-43\" class=\"texatom\"><span id=\"MathJax-Span-44\" class=\"mrow\"><span id=\"MathJax-Span-45\" class=\"mo\">\u5f53<\/span><\/span><\/span><span id=\"MathJax-Span-46\" class=\"texatom\"><span id=\"MathJax-Span-47\" class=\"mrow\"><span id=\"MathJax-Span-48\" class=\"mo\">\u524d<\/span><\/span><\/span><span id=\"MathJax-Span-49\" class=\"texatom\"><span id=\"MathJax-Span-50\" class=\"mrow\"><span id=\"MathJax-Span-51\" class=\"mo\">\u7ec4<\/span><\/span><\/span><span id=\"MathJax-Span-52\" class=\"texatom\"><span id=\"MathJax-Span-53\" class=\"mrow\"><span id=\"MathJax-Span-54\" class=\"mo\">\u7684<\/span><\/span><\/span><span id=\"MathJax-Span-55\" class=\"texatom\"><span id=\"MathJax-Span-56\" class=\"mrow\"><span id=\"MathJax-Span-57\" class=\"mo\">\u4e3b<\/span><\/span><\/span><span id=\"MathJax-Span-58\" class=\"texatom\"><span id=\"MathJax-Span-59\" class=\"mrow\"><span id=\"MathJax-Span-60\" class=\"mo\">\u673a<\/span><\/span><\/span><span id=\"MathJax-Span-61\" class=\"texatom\"><span id=\"MathJax-Span-62\" class=\"mrow\"><span id=\"MathJax-Span-63\" class=\"mo\">\u6570<\/span><\/span><\/span><span id=\"MathJax-Span-64\" class=\"texatom\"><span id=\"MathJax-Span-65\" class=\"mrow\"><span id=\"MathJax-Span-66\" class=\"mo\">\u91cf<\/span><\/span><\/span><span id=\"MathJax-Span-67\" class=\"mo\">)<\/span><span id=\"MathJax-Span-68\" class=\"mo\">[<\/span><span id=\"MathJax-Span-69\" class=\"mi\">f<\/span><span id=\"MathJax-Span-70\" class=\"mo\">:<\/span><span id=\"MathJax-Span-71\" class=\"texatom\"><span id=\"MathJax-Span-72\" class=\"mrow\"><span id=\"MathJax-Span-73\" class=\"mo\">\u5e76<\/span><\/span><\/span><span id=\"MathJax-Span-74\" class=\"texatom\"><span id=\"MathJax-Span-75\" class=\"mrow\"><span id=\"MathJax-Span-76\" class=\"mo\">\u53d1<\/span><\/span><\/span><span id=\"MathJax-Span-77\" class=\"texatom\"><span id=\"MathJax-Span-78\" class=\"mrow\"><span id=\"MathJax-Span-79\" class=\"mo\">\u6570<\/span><\/span><\/span><span id=\"MathJax-Span-80\" class=\"mo\">]<\/span><\/span><\/span><span class=\"MJX_Assistive_MathML\" role=\"presentation\">\u6267\u884c\u7528\u6237@\u5f53\u524d\u64cd\u4f5c\u7684\u4e3b\u673a\u7ec4(\u5f53\u524d\u7ec4\u7684\u4e3b\u673a\u6570\u91cf)[f:\u5e76\u53d1\u6570]<\/span><\/span><\/li>\n<li>\u8bbe\u7f6e\u5e76\u53d1\u6570\uff1a forks n \u4f8b\u5982\uff1a forks 10<\/li>\n<li>\u5207\u6362\u7ec4\uff1a cd \u4e3b\u673a\u7ec4 \u4f8b\u5982\uff1a cd web<\/li>\n<li>\u5217\u51fa\u5f53\u524d\u7ec4\u4e3b\u673a\u5217\u8868\uff1a list<\/li>\n<li>\u5217\u51fa\u6240\u6709\u7684\u5185\u7f6e\u547d\u4ee4\uff1a ?\u6216help<\/li>\n<li>\u793a\u4f8b\uff1a<br \/>\n<code>bahs<br \/>\nroot@all (2)[f:5]$ list<br \/>\nroot@all (2)[f:5]$ cd appsrvs<br \/>\nroot@appsrvs (2)[f:5]$ list<br \/>\nroot@appsrvs (2)[f:5]$ yum name=httpd state=present<br \/>\nroot@appsrvs (2)[f:5]$ service name=httpd state=started<br \/>\n<\/code><\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180114\/m1b8JDjBDD.png\" alt=\"mark\" \/><\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180114\/mFlLicjECm.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2 id=\"playbook\"><a name=\"t26\"><\/a>playbook<\/h2>\n<ul>\n<li>playbook\u662f\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u201cplay\u201d\u7ec4\u6210\u7684\u5217\u8868<\/li>\n<li>play\u7684\u4e3b\u8981\u529f\u80fd\u5728\u4e8e\u5c06\u4e8b\u5148\u5f52\u5e76\u4e3a\u4e00\u7ec4\u7684\u4e3b\u673a\u88c5\u626e\u6210\u4e8b\u5148\u901a\u8fc7ansible\u4e2d\u7684task\u5b9a\u4e49\u597d\u7684\u89d2\u8272\u3002\u4ece\u6839\u672c\u4e0a\u6765\u8bb2\uff0c\u6240\u8c13task\u65e0\u975e\u662f\u8c03\u7528ansible\u7684\u4e00\u4e2amodule\u3002\u5c06\u591a\u4e2aplay\u7ec4\u7ec7\u5728\u4e00\u4e2aplaybook\u4e2d\uff0c\u5373\u53ef\u4ee5\u8ba9\u5b83\u4eec\u8054\u540c\u8d77\u6765\u6309\u4e8b\u5148\u7f16\u6392\u7684\u673a\u5236\u540c\u5531\u4e00\u53f0\u5927\u620f<\/li>\n<li>Playbook\u91c7\u7528YAML\u8bed\u8a00\u7f16\u5199<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180113\/1FJ5h7604j.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<h3 id=\"yaml\u8bed\u6cd5\u7b80\u4ecb\"><a name=\"t27\"><\/a>YAML\u8bed\u6cd5\u7b80\u4ecb<\/h3>\n<ul>\n<li>List\uff1a\u5217\u8868\uff0c\u5176\u6240\u6709\u5143\u7d20\u5747\u4f7f\u7528\u201c-\u201d\u6253\u5934<\/li>\n<li>\u793a\u4f8b<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\"><span class=\"hljs-comment\"># A list of tasty fruits<\/span>\n- Apple\n- Orange\n- Strawberry\n- Mango<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<\/ul>\n<h4 id=\"yaml\u4ecb\u7ecd\">YAML\u4ecb\u7ecd<\/h4>\n<ul>\n<li>YAML\u662f\u4e00\u4e2a\u53ef\u8bfb\u6027\u9ad8\u7684\u7528\u6765\u8868\u8fbe\u8d44\u6599\u5e8f\u5217\u7684\u683c\u5f0f\u3002YAML\u53c2\u8003\u4e86\u5176\u4ed6\u591a\u79cd\u8bed\u8a00\uff0c\u5305\u62ec\uff1aXML\u3001C\u8bed\u8a00\u3001Python\u3001Perl\u4ee5\u53ca\u7535\u5b50\u90ae\u4ef6\u683c\u5f0fRFC2822\u7b49\u3002Clark Evans\u57282001\u5e74\u5728\u9996\u6b21\u53d1\u8868\u4e86\u8fd9\u79cd\u8bed\u8a00\uff0c\u53e6\u5916Ingy d\u00f6t Net\u4e0eOren Ben-Kiki\u4e5f\u662f\u8fd9\u8bed\u8a00\u7684\u5171\u540c\u8bbe\u8ba1\u8005<\/li>\n<li>YAML Ain\u2019t Markup Language\uff0c\u5373YAML\u4e0d\u662fXML\u3002\u4e0d\u8fc7\uff0c\u5728\u5f00\u53d1\u7684\u8fd9\u79cd\u8bed\u8a00\u65f6\uff0cYAML\u7684\u610f\u601d\u5176\u5b9e\u662f\uff1a\u201dYet Another Markup Language\u201d\uff08\u4ecd\u662f\u4e00\u79cd\u6807\u8bb0\u8bed\u8a00\uff09<br \/>\n<blockquote><p>\u7279\u6027<br \/>\nYAML\u7684\u53ef\u8bfb\u6027\u597d<br \/>\nYAML\u548c\u811a\u672c\u8bed\u8a00\u7684\u4ea4\u4e92\u6027\u597d<br \/>\nYAML\u4f7f\u7528\u5b9e\u73b0\u8bed\u8a00\u7684\u6570\u636e\u7c7b\u578b<br \/>\nYAML\u6709\u4e00\u4e2a\u4e00\u81f4\u7684\u4fe1\u606f\u6a21\u578b<br \/>\nYAML\u6613\u4e8e\u5b9e\u73b0<br \/>\nYAML\u53ef\u4ee5\u57fa\u4e8e\u6d41\u6765\u5904\u7406<br \/>\nYAML\u8868\u8fbe\u80fd\u529b\u5f3a\uff0c\u6269\u5c55\u6027\u597d<\/p><\/blockquote>\n<\/li>\n<li>\u66f4\u591a\u7684\u5185\u5bb9\u53ca\u89c4\u8303\u53c2\u89c1<a href=\"http:\/\/www.yaml.org\/\" target=\"_blank\" rel=\"noopener\">http:\/\/www.yaml.org<\/a><\/li>\n<\/ul>\n<h3 id=\"yaml\u8bed\u6cd5\u7b80\u4ecb-1\"><a name=\"t28\"><\/a><strong>YAML\u8bed\u6cd5\u7b80\u4ecb<\/strong><\/h3>\n<ul>\n<li>\u5728\u5355\u4e00\u6863\u6848\u4e2d\uff0c\u53ef\u7528\u8fde\u7eed\u4e09\u4e2a\u8fde\u5b57\u53f7(\u2014)\u533a\u5206\u591a\u4e2a\u6863\u6848\u3002\u53e6\u5916\uff0c\u8fd8\u6709\u9009\u62e9\u6027\u7684\u8fde\u7eed\u4e09\u4e2a\u70b9\u53f7( \u2026 )\u7528\u6765\u8868\u793a\u6863\u6848\u7ed3\u5c3e<\/li>\n<li>\u6b21\u884c\u5f00\u59cb\u6b63\u5e38\u5199Playbook\u7684\u5185\u5bb9\uff0c\u4e00\u822c\u5efa\u8bae\u5199\u660e\u8be5Playbook\u7684\u529f\u80fd<\/li>\n<li>\u4f7f\u7528#\u53f7\u6ce8\u91ca\u4ee3\u7801<\/li>\n<li>\u7f29\u8fdb\u5fc5\u987b\u662f\u7edf\u4e00\u7684\uff0c\u4e0d\u80fd\u7a7a\u683c\u548ctab\u6df7\u7528<\/li>\n<li>\u7f29\u8fdb\u7684\u7ea7\u522b\u4e5f\u5fc5\u987b\u662f\u4e00\u81f4\u7684\uff0c\u540c\u6837\u7684\u7f29\u8fdb\u4ee3\u8868\u540c\u6837\u7684\u7ea7\u522b\uff0c\u7a0b\u5e8f\u5224\u522b\u914d\u7f6e\u7684\u7ea7\u522b\u662f\u901a\u8fc7\u7f29\u8fdb\u7ed3\u5408\u6362\u884c\u6765\u5b9e\u73b0\u7684<\/li>\n<li>YAML\u6587\u4ef6\u5185\u5bb9\u548cLinux\u7cfb\u7edf\u5927\u5c0f\u5199\u5224\u65ad\u65b9\u5f0f\u4fdd\u6301\u4e00\u81f4\uff0c\u662f\u533a\u522b\u5927\u5c0f\u5199\u7684\uff0ck\/v\u7684\u503c\u5747\u9700\u5927\u5c0f\u5199\u654f\u611f<\/li>\n<li>k\/v\u7684\u503c\u53ef\u540c\u884c\u5199\u4e5f\u53ef\u6362\u884c\u5199\u3002\u540c\u884c\u4f7f\u7528:\u5206\u9694<\/li>\n<li>v\u53ef\u662f\u4e2a\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u662f\u53e6\u4e00\u4e2a\u5217\u8868<\/li>\n<li>\u4e00\u4e2a\u5b8c\u6574\u7684\u4ee3\u7801\u5757\u529f\u80fd\u9700\u6700\u5c11\u5143\u7d20\u9700\u5305\u62ec name: task<\/li>\n<li>\u4e00\u4e2aname\u53ea\u80fd\u5305\u62ec\u4e00\u4e2atask<\/li>\n<li>YAML\u6587\u4ef6\u6269\u5c55\u540d\u901a\u5e38\u4e3ayml\u6216yaml<\/li>\n<li>Dictionary\uff1a\u5b57\u5178\uff0c\u901a\u5e38\u7531\u591a\u4e2akey\u4e0evalue\u6784\u6210<br \/>\n-\u8303\u4f8b\uff1a<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">---\n<span class=\"hljs-comment\"># An employee record<\/span>\nname: Example Developer\njob: Developer\nskill: Elite\n\u4e5f\u53ef\u4ee5\u5c06key:value\u653e\u7f6e\u4e8e{}\u4e2d\u8fdb\u884c\u8868\u793a\uff0c\u7528,\u5206\u9694\u591a\u4e2akey:value<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b2\uff1a<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">---\n<span class=\"hljs-comment\"># An employee record<\/span>\n{name: Example Developer, job: Developer, skill: Elite}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<\/ul>\n<ul>\n<li>YAML\u7684\u8bed\u6cd5\u548c\u5176\u4ed6\u9ad8\u9636\u8bed\u8a00\u7c7b\u4f3c\uff0c\u5e76\u4e14\u53ef\u4ee5\u7b80\u5355\u8868\u8fbe\u6e05\u5355\u3001\u6563\u5217\u8868\u3001\u6807\u91cf\u7b49\u6570\u636e\u7ed3\u6784\u3002\u5176\u7ed3\u6784\uff08Structure\uff09\u901a\u8fc7\u7a7a\u683c\u6765\u5c55\u793a\uff0c\u5e8f\u5217\uff08Sequence\uff09\u91cc\u7684\u9879\u7528\u201d-\u201c\u6765\u4ee3\u8868\uff0cMap\u91cc\u7684\u952e\u503c\u5bf9\u7528\u201d:\u201d\u5206\u9694<\/li>\n<li>\u793a\u5217<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">name: John Smith\nage: <span class=\"hljs-number\">41<\/span>\ngender: Male\nspouse:\n  name: Jane Smith\n  age: <span class=\"hljs-number\">37<\/span>\n  gender: Female\nchildren:\n- name: Jimmy Smith\n  age: <span class=\"hljs-number\">17<\/span>\n  gender: Male\n- name: Jenny Smith\n  age <span class=\"hljs-number\">13<\/span>\n  gender: Female<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<\/ul>\n<h2 id=\"playbook\u6838\u5fc3\u5143\u7d20\"><a name=\"t29\"><\/a>Playbook\u6838\u5fc3\u5143\u7d20<\/h2>\n<ul>\n<li>Hosts \u6267\u884c\u7684\u8fdc\u7a0b\u4e3b\u673a\u5217\u8868<\/li>\n<li>Tasks \u4efb\u52a1\u96c6<\/li>\n<li>Varniables \u5185\u7f6e\u53d8\u91cf\u6216\u81ea\u5b9a\u4e49\u53d8\u91cf\u5728playbook\u4e2d\u8c03\u7528<\/li>\n<li>Templates \u6a21\u677f\uff0c\u53ef\u66ff\u6362\u6a21\u677f\u6587\u4ef6\u4e2d\u7684\u53d8\u91cf\u5e76\u5b9e\u73b0\u4e00\u4e9b\u7b80\u5355\u903b\u8f91\u7684\u6587\u4ef6<\/li>\n<li>Handlers \u548cnotity\u7ed3\u5408\u4f7f\u7528\uff0c\u7531\u7279\u5b9a\u6761\u4ef6\u89e6\u53d1\u7684\u64cd\u4f5c\uff0c\u6ee1\u8db3\u6761\u4ef6\u65b9\u624d\u6267\u884c\uff0c\u5426\u5219\u4e0d\u6267\u884c<\/li>\n<li>tags \u6807\u7b7e \u6307\u5b9a\u67d0\u6761\u4efb\u52a1\u6267\u884c\uff0c\u7528\u4e8e\u9009\u62e9\u8fd0\u884cplaybook\u4e2d\u7684\u90e8\u5206\u4ee3\u7801\u3002ansible\u5177\u6709\u5e42\u7b49\u6027\uff0c\u56e0\u6b64\u4f1a\u81ea\u52a8\u8df3\u8fc7\u6ca1\u6709\u53d8\u5316\u7684\u90e8\u5206\uff0c\u5373\u4fbf\u5982\u6b64\uff0c\u6709\u4e9b\u4ee3\u7801\u4e3a\u6d4b\u8bd5\u5176\u786e\u5b9e\u6ca1\u6709\u53d1\u751f\u53d8\u5316\u7684\u65f6\u95f4\u4f9d\u7136\u4f1a\u975e\u5e38\u5730\u957f\u3002\u6b64\u65f6\uff0c\u5982\u679c\u786e\u4fe1\u5176\u6ca1\u6709\u53d8\u5316\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7tags\u8df3\u8fc7\u6b64\u4e9b\u4ee3\u7801\u7247\u65ad<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ansible-playbook \u2013t tagsname useradd.yml<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180114\/FcAGIlm3bF.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim test.yml <\/span>\n---\n- hosts: cen7   \u8981\u6267\u884c\u7684\u4e3b\u673a\n  remote_user: root   \u7528\u54ea\u4e2a\u7528\u6237\u6267\u884c\n\n  tasks:  \u4efb\u52a1\u96c6\n    - name: install package   \u4e00\u6761\u4efb\u52a1\u8981\u6709\u4e00\u4e2a\u540d\u5b57\n      yum: name=httpd       \u8c03\u7528\u7684\u6a21\u5757\uff0c\u548c\u53c2\u6570\n    - name: start service\n      service: name=httpd state=started enabled=yes<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<\/ul>\n<ul>\n<li>\u67e5\u770bplaybook\u4e2d\u88ab\u6267\u884c\u7684\u4e3b\u673a\u5217\u8868<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># ansible-playbook test.yml --list-host<\/span>\n\nplaybook: test.yml\n\n  play <span class=\"hljs-comment\">#1 (cen7): cen7  TAGS: []<\/span>\n    pattern: [u<span class=\"hljs-string\">'cen7'<\/span>]\n    hosts (<span class=\"hljs-number\">2<\/span>):\n      <span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span>\n      <span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<\/ul>\n<ul>\n<li>\u67e5\u770bplaybook\u4e2d\u6709\u54ea\u4e9b\u4efb\u52a1<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ERROR! You must specify a playbook file to run\n[root@ansible ansible]<span class=\"hljs-comment\"># ansible-playbook test.yml --list-tasks  <\/span>\n\nplaybook: test.yml\n\n  play <span class=\"hljs-comment\">#1 (cen7): cen7  TAGS: []<\/span>\n    tasks:\n      install package   TAGS: []\n      start service     TAGS: []<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<\/ul>\n<ul>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180114\/emFJle29c3.png\" alt=\"mark\" \/><\/li>\n<li>\u4e00\u4e2aplaybook\u63a8\u8350\u53ea\u5199\u4e00\u4e2aplay\uff0c\u5f53\u7136\u53ef\u4ee5\u5199\u591a\u4e2aplay<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180114\/BfL9eLLj8D.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<h4 id=\"\u5b9e\u9a8c\">\u5b9e\u9a8c<\/h4>\n<ul>\n<li>\u5b89\u88c5http\u670d\u52a1\u5e76\u628a\u5b83\u8bbe\u7f6e\u4e3a\u5f00\u673a\u542f\u52a8\uff0c\u5e76\u5c06\u7aef\u53e3\u6539\u4e3a8080\u7aef\u53e3\uff0c\u5f00\u542f\u670d\u52a1\uff0c\u5199\u4e00\u4e2aplaybook<\/li>\n<li>\u9996\u5148\u5728\u81ea\u5df1\u7684\u7535\u8111\u4e0a\u88c5\u4e00\u4e2ahttp\u670d\u52a1\uff0c\u5e76\u4e14\u4fee\u6539\u597d\u914d\u7f6e\u6587\u4ef6\uff0c\u7136\u540e\u5728\u5c06\u914d\u7f6e\u6587\u4ef6\u590d\u5236\u5230\u8fdc\u7a0b\u4e3b\u673a\u4e0a\uff0c\u542f\u52a8\u670d\u52a1\uff0c\u8fd9\u91cc\u7684\u6240\u6709\u4e3b\u673a\u90fd\u662fcentos7\u7684\uff0c\u4e0d\u540c\u7cfb\u7edf\u7248\u672c\u7684http\u914d\u7f6e\u6587\u4ef6\u4e0d\u4e00\u6837\uff0c\u6240\u4ee5\u8981\u540c\u4e00\u7248\u672c<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">- hosts: cen7\n  remote_user: root\n\n  tasks:\n    - name: isntall httpd\n      yum: name=httpd\n    - name: copy config httpd\n      copy: src=\/app\/httpd.conf dest=\/etc\/httpd\/conf\/  backup=yes\n    - name: start httpd\n      service: name=httpd state=started enabled=yes<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<\/ul>\n<ul>\n<li>\u5f53\u6211\u4eec\u5728\u60f3\u628a\u914d\u7f6e\u6587\u4ef6\u6539\u4e3a80\u7aef\u53e3\uff0c\u5982\u679c\u5728\u91cd\u65b0\u6267\u884c\u4e0a\u9762\u7684playbook\u5e76\u4e0d\u4f1a\u6539\u8fc7\u4e86\uff0c\u56e0\u4e3a\u4e0a\u9762\u53ea\u662f\u5b9a\u4e49\u4e86\u542f\u52a8\u670d\u52a1\uff0c\u6240\u4ee5\u6211\u4eec\u8981\u7528handlers\u6761\u4ef6\u51fa\u53d1<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">---\n- hosts: cen7\n  remote_user: root\n\n  tasks:\n    - name: isntall httpd\n      yum: name=httpd\n    - name: copy config httpd\n      copy: src=\/app\/httpd.conf dest=\/etc\/httpd\/conf\/  backup=yes\n      notify: restart httpd   \u5f53copy\u53d1\u751f\u6539\u53d8\u65f6\u4f1a\u6267\u884cnotify\u6240\u6307\u5b9a\u7684\u540d\u5b57\u4efb\u52a1\n    - name: start httpd\n      service: name=httpd state=started enabled=yes\n\n  handlers:   handlers\u662f\u4e00\u4e2a\u7279\u6b8a\u7684tasks\u4e5f\u53ef\u4ee5\u5199\u591a\u4e2a\u4efb\u52a1\n    - name: restart httpd\n      service: name=httpd state=restarted <\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<\/ul>\n<ul>\n<li>\u5f53\u7b2c\u4e00\u6b21\u6267\u884c\u8fd9\u4e2aplaybook\u4f1a\u987a\u5e8f\u6267\u884c\uff0c\u4e0d\u4f1a\u89e6\u53d1handlers\uff0c\u56e0\u4e3a\u7b2c\u4e00\u6b21\u5305\u90fd\u6ca1\u6709\u88c5\uff0c\u4f46\u662f\u5f53\u7b2c\u4e8c\u6b21\u6267\u884c\u65f6\u5f00\u59cb\u6267\u884chandlers\uff0c\u5f53copy\u547d\u4ee4\u6267\u884c\u7ed3\u679c\u6539\u53d8\u65f6\uff0c\u4f1a\u6267\u884c\u5bf9\u5e94\u7684handlers\u7684\u4efb\u52a1<\/li>\n<li><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180114\/310C3C3ImL.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<h3 id=\"playbook\u57fa\u7840\u7ec4\u4ef6\"><a name=\"t30\"><\/a>playbook\u57fa\u7840\u7ec4\u4ef6<\/h3>\n<ul>\n<li>Hosts\n<ul>\n<li>playbook\u4e2d\u7684\u6bcf\u4e00\u4e2aplay\u7684\u76ee\u7684\u90fd\u662f\u4e3a\u4e86\u8ba9\u67d0\u4e2a\u6216\u67d0\u4e9b\u4e3b\u673a\u4ee5\u67d0\u4e2a\u6307\u5b9a\u7684\u7528\u6237\u8eab\u4efd\u6267\u884c\u4efb\u52a1\u3002hosts\u7528\u4e8e\u6307\u5b9a\u8981\u6267\u884c\u6307\u5b9a\u4efb\u52a1\u7684\u4e3b\u673a\uff0c\u987b\u4e8b\u5148\u5b9a\u4e49\u5728\u4e3b\u673a\u6e05\u5355\u4e2d<\/li>\n<li>\u53ef\u4ee5\u662f\u5982\u4e0b\u5f62\u5f0f<br \/>\n<code>bash<br \/>\none.example.com<br \/>\none.example.com:two.example.com<br \/>\n192.168.1.50<br \/>\n192.168.1.*<br \/>\n<\/code><\/li>\n<li>Websrvs:dbsrvs \u4e24\u4e2a\u7ec4\u7684\u5e76\u96c6<\/li>\n<li>Websrvs:&amp;dbsrvs \u4e24\u4e2a\u7ec4\u7684\u4ea4\u96c6<\/li>\n<li>webservers:!phoenix \u5728websrvs\u7ec4\uff0c\u4f46\u4e0d\u5728dbsrvs\u7ec4<\/li>\n<li>\u793a\u4f8b: &#8211; hosts: websrvs\uff1adbsrvs<\/li>\n<\/ul>\n<\/li>\n<li>remote_user: \u53ef\u7528\u4e8eHost\u548ctask\u4e2d\u3002\u4e5f\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5176\u901a\u8fc7sudo\u7684\u65b9\u5f0f\u5728\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u6267\u884c\u4efb\u52a1\uff0c\u5176\u53ef\u7528\u4e8eplay\u5168\u5c40\u6216\u67d0\u4efb\u52a1\uff1b\u6b64\u5916\uff0c\u751a\u81f3\u53ef\u4ee5\u5728sudo\u65f6\u4f7f\u7528sudo_user\u6307\u5b9asudo\u65f6\u5207\u6362\u7684\u7528\u6237<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">- hosts: websrvs\n  remote_user: root\n  tasks:\n   - name: test connection\n   ping:\n   remote_user: magedu\n   <span class=\"hljs-built_in\">sudo<\/span>: yes \u9ed8\u8ba4<span class=\"hljs-built_in\">sudo<\/span>\u4e3aroot\n   <span class=\"hljs-built_in\">sudo<\/span>_user:wang <span class=\"hljs-built_in\">sudo<\/span>\u4e3awang<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<\/ul>\n<ul>\n<li>task\u5217\u8868\u548caction\n<ul>\n<li>play\u7684\u4e3b\u4f53\u90e8\u5206\u662ftask list\u3002task list\u4e2d\u7684\u5404\u4efb\u52a1\u6309\u6b21\u5e8f\u9010\u4e2a\u5728hosts\u4e2d\u6307\u5b9a\u7684\u6240\u6709\u4e3b\u673a\u4e0a\u6267\u884c\uff0c\u5373\u5728\u6240\u6709\u4e3b\u673a\u4e0a\u5b8c\u6210\u7b2c\u4e00\u4e2a\u4efb\u52a1\u540e\u518d\u5f00\u59cb\u7b2c\u4e8c\u4e2a\u3002\u5728\u8fd0\u884c\u81ea\u4e0b\u800c\u4e0b\u67d0playbook\u65f6\uff0c\u5982\u679c\u4e2d\u9014\u53d1\u751f\u9519\u8bef\uff0c\u6240\u6709\u5df2\u6267\u884c\u4efb\u52a1\u90fd\u5c06\u56de\u6eda\uff0c\u56e0\u6b64\uff0c\u5728\u66f4\u6b63playbook\u540e\u91cd\u65b0\u6267\u884c\u4e00\u6b21\u5373\u53ef<\/li>\n<li>task\u7684\u76ee\u7684\u662f\u4f7f\u7528\u6307\u5b9a\u7684\u53c2\u6570\u6267\u884c\u6a21\u5757\uff0c\u800c\u5728\u6a21\u5757\u53c2\u6570\u4e2d\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf\u3002\u6a21\u5757\u6267\u884c\u662f\u5e42\u7b49\u7684\uff0c\u8fd9\u610f\u5473\u7740\u591a\u6b21\u6267\u884c\u662f\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u5176\u7ed3\u679c\u5747\u4e00\u81f4<\/li>\n<li>\u6bcf\u4e2atask\u90fd\u5e94\u8be5\u6709\u5176name\uff0c\u7528\u4e8eplaybook\u7684\u6267\u884c\u7ed3\u679c\u8f93\u51fa\uff0c\u5efa\u8bae\u5176\u5185\u5bb9\u5c3d\u53ef\u80fd\u6e05\u6670\u5730\u63cf\u8ff0\u4efb\u52a1\u6267\u884c\u6b65\u9aa4\u3002\u5982\u679c\u672a\u63d0\u4f9bname\uff0c\u5219action\u7684\u7ed3\u679c\u5c06\u7528\u4e8e\u8f93\u51fa<\/li>\n<\/ul>\n<\/li>\n<li>tasks\uff1a\u4efb\u52a1\u5217\u8868<\/li>\n<li>\u683c\u5f0f\uff1a\n<ul>\n<li>(1) action: module arguments<\/li>\n<li>(2) module: arguments \u5efa\u8bae\u4f7f\u7528<\/li>\n<\/ul>\n<\/li>\n<li>\u6ce8\u610f\uff1ashell\u548ccommand\u6a21\u5757\u540e\u9762\u8ddf\u547d\u4ee4\uff0c\u800c\u975ekey=value<\/li>\n<li>\u67d0\u4efb\u52a1\u7684\u72b6\u6001\u5728\u8fd0\u884c\u540e\u4e3achanged\u65f6\uff0c\u53ef\u901a\u8fc7\u201cnotify\u201d\u901a\u77e5\u7ed9\u76f8\u5e94\u7684handlers<\/li>\n<li>\u4efb\u52a1\u53ef\u4ee5\u901a\u8fc7\u201dtags\u201c\u6253\u6807\u7b7e\uff0c\u800c\u540e\u53ef\u5728ansible-playbook\u547d\u4ee4\u4e0a\u4f7f\u7528-t\u6307\u5b9a\u8fdb\u884c\u8c03\u7528<\/li>\n<li>\u793a\u4f8b<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">tasks:\n- name: disable selinux\n  command: \/sbin\/setenforce <span class=\"hljs-number\">0<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<\/ul>\n<ul>\n<li>\u5982\u679c\u547d\u4ee4\u6216\u811a\u672c\u7684\u9000\u51fa\u7801\u4e0d\u4e3a\u96f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u65b9\u5f0f\u66ff\u4ee3<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">tasks:\n- name: run this command and ignore the result\n  shell: \/usr\/bin\/somecommand || \/bin\/<span class=\"hljs-literal\">true<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<\/ul>\n<ul>\n<li>\u6216\u8005\u4f7f\u7528ignore_errors\u6765\u5ffd\u7565\u9519\u8bef\u4fe1\u606f<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">tasks:\n- name: run this command and ignore the result\n  shell: \/usr\/bin\/somecommand\n  ignore_errors: True<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<\/ul>\n<h3 id=\"\u8fd0\u884cplaybook\"><a name=\"t31\"><\/a>\u8fd0\u884cplaybook<\/h3>\n<ul>\n<li>\u8fd0\u884cplaybook\u7684\u65b9\u5f0f<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ansible-playbook &lt;filename.yml&gt; ... [options]<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<\/ul>\n<blockquote><p>\u5e38\u89c1\u9009\u9879<br \/>\n\u2013check \u53ea\u68c0\u6d4b\u53ef\u80fd\u4f1a\u53d1\u751f\u7684\u6539\u53d8\uff0c\u4f46\u4e0d\u771f\u6b63\u6267\u884c\u64cd\u4f5c<br \/>\n\u2013list-hosts \u5217\u51fa\u8fd0\u884c\u4efb\u52a1\u7684\u4e3b\u673a<br \/>\n\u2013limit \u4e3b\u673a\u5217\u8868 \u53ea\u9488\u5bf9\u4e3b\u673a\u5217\u8868\u4e2d\u7684\u4e3b\u673a\u6267\u884c<br \/>\n-v \u663e\u793a\u8fc7\u7a0b -vv -vvv \u66f4\u8be6\u7ec6<br \/>\n&#8211; \u8303\u4f8b<\/p><\/blockquote>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ansible-playbook file.yml --check \u53ea\u68c0\u6d4b\nansible-playbook file.yml\nansible-playbook file.yml --limit websrvs<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b1\uff0c\u9650\u5b9a\u7279\u5b9a\u7684\u4e3b\u673a\u6267\u884c\uff0c\u5e76\u4e0d\u662f\u6240\u4ee5\u7684\u4e3b\u673a\u6267\u884c<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible app]<span class=\"hljs-comment\"># ansible-playbook httpd.yml  --limit 192.168.27.101<\/span>\n\u53ea\u9488\u5bf9<span class=\"hljs-number\">101<\/span>\u4e3b\u673a\u6267\u884c\u5e76\u4e0d\u662f\u6240\u6709\u4e3b\u673a<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<p><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180114\/kIDkF7719K.png\" alt=\"mark\" \/><\/p>\n<h2 id=\"playbook-vs-shellscripts\"><a name=\"t32\"><\/a>Playbook VS ShellScripts<\/h2>\n<h3 id=\"shell\u811a\u672c\"><a name=\"t33\"><\/a>SHELL\u811a\u672c<\/h3>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\"><span class=\"hljs-shebang\">#!\/bin\/bash<\/span>\n<span class=\"hljs-comment\"># \u5b89\u88c5Apache<\/span>\nyum install --quiet -y httpd\n<span class=\"hljs-comment\"># \u590d\u5236\u914d\u7f6e\u6587\u4ef6<\/span>\ncp \/path\/to\/config\/httpd.conf\n\/etc\/httpd\/conf\/httpd.conf\ncp\/path\/to\/httpd-vhosts.conf\n\/etc\/httpd\/conf\/httpd-vhosts.conf\n<span class=\"hljs-comment\"># \u542f\u52a8Apache\uff0c\u5e76\u8bbe\u7f6e\u5f00\u673a\u542f\u52a8<\/span>\nservice httpd start\nchkconfig httpd on<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<\/ul>\n<h3 id=\"playbook\u5b9a\u4e49\"><a name=\"t34\"><\/a>Playbook\u5b9a\u4e49<\/h3>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">---\n- hosts: all\n  tasks:\n- name: <span class=\"hljs-string\">\"\u5b89\u88c5Apache\"<\/span>\n    command: yum install -q -y httpd\n- name: <span class=\"hljs-string\">\"\u590d\u5236\u914d\u7f6e\u6587\u4ef6\"<\/span>\n    command: cp \/tmp\/httpd.conf  \/etc\/httpd\/conf\/httpd.conf\n    command: cp \/tmp\/httpd-vhosts.conf \/etc\/httpd\/conf\/httpd-vhosts.conf\n- name: <span class=\"hljs-string\">\"\u542f\u52a8Apache\uff0c\u5e76\u8bbe\u7f6e\u5f00\u673a\u542f\u52a8\"<\/span>\n    service: name=httpd state=started enabled=yes<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\u793a\u4f8b\uff1asystem.yml\n---\n-hosts: all\n remote_user: root\n tasks:\n   - name: create mysql user\n   user: name=mysql system=yes uid=<span class=\"hljs-number\">36<\/span>\n   - name: create a group\n   group: name=httpd system=yes<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<\/ul>\n<h3 id=\"\u793a\u4f8bhttpdyml\"><a name=\"t35\"><\/a>\u793a\u4f8b\uff1ahttpd.yml<\/h3>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">- hosts: websrvs\n  remote_user: root\n  tasks:\n    - name: Install httpd\n       yum: name=httpd state=present\n    - name: Install configure file\n       copy: src=files\/httpd.conf dest=\/etc\/httpd\/conf\/\n     - name: start service\n       service: name=httpd state=started enabled=yes<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<\/ul>\n<h2 id=\"handlers\u548cnotify\u7ed3\u5408\u4f7f\u7528\u89e6\u53d1\u6761\u4ef6\"><a name=\"t36\"><\/a>handlers\u548cnotify\u7ed3\u5408\u4f7f\u7528\u89e6\u53d1\u6761\u4ef6<\/h2>\n<ul>\n<li>Handlers\n<ul>\n<li>\u662ftask\u5217\u8868\uff0c\u8fd9\u4e9btask\u4e0e\u524d\u8ff0\u7684task\u5e76\u6ca1\u6709\u672c\u8d28\u4e0a\u7684\u4e0d\u540c,\u7528\u4e8e\u5f53\u5173\u6ce8\u7684\u8d44\u6e90\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u624d\u4f1a\u91c7\u53d6\u4e00\u5b9a\u7684\u64cd\u4f5c<\/li>\n<\/ul>\n<\/li>\n<li>notify\u8fd9\u4e2aaction\u53ef\u7528\u4e8e\u5728\u6bcf\u4e2aplay\u7684\u6700\u540e\u88ab\u89e6\u53d1\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u591a\u6b21\u6709\u6539\u53d8\u53d1\u751f\u65f6\u6bcf\u6b21\u90fd\u6267\u884c\u6307\u5b9a\u7684\u64cd\u4f5c\uff0c\u4ec5\u5728\u6240\u6709\u7684\u53d8\u5316\u53d1\u751f\u5b8c\u6210\u540e\u4e00\u6b21\u6027\u5730\u6267\u884c\u6307\u5b9a\u64cd\u4f5c\u3002\u5728notify\u4e2d\u5217\u51fa\u7684\u64cd\u4f5c\u79f0\u4e3ahandler\uff0c\u4e5f\u5373notify\u4e2d\u8c03\u7528handler\u4e2d\u5b9a\u4e49\u7684\u64cd\u4f5c<\/li>\n<\/ul>\n<h3 id=\"playbook\u4e2dhandlers\u4f7f\u7528\"><a name=\"t37\"><\/a>Playbook\u4e2dhandlers\u4f7f\u7528<\/h3>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">- hosts: websrvs\n  remote_user: root\n  tasks:\n    - name: Install httpd\n    yum: name=httpd state=present\n    - name: Install configure file\n    copy: src=files\/httpd.conf dest=\/etc\/httpd\/conf\/\n    notify: restart httpd\n    - name: ensure apache is running\n    service: name=httpd state=started enabled=yes\n   handlers:\n    - name: restart httpd\n    service: name=httpd status=restarted<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<\/ul>\n<ul>\n<li>\u793a\u4f8b<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">- hosts: websrvs\n  remote_user: root\n  tasks:\n    - name: add group nginx\n    tags: user\n    user: name=nginx state=present\n    - name: add user nginx\n    user: name=nginx state=present group=nginx\n    - name: Install Nginx\n    yum: name=nginx state=present\n    - name: config\n    copy: src=\/root\/config.txt dest=\/etc\/nginx\/nginx.conf\n    notify:\n        - Restart Nginx\n        - Check Nginx Process\n  handlers:\n    - name: Restart Nginx\n    service: name=nginx state=restarted enabled=yes\n    - name: Check Nginx process\n    shell: killall -<span class=\"hljs-number\">0<\/span> nginx &gt; \/tmp\/nginx.log<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<li>20<\/li>\n<\/ul>\n<h2 id=\"playbook\u4e2dtags\u4f7f\u7528\u4ece\u5267\u672c\u4e2d\u6311\u51fatags\u4ee3\u8868\u7684\u4efb\u52a1\u53ea\u6267\u884c\u8fd9\u4e2a\u5176\u5b83\u7684\u4e0d\u6267\u884c\"><a name=\"t38\"><\/a>Playbook\u4e2dtags\u4f7f\u7528,\u4ece\u5267\u672c\u4e2d\u6311\u51fatags\u4ee3\u8868\u7684\u4efb\u52a1\uff0c\u53ea\u6267\u884c\u8fd9\u4e2a\uff0c\u5176\u5b83\u7684\u4e0d\u6267\u884c<\/h2>\n<ul>\n<li>tage\u7684\u540d\u5b57\u53ef\u4ee5\u76f8\u540c<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\u793a\u4f8b\uff1ahttpd.yml\n- hosts: websrvs\nremote_user: root\ntasks:\n  - name: Install httpd\n  yum: name=httpd state=present\n  - name: Install configure file\n  copy: src=files\/httpd.conf dest=\/etc\/httpd\/conf\/\n  tags: conf\n  - name: start httpd service\n  tags: service\n  service: name=httpd state=started enabled=yes<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible app]<span class=\"hljs-comment\"># ansible-playbook -t conf\uff0cservice httpd.yml <\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<\/ul>\n<ul>\n<li>\u53ef\u4ee5\u4e00\u6b21\u542f\u52a8\u591a\u4e2atags\u6807\u7b7e\u3002\u52a0-t\u6216\u8005\u2013tags<\/li>\n<\/ul>\n<h2 id=\"playbook\u4e2d\u53d8\u91cf\u4f7f\u7528\"><a name=\"t39\"><\/a>Playbook\u4e2d\u53d8\u91cf\u4f7f\u7528<\/h2>\n<ul>\n<li>\u53d8\u91cf\u540d\uff1a\u4ec5\u80fd\u7531\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u7ec4\u6210\uff0c\u4e14\u53ea\u80fd\u4ee5\u5b57\u6bcd\u5f00\u5934<\/li>\n<li>\u53d8\u91cf\u6765\u6e90\uff1a\n<ul>\n<li>1 ansible setup facts \u8fdc\u7a0b\u4e3b\u673a\u7684\u6240\u6709\u53d8\u91cf\u90fd\u53ef\u76f4\u63a5\u8c03\u7528<\/li>\n<li>2 \u5728\/etc\/ansible\/hosts\u4e2d\u5b9a\u4e49\n<ul>\n<li>\u666e\u901a\u53d8\u91cf\uff1a\u4e3b\u673a\u7ec4\u4e2d\u4e3b\u673a\u5355\u72ec\u5b9a\u4e49\uff0c\u4f18\u5148\u7ea7\u9ad8\u4e8e\u516c\u5171\u53d8\u91cf<\/li>\n<li>\u516c\u5171\uff08\u7ec4\uff09\u53d8\u91cf\uff1a\u9488\u5bf9\u4e3b\u673a\u7ec4\u4e2d\u6240\u6709\u4e3b\u673a\u5b9a\u4e49\u7edf\u4e00\u53d8\u91cf<\/li>\n<\/ul>\n<\/li>\n<li>3 \u901a\u8fc7\u547d\u4ee4\u884c\u6307\u5b9a\u53d8\u91cf\uff0c\u4f18\u5148\u7ea7\u6700\u9ad8\n<ul>\n<li>ansible-playbook \u2013e varname=value<\/li>\n<\/ul>\n<\/li>\n<li>4 \u5728playbook\u4e2d\u5b9a\u4e49<br \/>\n<code>bash<br \/>\nvars:&lt;\/li&gt;<br \/>\n&lt;li&gt;var1: value1&lt;\/li&gt;<br \/>\n&lt;li&gt;var2: value2<br \/>\n<\/code><\/li>\n<li>5 \u5728role\u4e2d\u5b9a\u4e49<\/li>\n<\/ul>\n<\/li>\n<li>\u7528setup\u6a21\u5757\u53ef\u4ee5\u67e5\u770b\u673a\u5668\u7684\u6240\u6709\u7684\u5185\u7f6e\u53d8\u91cf<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\"> ansible cen7 -m setup <\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<\/ul>\n<ul>\n<li>\u4e5f\u53ef\u4ee5\u7528filter\u53c2\u6570\u641c\u7d22\u53d8\u91cf,\u652f\u6301\u901a\u914d\u7b26<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible app]<span class=\"hljs-comment\"># ansible cen7 -m setup -a 'filter=*hostname*'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"ansible_facts\"<\/span>: {\n        <span class=\"hljs-string\">\"ansible_hostname\"<\/span>: <span class=\"hljs-string\">\"g102\"<\/span>\n    },\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">false<\/span>\n}\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"ansible_facts\"<\/span>: {\n        <span class=\"hljs-string\">\"ansible_hostname\"<\/span>: <span class=\"hljs-string\">\"g101\"<\/span>\n    },\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">false<\/span>\n}\n[root@ansible app]<span class=\"hljs-comment\"># ansible cen7 -m setup -a 'filter=*nodename*'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"ansible_facts\"<\/span>: {\n        <span class=\"hljs-string\">\"ansible_nodename\"<\/span>: <span class=\"hljs-string\">\"g101.com\"<\/span>\n    },\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">false<\/span>\n}\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"ansible_facts\"<\/span>: {\n        <span class=\"hljs-string\">\"ansible_nodename\"<\/span>: <span class=\"hljs-string\">\"g102.com\"<\/span>\n    },\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">false<\/span>\n}\n[root@ansible app]<span class=\"hljs-comment\"># ansible cen7 -m setup -a 'filter=*fqdn*'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"ansible_facts\"<\/span>: {\n        <span class=\"hljs-string\">\"ansible_fqdn\"<\/span>: <span class=\"hljs-string\">\"g102.com\"<\/span>\n    },\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">false<\/span>\n}\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"ansible_facts\"<\/span>: {\n        <span class=\"hljs-string\">\"ansible_fqdn\"<\/span>: <span class=\"hljs-string\">\"g101.com\"<\/span>\n    },\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">false<\/span>\n}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<li>20<\/li>\n<li>21<\/li>\n<li>22<\/li>\n<li>23<\/li>\n<li>24<\/li>\n<li>25<\/li>\n<li>26<\/li>\n<li>27<\/li>\n<li>28<\/li>\n<li>29<\/li>\n<li>30<\/li>\n<li>31<\/li>\n<li>32<\/li>\n<li>33<\/li>\n<li>34<\/li>\n<li>35<\/li>\n<li>36<\/li>\n<li>37<\/li>\n<li>38<\/li>\n<li>39<\/li>\n<\/ul>\n<ul>\n<li>\u6700\u597d\u5199\u5168\u540d\u79f0<\/li>\n<li>\u53ef\u4ee5\u67e5ip\u5730\u5740<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ct, raw, meta\n[root@ansible app]<span class=\"hljs-comment\"># ansible cen7 -m setup -a 'filter=*addr*'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"ansible_facts\"<\/span>: {\n        <span class=\"hljs-string\">\"ansible_all_ipv4_addresses\"<\/span>: [\n            <span class=\"hljs-string\">\"192.168.27.102\"<\/span>\n        ],\n        <span class=\"hljs-string\">\"ansible_all_ipv6_addresses\"<\/span>: [\n            <span class=\"hljs-string\">\"fe80::20c:29ff:fe8b:f0dd\"<\/span>\n        ]\n    },\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">false<\/span>\n}\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"ansible_facts\"<\/span>: {\n        <span class=\"hljs-string\">\"ansible_all_ipv4_addresses\"<\/span>: [\n            <span class=\"hljs-string\">\"192.168.27.101\"<\/span>\n        ],\n        <span class=\"hljs-string\">\"ansible_all_ipv6_addresses\"<\/span>: [\n            <span class=\"hljs-string\">\"fe80::20c:29ff:fec3:887f\"<\/span>\n        ]\n    },\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">false<\/span>\n}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<li>20<\/li>\n<li>21<\/li>\n<li>22<\/li>\n<li>23<\/li>\n<li>24<\/li>\n<\/ul>\n<h3 id=\"playbook\u4e2d\u53d8\u91cf\u4f7f\u7528-1\"><a name=\"t40\"><\/a>Playbook\u4e2d\u53d8\u91cf\u4f7f\u7528<\/h3>\n<ul>\n<li>\u53d8\u91cf\u547d\u540d\n<ul>\n<li>\u53d8\u91cf\u540d\u4ec5\u80fd\u7531\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u7ec4\u6210\uff0c\u4e14\u53ea\u80fd\u4ee5\u5b57\u6bcd\u5f00\u5934<\/li>\n<\/ul>\n<\/li>\n<li>\u53d8\u91cf\u5b9a\u4e49\uff1akey=value\n<ul>\n<li>\u793a\u4f8b\uff1ahttp_port=80<\/li>\n<\/ul>\n<\/li>\n<li>\u53d8\u91cf\u8c03\u7528\u65b9\u5f0f\uff1a\n<ul>\n<li>\u901a\u8fc7{{ variable_name }} \u8c03\u7528\u53d8\u91cf\uff0c\u4e14\u53d8\u91cf\u540d\u524d\u540e\u5fc5\u987b\u6709\u7a7a\u683c\uff0c\u6709\u65f6\u7528\u201c{{ variable_name }}\u201d\u624d\u751f\u6548<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ansible-playbook \u2013e \u9009\u9879\u6307\u5b9a\nansible-playbook test.yml <span class=\"hljs-operator\">-e<\/span> <span class=\"hljs-string\">\"hosts=www user=mageedu\"<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b1:var.yml<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">- hosts: websrvs\n  remote_user: root\n  tasks\n   - name: install package\n     yum: name={{ pkname }} state=present\n<span class=\"hljs-comment\"># ansible-playbook -e pkname=httpd var.yml<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b\u5728playbook\u4e2d\u8d4b\u503c\u5e76\u8c03\u7528<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim var2.yml<\/span>\n---\n- hosts: cen7\n  remote_user: root\n  vars:                    \/\/\u5982\u679c\u8981\u5728play\u4e2d\u7b2c\u4e00\u53d8\u91cf\u8981\u7528vars\n   - username: user123     \/\/\u53d8\u91cf\u540d\uff1a\u8d4b\u503c\n   - groupname: group123\n\n  tasks:\n   - name: create group\n     group: name={{ groupname }}   \/\/\u8c03\u7528\u53d8\u91cf\n   - name: create uesr\n     user: name={{ username }} group={{ groupname }} home=\/app\/{{ username }}dir  \/\/\u53ef\u4ee5\u5199\u53d8\u91cf\u52a0\u5176\u5b83\u5b57\u6bb5\n[root@ansible ansible]<span class=\"hljs-comment\"># ansible-playbook  var2.yml<\/span>\n[root@ansible ansible]<span class=\"hljs-comment\"># ansible cen7 -a 'getent passwd user123'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\nuser123:x:<span class=\"hljs-number\">1001<\/span>:<span class=\"hljs-number\">1001<\/span>::\/app\/user123dir:\/bin\/bash<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b2\uff1a\u53d8\u91cf<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\"><span class=\"hljs-comment\"># vim var2.yml<\/span>\n- hosts: websrvs\n  remote_user: root\n  vars:\n   - username: user1\n   - groupname: group1\n\n  tasks:\n   - name: create group\n     group: name={{ groupname }} state=present\n   - name: create user\n     user: name={{ username }} state=present\n<span class=\"hljs-comment\"># ansible-playbook var2.yml<\/span>\n<span class=\"hljs-comment\"># ansible-playbook -e \"username=user2 groupname=group2\u201d var2.yml<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b3<\/li>\n<\/ul>\n<h2 id=\"rootansible-ansible-vim-var1yml\"><a name=\"t41\"><\/a>[root@ansible ansible]# vim var1.yml<\/h2>\n<ul>\n<li>hosts: cen7<br \/>\nremote_user: root<\/p>\n<p>tasks:<\/p>\n<ul>\n<li>name: install package<br \/>\nyum: name={{ pkname }} \/\/\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u540d\u4e3apkname<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>[root@ansible ansible]# ansible-playbook -e pkname=vsftpd var1.yml \u7528-e \u5199\u8981\u6307\u5b9a\u5b9a\u7684\u53d8\u91cf\u540d\u7136\u540e\u8d4b\u503c<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs haml has-numbering\">-<span class=\"ruby\"> \u8303\u4f8b<span class=\"hljs-number\">4<\/span>\uff1a\u4f7f\u7528\u591a\u4e2a\u53d8\u91cf\u540d\n<\/span>```bash\n\n\n\n\n&lt;div class=\"se-preview-section-delimiter\"&gt;&lt;\/div&gt;\n\n[root@ansible ansible]# vim var1.yml\n-<span class=\"ruby\">--\n<\/span>-<span class=\"ruby\"> <span class=\"hljs-symbol\">hosts:<\/span> cen7\n<\/span>  remote_user: root\n\n  tasks:\n    -<span class=\"ruby\"> <span class=\"hljs-symbol\">name:<\/span> install package\n<\/span>      yum: name={{ pkname }}   \/\/\u7b2c\u4e00\u4e2a\u53d8\u91cf\n    -<span class=\"ruby\"> <span class=\"hljs-symbol\">name:<\/span> copy file\n<\/span>      copy: src=\/app\/{{ filename }} dest=\/app\/   \/\/\u7b2c\u4e8c\u4e2a\u53d8\u91cf\n\n[root@ansible ansible]# ansible-playbook -e \"pkname=dstat filename=httpd.conf\" var1.yml\n\u8981\u8d4b\u503c\u591a\u4e2a\u53d8\u91cf\u8981\u7528\u53cc\u5f15\u53f7\u5f15\u8d77\u6765\uff0c\n\n\n\n\n&lt;div class=\"se-preview-section-delimiter\"&gt;&lt;\/div&gt;\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<li>20<\/li>\n<li>21<\/li>\n<li>22<\/li>\n<li>23<\/li>\n<li>24<\/li>\n<li>25<\/li>\n<li>26<\/li>\n<li>27<\/li>\n<\/ul>\n<ul>\n<li>\u7528yum\u6a21\u5757\u53ef\u4ee5\u4e00\u6b21\u5b89\u88c5\u6216\u5378\u8f7d\u591a\u4e2a\u5305<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># ansible cen7 -m yum -a 'name=dstat,httpd state=absent'<\/span>\n\n\n\n\n&lt;div class=<span class=\"hljs-string\">\"se-preview-section-delimiter\"<\/span>&gt;&lt;\/div&gt;\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim var2.yml<\/span>\n---\n- hosts: cen7\n  remote_user: root\n  vars:\n   - username: user123\n   - groupname: group123\n\n  tasks:\n   - name: create group\n     group: name={{ groupname }}\n   - name: create uesr\n     user: name={{ username }} group={{ groupname }} home=\/app\/{{ username }}dir<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<\/ul>\n<h3 id=\"\u4e5f\u53ef\u5728palybook\u4e2d\u8c03\u7528setup\u6a21\u5757\u4e2d\u7684\u53d8\u91cfsetup\u6a21\u5757\u662f\u7cfb\u7edf\u5185\u6240\u6709\u7684\u53d8\u91cf\u53ef\u4ee5\u7528filter\u53c2\u6570\u641c\u7d22\u5bf9\u5e94\u7684\u53d8\u91cf\u540d\"><a name=\"t42\"><\/a>\u4e5f\u53ef\u5728palybook\u4e2d\u8c03\u7528setup\u6a21\u5757\u4e2d\u7684\u53d8\u91cf\uff0csetup\u6a21\u5757\u662f\u7cfb\u7edf\u5185\u6240\u6709\u7684\u53d8\u91cf\u53ef\u4ee5\u7528filter\u53c2\u6570\u641c\u7d22\u5bf9\u5e94\u7684\u53d8\u91cf\u540d<\/h3>\n<ul>\n<li>\u5728setup\u662f\u6709\u5b9a\u4e49\u4e3b\u673a\u540d\u7684\u53d8\u91cf\uff0c\u5206\u522b\u662fansible_nodename\u8fd9\u4e2a\u53d8\u91cf\u662f\u4e3b\u673a\u540d\u5168\u540d\uff0c\u8fd8\u6709\u4e00\u4e2aansible_hostnam\u662f\u53ea\u6709\u524d\u9762\u4e00\u822c\uff0c\u53ef\u4ee5\u6839\u636e\u4e0b\u9762\u7684\u4f8b\u5b50\u770b\u51fa\u5f88\u660e\u663e\u3002<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># ansible cen7 -m setup -a 'filter=\"*nodename*\"'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"ansible_facts\"<\/span>: {\n        <span class=\"hljs-string\">\"ansible_nodename\"<\/span>: <span class=\"hljs-string\">\"g102.com\"<\/span>\n    },\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">false<\/span>\n}\n[root@ansible ansible]<span class=\"hljs-comment\"># ansible cen7 -m setup -a 'filter=\"*hostname*\"'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"ansible_facts\"<\/span>: {\n        <span class=\"hljs-string\">\"ansible_hostname\"<\/span>: <span class=\"hljs-string\">\"g102\"<\/span>\n    },\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">false<\/span>\n}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<\/ul>\n<ul>\n<li>\u5728playbook\u4e2d\u8c03\u7528setup\u7cfb\u7edf\u4e2d\u7684\u53d8\u91cf<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim var3.yml<\/span>\n---\n- hosts: cen7\n  remote_user: root\n\n  tasks:\n    - name: create file\n      file: name=\/app\/{{ ansible_hostname }}.txt  state=touch    \/\/ansible_hostnam \u662fsetup\u4e2d\u7684\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\n[root@ansible ansible]<span class=\"hljs-comment\"># ansible-playbook var3.yml <\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<\/ul>\n<h3 id=\"\u5728\u4e3b\u673a\u6e05\u5355hosts\u4e2d\u5b9a\u4e49\u53d8\u91cf\"><a name=\"t43\"><\/a>\u5728\u4e3b\u673a\u6e05\u5355hosts\u4e2d\u5b9a\u4e49\u53d8\u91cf<\/h3>\n<p><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/5g4fIEi9b9.jpg\" alt=\"mark\" \/><br \/>\n&#8211; \u8fd9\u91cc101\u4e3b\u673a\u5b9a\u4e49\u7684http_port\u662f85,102\u76f4\u63a5\u5b9a\u4e49\u7684\u662f86\uff0c\u867d\u7136\u53d8\u91cf\u540d\u90fd\u662f\u540c\u4e00\u4e2a\uff0c\u9488\u5bf9\u4e0d\u540c\u7684\u4e3b\u673a\u53ef\u4ee5\u8bbe\u7f6e\u4e0d\u540c\u7684\u503c<br \/>\n&#8211; \u7136\u540e\u6211\u4eec\u53ef\u4ee5\u8c03\u7528\u7528\u547d\u4ee4\u884c<\/p>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># ansible webser -m hostname -a 'name=web{{ http_port  }}'  \/\/\u4fee\u6539\u4e3b\u673a\u540d\uff0c\u4f1a\u9488\u5bf9\u4e0d\u540c\u7684\u4e3b\u673a\u7684\u53d8\u91cf\u503c\u4e0d\u4e00\u6837<\/span>\n[root@ansible ~]<span class=\"hljs-comment\"># ansible webser -a 'hostname'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\nweb85   \u8fd9\u662f\u9488\u5bf9\u67d0\u4e2a\u4e3b\u673a\u8bbe\u7f6e\u4e0d\u540c\u7684\u503c\n\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\nweb86<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<\/ul>\n<ul>\n<li>\u4e5f\u53ef\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff0c\u5e76\u5728playbook\u4e2d\u4f7f\u7528<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/iH9dgfbmHE.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim var4.yml<\/span>\n---\n- hosts: webser\n  remote_user: root\n\n  tasks:\n    - name: <span class=\"hljs-keyword\">set<\/span> hostname\n      hostname: name={{ hostname }}-{{ http_port}}\n[root@ansible ansible]<span class=\"hljs-comment\"># ansible webser -a 'hostname'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\nweb1-<span class=\"hljs-number\">88<\/span>\n\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\nweb2-<span class=\"hljs-number\">86<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<\/ul>\n<ul>\n<li>\u5982\u679c\u6211\u4eec\u4eba\u4e3a\u7684\u7528-e\u6307\u5b9a\u53d8\u91cf\u503c\uff0c\u5219\u4f1a\u4ee5-e\u5b9a\u4e49\u7684\u53c2\u6570\u4e3a\u51c6\uff0c\u4e0d\u4f1a\u8c03\u7528hosts\u4e2d\u7684\u53d8\u91cf<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># ansible-playbook -e \"http_port=9090 hostname=abc.com\" var4.yml<\/span>\n[root@ansible ansible]<span class=\"hljs-comment\"># ansible webser -a 'hostname'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\nabc.com-<span class=\"hljs-number\">9090<\/span>\n\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\nabc.com-<span class=\"hljs-number\">9090<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<\/ul>\n<h3 id=\"\u53d8\u91cf\"><a name=\"t44\"><\/a>\u53d8\u91cf<\/h3>\n<ul>\n<li>\u4e3b\u673a\u53d8\u91cf\n<ul>\n<li>\u53ef\u4ee5\u5728inventory\u4e2d\u5b9a\u4e49\u4e3b\u673a\u65f6\u4e3a\u5176\u6dfb\u52a0\u4e3b\u673a\u53d8\u91cf\u4ee5\u4fbf\u4e8e\u5728playbook\u4e2d\u4f7f\u7528<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[websrvs]\nwww1.magedu.com http_port=<span class=\"hljs-number\">80<\/span> maxRequestsPerChild=<span class=\"hljs-number\">808<\/span>\nwww2.magedu.com http_port=<span class=\"hljs-number\">8080<\/span> maxRequestsPerChild=<span class=\"hljs-number\">909<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<\/ul>\n<ul>\n<li>\u7ec4\u53d8\u91cf\n<ul>\n<li>\u7ec4\u53d8\u91cf\u662f\u6307\u8d4b\u4e88\u7ed9\u6307\u5b9a\u7ec4\u5185\u6240\u6709\u4e3b\u673a\u4e0a\u7684\u5728playbook\u4e2d\u53ef\u7528\u7684\u53d8\u91cf<\/li>\n<\/ul>\n<\/li>\n<li>\u8303\u4f8b<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[websrvs]\nwww1.magedu.com\nwww2.magedu.com\n\n[websrvs:vars]     \u8fd9\u4e2a\u76f8\u5f53\u4e8e\u662f\u5728websrvs\u7ec4\u4e2d\u7684\u6240\u6709\u4e3b\u673a\u516c\u7528\u7684\uff0c\u53eb\u505a\u7ec4\u516c\u5171\u53d8\u91cf\nntp_server=ntp.magedu.com\nnfs_server=nfs.magedu.com<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<\/ul>\n<p><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/ibEi23D64H.png\" alt=\"mark\" \/><\/p>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim var4.yml<\/span>\n---\n- hosts: webser\n  remote_user: root\n\n  tasks:\n    - name: <span class=\"hljs-keyword\">set<\/span> hostname\n      hostname: name={{ hname }}-{{ http_port}}\n[root@ansible ansible]<span class=\"hljs-comment\"># ansible-playbook  var4.yml <\/span>\n[root@ansible ansible]<span class=\"hljs-comment\"># ansible webser -a 'hostname'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\nansible-<span class=\"hljs-number\">91<\/span>\n\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\nansible-<span class=\"hljs-number\">90<\/span>\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<\/ul>\n<ul>\n<li>\u666e\u901a\u53d8\u91cf<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[websrvs]\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">99.101<\/span> http_port=<span class=\"hljs-number\">8080<\/span> hname=www1\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">99.102<\/span> http_port=<span class=\"hljs-number\">80<\/span> hname=www2<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<\/ul>\n<ul>\n<li>\u516c\u5171\u53d8\u91cf<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[websvrs:vars]\nhttp_port=<span class=\"hljs-number\">808<\/span>\nmark=\u201c_\u201d\n[websrvs]\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">99.101<\/span> http_port=<span class=\"hljs-number\">8080<\/span> hname=www1\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">99.102<\/span> http_port=<span class=\"hljs-number\">80<\/span> hname=www2\n<span class=\"hljs-comment\"># ansible websvrs \u2013m hostname \u2013a \u2018name={{ hname }}{{ mark }}{{http_port }}\u2019<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<\/ul>\n<ul>\n<li>\u547d\u4ee4\u884c\u6307\u5b9a\u53d8\u91cf<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">ansible websvrs \u2013e http_port=<span class=\"hljs-number\">8000<\/span> \u2013m hostname \u2013a \u2018name={{hname }}{{ mark }}{{ http_port }}\u2019<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<\/ul>\n<h3 id=\"\u4e5f\u53ef\u4ee5\u628a\u53d8\u91cf\u4e13\u95e8\u5199\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\u53ef\u4ee5\u8ba9\u522b\u7684playbook\u8c03\u7528\u6b64\u53d8\u91cf\"><a name=\"t45\"><\/a>\u4e5f\u53ef\u4ee5\u628a\u53d8\u91cf\u4e13\u95e8\u5199\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u8ba9\u522b\u7684playbook\u8c03\u7528\u6b64\u53d8\u91cf<\/h3>\n<ul>\n<li>\u540d\u5b57\u5fc5\u987b\u662f.yml\u540e\u7f00\u7684<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># vim add.yml<\/span>\nhost: <span class=\"hljs-number\">123<\/span>  \u5728\u6587\u4ef6\u4e2d\u5b9a\u4e49\u53d8\u91cf\u662f\u8fd9\u4e2d\u683c\u5f0f host\u662f\u53d8\u91cf\u540d \uff1a\u540e\u9762\u662f\u503c \u8fd9\u662f\u548c\u4e0a\u9762\u5b9a\u4e49\u53d8\u91cf\u662f\u7528\u533a\u522b\u7684\nfilen: <span class=\"hljs-number\">456<\/span>\n[root@ansible ansible]<span class=\"hljs-comment\"># vim var5.yml<\/span>\n---\n- hosts: cen7\n  remote_user: root\n  vars_files:   \u4e00\u5b9a\u8981\u5199vars_files\n    - \/root\/add.yml   \u8fd9\u91cc\u662f\u5b9a\u4e49\u53d8\u91cf\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u8c03\u7528\u91cc\u9762\u7684\u53d8\u91cf\u4e86\n\n  tasks:\n   - name: create file\n     file: name=\/app\/{{host}}-{{filen}}.log  state=touch  mode=<span class=\"hljs-number\">600<\/span> owner=guo\n[root@ansible ansible]<span class=\"hljs-comment\"># ansible cen7 -a 'ls \/app\/'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\n<span class=\"hljs-number\">123<\/span>-<span class=\"hljs-number\">456<\/span>.log\n\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS | rc=<span class=\"hljs-number\">0<\/span> &gt;&gt;\n<span class=\"hljs-number\">123<\/span>-<span class=\"hljs-number\">456<\/span>.log\nf2\nf3<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<li>20<\/li>\n<li>21<\/li>\n<\/ul>\n<h3 id=\"\u6a21\u677ftemplates\"><a name=\"t46\"><\/a>\u6a21\u677ftemplates<\/h3>\n<ul>\n<li>templates \u662f\u4e00\u4e2a\u6a21\u5757\n<ul>\n<li>\u6a21\u677f\u5b58\u653e\u7740\u5c06\u6765\u8981\u5b9e\u73b0\u7684\u914d\u7f6e\u6587\u4ef6\u7684\u516c\u7528\u8bbe\u7f6e\u653e\u5728\u6a21\u677f\u91cc\uff0c\u5fc5\u987b\u662f\u7279\u6b8a\u683c\u5f0f\u7684\uff0c\u53ef\u4ee5\u6839\u636e\u4e00\u4e9b\u53d8\u91cf\u6765\u6839\u636e\u4e3b\u673a\u751f\u6210\u914d\u7f6e\u6587\u4ef6<\/li>\n<\/ul>\n<\/li>\n<li>\u6587\u672c\u6587\u4ef6\uff0c\u5d4c\u5957\u6709\u811a\u672c\uff08\u4f7f\u7528\u6a21\u677f\u7f16\u7a0b\u8bed\u8a00\u7f16\u5199\uff09<\/li>\n<li>Jinja2\u8bed\u8a00\uff0c\u4f7f\u7528\u5b57\u9762\u91cf\uff0c\u6709\u4e0b\u9762\u5f62\u5f0f<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bahs hljs r has-numbering\">\u5b57\u7b26\u4e32\uff1a\u4f7f\u7528\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\n\u6570\u5b57\uff1a\u6574\u6570\uff0c\u6d6e\u70b9\u6570\n\u5217\u8868\uff1a[item1, item2, <span class=\"hljs-keyword\">...<\/span>]\n\u5143\u7ec4\uff1a(item1, item2, <span class=\"hljs-keyword\">...<\/span>)\n\u5b57\u5178\uff1a{key1:value1, key2:value2, <span class=\"hljs-keyword\">...<\/span>}\n\u5e03\u5c14\u578b\uff1atrue\/false<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<\/ul>\n<ul>\n<li>\u7b97\u672f\u8fd0\u7b97\uff1a+, -,\u00a0<em>, \/, \/\/, %, *<\/em><\/li>\n<li>\u6bd4\u8f83\u64cd\u4f5c\uff1a==, !=, &gt;, &gt;=, &lt;, &lt;=<\/li>\n<li>\u903b\u8f91\u8fd0\u7b97\uff1aand, or, not<\/li>\n<li>\u6d41\u8868\u8fbe\u5f0f\uff1aFor If When<\/li>\n<li>templates\u529f\u80fd\uff1a\u6839\u636e\u6a21\u5757\u6587\u4ef6\u52a8\u6001\u751f\u6210\u5bf9\u5e94\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u590d\u5236\u5230\u8fdc\u7aef\u4e3b\u673a\u4e0a\n<ul>\n<li>templates\u6587\u4ef6\u5fc5\u987b\u5b58\u653e\u4e8etemplates\u76ee\u5f55\u4e0b\uff0c\u4e14\u547d\u540d\u4e3a .j2 \u7ed3\u5c3e<\/li>\n<li>yaml\/yml \u6587\u4ef6\u9700\u548ctemplates\u76ee\u5f55\u5e73\u7ea7\uff0c\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">.\/\n\u251c\u2500\u2500 temnginx.yml\n\u2514\u2500\u2500 templates\n    \u2514\u2500\u2500 nginx.conf.j2   <\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<\/ul>\n<h4 id=\"\u8303\u4f8b\">\u8303\u4f8b<\/h4>\n<ul>\n<li>\u5229\u7528templates \u540c\u6b65nginx\u914d\u7f6e\u6587\u4ef6\n<ul>\n<li>\u51c6\u5907templates\/nginx.conf.j2\u6587\u4ef6<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\"><span class=\"hljs-comment\"># vim temnginx.yml<\/span>\n---\n- hosts: websrvs\n  remote_user: root\n  tasks:\n   - name: template config to remote hosts\n     template: src=nginx.conf.j2 dest=\/etc\/nginx\/nginx.conf\n<span class=\"hljs-comment\"># ansible-playbook temnginx.yml<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\u9996\u5148\u8981\u5728\u672c\u673a\u4e0a\u5b89\u88c5\u76f8\u540c\u7684\u670d\u52a1\uff0c\u7136\u540e\u628a\u914d\u7f6e\u6587\u4ef6\u590d\u5236\u4e00\u4efd\u5230template\u76ee\u5f55\u4e0b\u6539\u540e\u7f00\u4e3aj2\n<span class=\"hljs-number\">1<\/span>. [root@ansible ansible]<span class=\"hljs-comment\"># cp \/etc\/nginx\/nginx.conf templates\/nginx.conf.j2   \u9996\u5148\u628a\u8981\u51c6\u5907\u4e00\u4efd\u6a21\u677f\u6587\u4ef6\uff0c\u540e\u7f00\u540d\u5fc5\u987b\u662fj2<\/span>\n[root@ansible ansible]<span class=\"hljs-comment\"># vim temnginx.yml<\/span>\n---\n- hosts: cen7\n  remote_user: root\n\n  tasks:\n   - name: install nginx\n     yum: name=nginx\n   - name: template\n     template: src=nginx.conf.j2 dest=\/etc\/nginx\/nginx.conf    \/\/src=\u8fd9\u4e2a\u53ef\u4ee5\u5199\u7edd\u5bf9\u8def\u5f84\uff0c\u4e5f\u53ef\u4ee5\u8fd9\u6837\u5199\uff0c\u56e0\u4e3a\u6211\u7684template\u76ee\u5f55\u548c\u8fd9playbook\u6587\u4ef6\u662f\u5728\u540c\u4e00\u76ee\u5f55\uff0c\u9ed8\u8ba4play\u4f1a\u5230template\u76ee\u5f55\u4e0b\u627e\u3002\n     tags: instconf\n   - name: start service\n     service: name=nginx state=started\n[root@ansible ansible]<span class=\"hljs-comment\"># ansible-playbook temnginx.yml  \u8fd0\u884c\u8fd9\u4e2aplaybook,\u5b89\u88c5\u670d\u52a1\uff0c\u5e76\u751f\u6210\u6a21\u677f\u6587\u4ef6<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<\/ul>\n<ol>\n<li>\u4fee\u6539\u6a21\u677f\u6587\u4ef6<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/8Kla13CGci.png\" alt=\"mark\" \/><\/li>\n<li>\u67e5\u770b\u4e00\u4e0b\u9ed8\u8ba4\u662f\u51e0\u4e2a\u8fdb\u7a0b\u53f7\u53d1\u73b0\u662f\u4e00\u4e2a\u8fdb\u7a0b<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/FAjHj04ac2.png\" alt=\"mark\" \/><\/li>\n<li>\u6211\u4eec\u4fee\u6539\u4e00\u4e0bCPU\u7684\u6570\u91cf\u5728\u770b\u4e00\u4e0b\u7ed3\u679c,\u53ea\u4fee\u6539\u4e00\u4e2a\u4e3b\u673a\u4e0a\u7684CPU\u4e2a\u6570<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/0Kkd57Amd0.png\" alt=\"mark\" \/><\/li>\n<li>\u5728setup\u6a21\u5757\u4e2d\u6709\u4e00\u4e2a\u53d8\u91cf\u662f\u663e\u793aCPU\u4e2a\u6570\u7684\u53d8\u91cf<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># ansible cen7 -m setup -a 'filter=\"*cpu*\"'<\/span>\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.101<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"ansible_facts\"<\/span>: {\n        <span class=\"hljs-string\">\"ansible_processor_vcpus\"<\/span>: <span class=\"hljs-number\">1<\/span>   \u8fd9\u4e2a\u5c31\u662f\u663e\u793aCPU\u53d8\u91cf\u7684\n    },\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">false<\/span>\n}\n<span class=\"hljs-number\">192.168<\/span>.<span class=\"hljs-number\">27.102<\/span> | SUCCESS =&gt; {\n    <span class=\"hljs-string\">\"ansible_facts\"<\/span>: {\n        <span class=\"hljs-string\">\"ansible_processor_vcpus\"<\/span>: <span class=\"hljs-number\">4<\/span>\n    },\n    <span class=\"hljs-string\">\"changed\"<\/span>: <span class=\"hljs-literal\">false<\/span>\n}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<\/ul>\n<ol>\n<li>\u4fee\u6539\u6a21\u677f\u6587\u4ef6<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/lcHCj1GdBA.png\" alt=\"mark\" \/><\/p>\n<ul>\n<li>\u8fd9\u6837\u5c31\u4f1a\u53d8\u6210\uff0c\u6a21\u677f\u662f\u4e00\u6837\u7684\uff0c\u4f46\u662f\u5230\u5404\u4e2a\u4e3b\u673a\u4e0a\u56de\u5427ansible_processor_vcpus\u8fd9\u4e2a\u53d8\u91cf\u4f1a\u53d8\u6210CPU\u4e2a\u6570\uff0c\u673a\u5668\u6709\u662f\u51e0\u4e2aCPU\u5c31\u66ff\u6362\u6210\u51e0\uff0c\u8fd9\u6837\u751f\u6210\u7684\u914d\u7f6e\u6587\u4ef6\u4e5f\u4e0d\u540c\u3002<\/li>\n<li>\u521a\u521a\u4e0a\u9762\u7684playbook\u5df2\u7ecf\u5c06\u670d\u52a1\u542f\u52a8\u4e86\uff0c\u6240\u4ee5\u6211\u4eec\u5728\u4fee\u6539\u4e00\u4e0bplaybook,\u5728template\u4e0b\u52a0\u4e00\u4e2anotify\uff0c\u5f53template\u8fd9\u4e2a\u4efb\u52a1\u6267\u884c\u65f6\u4f1a\u89e6\u53d1\u6307\u793a\u7684\u4efb\u52a1<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim temnginx.yml <\/span>\n---\n- hosts: cen7\n  remote_user: root\n\n  tasks:\n   - name: install nginx\n     yum: name=nginx\n   - name: template\n     template: src=nginx.conf.j2 dest=\/etc\/nginx\/nginx.conf\n     notify: restart nginx\n     tags: instconf\n   - name: start service\n     service: name=nginx state=started\n\n  handlers:\n   - name: restart nginx\n     service: name=nginx state=restarted<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<\/ul>\n<ul>\n<li>\u73b0\u5728\u6267\u884c\u4e00\u4e0bplaybook\uff0c\u6267\u884c\u5b8c\u540e\u6211\u4eec\u53ef\u4ee5\u770b\u4e00\u4e0b\u662f\u4e0d\u662f\u6309CPU\u6570\u6765\u521b\u5efa\u8fdb\u7a0b\u7684<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/J8AcJ5e8LL.png\" alt=\"mark\" \/><\/li>\n<li>\u786e\u5b9e4\u9897CPU\u5c31\u662f4\u4e2a\u8fdb\u7a0b\u6570\uff0c1\u4e2aCPU\u5c31\u662f\u4e00\u4e2a\uff0c\u8fd9\u5c31\u662ftemplate\u6a21\u677f\u7684\u5f3a\u5927\u4e4b\u5904<\/li>\n<li>\u770b\u4e00\u4e0b\u5404\u81ea\u7684\u914d\u7f6e\u6587\u4ef6<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/FIh9812E6c.png\" alt=\"mark\" \/><br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/ea7iaH4j6C.png\" alt=\"mark\" \/><\/li>\n<li>\u8fd8\u53ef\u4ee5\u505a\u8fd0\u7b97<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/IfiJ97geiH.png\" alt=\"mark\" \/><\/li>\n<li>\u6211\u4eec*2\u628aCPUx2\uff0c\u6267\u884c\u4e00\u4e0bplaybook<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/48Khe38A98.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<h2 id=\"playbook\u4e2dtemplate\u53d8\u66f4\u66ff\u6362\"><a name=\"t47\"><\/a>Playbook\u4e2dtemplate\u53d8\u66f4\u66ff\u6362<\/h2>\n<ul>\n<li>\u4fee\u6539\u6587\u4ef6nginx.conf.j2 \u4e0b\u9762\u884c\u4e3a<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">worker_processes {{ ansible_processor_vcpus }};<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\"><span class=\"hljs-comment\"># cat temnginx2.yml<\/span>\n---\n- hosts: websrvs\nremote_user: root\ntasks:\n  - name: template config to remote hosts\n    template: src=nginx.conf.j2 dest=\/etc\/nginx\/nginx.conf\n<span class=\"hljs-comment\"># ansible-playbook temnginx2.yml<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<\/ul>\n<h3 id=\"playbook\u4e2dtemplate\u7b97\u672f\u8fd0\u7b97\"><a name=\"t48\"><\/a>Playbook\u4e2dtemplate\u7b97\u672f\u8fd0\u7b97<\/h3>\n<ul>\n<li>\u7b97\u6cd5\u8fd0\u7b97<\/li>\n<li>\u8303\u4f8b<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\"><span class=\"hljs-comment\"># vim nginx.conf.j2<\/span>\nworker_processes {{ ansible_processor_vcpus*<span class=\"hljs-number\">2<\/span> }};\nworker_processes {{ ansible_processor_vcpus+<span class=\"hljs-number\">2<\/span> }};<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<\/ul>\n<h3 id=\"template\u4e5f\u652f\u6301\u5728\u4e3b\u673a\u6e05\u5355\u4e2d\u53d8\u91cf\"><a name=\"t49\"><\/a>template\u4e5f\u652f\u6301\u5728\u4e3b\u673a\u6e05\u5355\u4e2d\u53d8\u91cf<\/h3>\n<p><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/22D2mB4HDi.png\" alt=\"mark\" \/><br \/>\n&#8211; \u4fee\u6539\u6a21\u677f\u6587\u4ef6<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/DDK24JK23h.png\" alt=\"mark\" \/><br \/>\n&#8211; \u6267\u884cplaybook<br \/>\n&#8211; \u67e5\u770b\u76d1\u542c\u7aef\u53e3\u662f\u5426\u6539\u53d8<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/D6Da468kec.png\" alt=\"mark\" \/><br \/>\n&#8211; \u6211\u4eec\u4e5f\u53ef\u4ee5\u5728playbook\u4e2d\u5b9a\u4e49\u53d8\u91cf\u8ba9\u6a21\u677f\u8c03\u7528<\/p>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim temnginx.yml <\/span>\n---\n- hosts: cen7\n  remote_user: root\n  vars:\n    - http_port: <span class=\"hljs-number\">6060<\/span>    \u5728playbook\u4e2d\u5b9a\u4e49http_port\u53d8\u91cf\u503c\u4e3a<span class=\"hljs-number\">6060<\/span>\n\n  tasks:\n   - name: install nginx\n     yum: name=nginx\n   - name: template\n     template: src=nginx.conf.j2 dest=\/etc\/nginx\/nginx.conf\n     notify: restart nginx\n     tags: instconf\n   - name: start service\n     service: name=nginx state=started\n\n  handlers:\n   - name: restart nginx\n     service: name=nginx state=restarted<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<li>20<\/li>\n<\/ul>\n<ul>\n<li>\u73b0\u5728\u6709\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u6211\u4eec\u5728\u4e3b\u673a\u6e05\u5355\u4e2d\u4e5f\u5b9a\u4e49\u4e86\u540c\u6837\u540d\u5b57\u7684\u53d8\u91cf\uff0c\u8fd9\u79cd\u60c5\u51b5\u4f1a\u4ee5playbook\u4e2d\u7684\u53d8\u91cf\u4f1a\u751f\u6548\uff0c\u5982\u679c\u5728\u547d\u4ee4\u884c\u4e2d\u7528-e\u4e5f\u5b9a\u4e49\u4e86\u540c\u6837\u540d\u5b57\u7684\u53d8\u91cf\u5219\u547d\u4ee4\u884c\u7684\u4f18\u5148<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/4acBgHDa8L.png\" alt=\"mark\" \/><\/li>\n<\/ul>\n<h4 id=\"\u53d8\u91cf\u53c2\u6570\u7684\u4f18\u5148\u7ea7\">\u53d8\u91cf\u53c2\u6570\u7684\u4f18\u5148\u7ea7<\/h4>\n<p>\u547d\u4ee4\u884c\u5b9a\u4e49\u53d8\u91cf&gt; playbook\u4e2d\u5b9a\u4e49\u7684&gt; hosts\u4e3b\u673a\u6e05\u5355\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf&gt; \u6a21\u677f\u6587\u4ef6\u4e2d<\/p>\n<h2 id=\"when\"><a name=\"t50\"><\/a>when<\/h2>\n<ul>\n<li>\u6761\u4ef6\u6d4b\u8bd5:\u5982\u679c\u9700\u8981\u6839\u636e\u53d8\u91cf\u3001facts\u6216\u6b64\u524d\u4efb\u52a1\u7684\u6267\u884c\u7ed3\u679c\u6765\u505a\u4e3a\u67d0task\u6267\u884c\u4e0e\u5426\u7684\u524d\u63d0\u65f6\u8981\u7528\u5230\u6761\u4ef6\u6d4b\u8bd5,\u901a\u8fc7when\u8bed\u53e5\u5b9e\u73b0\uff0c\u5728task\u4e2d\u4f7f\u7528\uff0cjinja2\u7684\u8bed\u6cd5\u683c\u5f0f<\/li>\n<li>when\u8bed\u53e5<\/li>\n<li>\u5728task\u540e\u6dfb\u52a0when\u5b50\u53e5\u5373\u53ef\u4f7f\u7528\u6761\u4ef6\u6d4b\u8bd5\uff1bwhen\u8bed\u53e5\u652f\u6301Jinja2\u8868\u8fbe\u5f0f\u8bed\u6cd5<\/li>\n<li>\u793a\u4f8b<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">tasks:\n  - name: <span class=\"hljs-string\">\"shutdown RedHat flavored systems\"<\/span>\n    command: \/sbin\/shutdown -h now\n    when: ansible_os_family == <span class=\"hljs-string\">\"RedHat\"<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<\/ul>\n<ul>\n<li>\u793a\u4f8b\uff1awhen\u6761\u4ef6\u5224\u65ad<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">---\n\n- hosts: websrvs\n  remote_user: root\n  tasks:\n    - name: add group nginx\n      tags: user\n      user: name=nginx state=present\n    - name: add user nginx\n      user: name=nginx state=present group=nginx\n    - name: Install Nginx\n      yum: name=nginx state=present\n    - name: restart Nginx\n      service: name=nginx state=restarted\n      when: ansible_distribution_major_version == <span class=\"hljs-string\">\"6\"<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<\/ul>\n<ul>\n<li>\u793a\u4f8b<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">tasks:\n  - name: install conf file to centos7\n    template: src=nginx.conf.c7.j2\n    when: ansible_distribution_major_version == <span class=\"hljs-string\">\"7\"<\/span>\n  - name: install conf file to centos6\n    template: src=nginx.conf.c6.j2\n    when: ansible_distribution_major_version == <span class=\"hljs-string\">\"6\"<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b<\/li>\n<li>\u5f53\u7cfb\u7edf\u7248\u672c\u4e0d\u540c\uff0c\u670d\u52a1\u7248\u672c\u4e5f\u4f1a\u4e0d\u540c\uff0c\u5c31httpd\u670d\u52a1\uff0c7\u7248\u672c\u548c6\u7248\u672c\u4e0d\u540c\uff0c\u5982\u679c\u5728\u7ba1\u7406\u4e3b\u673a\u4e2d\u67096\u7684\u673a\u5668\uff0c\u4e5f\u6709centos7\u7684\u673a\u5668\uff0c\u6211\u4eec\u5728\u7528\u6a21\u677f\u6587\u4ef6\u521b\u5efa\u914d\u7f6e\u6587\u4ef6\u65f6\u5c31\u9700\u8981\u5224\u65ad\u4e00\u4e0b\u7cfb\u7edf\u662f\u51e0\u7684\u5c31\u7528\u5bf9\u5e94\u7684\u914d\u7f6e\u6a21\u677f<\/li>\n<li>\u9996\u5148\u628acentos7 \u7248\u672c\u548c6\u7248\u672c\u7684httpd\u914d\u7f6e\u6587\u4ef6\u590d\u5236\u4e00\u4efd\u5230template\u6587\u4ef6\u5939\u4e2d\u5f53\u6a21\u677f\u6587\u4ef6<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/ILl1Lh7aEJ.png\" alt=\"mark\" \/><\/li>\n<li>\u73b0\u5728\u5df2\u7ecf\u627e\u5230\u4e86\u7248\u672c\u53f7\u6211\u4eec\u5728playbook\u4e2d\u7528when\u5224\u65ad<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim when.yml                 <\/span>\n---\n- hosts: all\n  remote_user: root\n\n  tasks:\n   - name: install httpd\n     yum: name=httpd\n\n   - name: template\n\u25bd    template: src=httpd-<span class=\"hljs-number\">6<\/span>.conf.j2 dest=\/etc\/httpd\/conf\/httpd.conf\n     when: ansible_distribution_major_version==<span class=\"hljs-string\">\"6\"<\/span>    \u5f53\u7248\u672c==<span class=\"hljs-number\">6<\/span>\u65f6\u4f1a\u6267\u884c\uff0c\u4e0d\u7136\u5c31\u4e0d\u6267\u884c\n\n   - name: template\n     template: src=httpd-<span class=\"hljs-number\">7<\/span>.conf.j2 dest=\/etc\/httpd\/conf\/httpd.conf\n     when: ansible_distribution_major_version==<span class=\"hljs-string\">\"7\"<\/span>\n\n   - name: start service\n     service: name=httpd state=started<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<\/ul>\n<p><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180116\/2b7a2l0c4H.png\" alt=\"mark\" \/><\/p>\n<h2 id=\"\u8fed\u4ee3withitems\u7c7b\u4f3c\u4e8e\u5faa\u73af\"><a name=\"t51\"><\/a>\u8fed\u4ee3\uff1awith_items\uff08\u7c7b\u4f3c\u4e8e\u5faa\u73af\uff09<\/h2>\n<ul>\n<li>\u8fed\u4ee3\uff1a\u5f53\u6709\u9700\u8981\u91cd\u590d\u6027\u6267\u884c\u7684\u4efb\u52a1\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fed\u4ee3\u673a\u5236\n<ul>\n<li>\u5bf9\u8fed\u4ee3\u9879\u7684\u5f15\u7528\uff0c\u56fa\u5b9a\u53d8\u91cf\u540d\u4e3a\u201ditem\u201c<\/li>\n<li>\u8981\u5728task\u4e2d\u4f7f\u7528with_items\u7ed9\u5b9a\u8981\u8fed\u4ee3\u7684\u5143\u7d20\u5217\u8868<\/li>\n<li>\u5217\u8868\u683c\u5f0f\uff1a\n<ul>\n<li>\u5b57\u7b26\u4e32<\/li>\n<li>\u5b57\u5178<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\u793a\u4f8b\uff0c\u5728\u7cfb\u7edf\u4e2d\u521b\u5efa\u82e5\u5e72\u7528\u6237<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">- name: add several users\n  user: name={{ item }} state=present groups=wheel   \/\/item\u662f\u4e2a\u5173\u952e\u5b57\uff0c\u5fc5\u987b\u8fd9\u6837\u5199\n  with_items:\n    - testuser1\n    - testuser2<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<\/ul>\n<ul>\n<li>\u4e0a\u9762\u8bed\u53e5\u7684\u529f\u80fd\u7b49\u540c\u4e8e\u4e0b\u9762\u7684\u8bed\u53e5<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">- name: add user testuser1\n  user: name=testuser1 state=present groups=wheel\n- name: add user testuser2\n  user: name=testuser2 state=present groups=wheel<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<\/ul>\n<ul>\n<li>\u521b\u5efa\u591a\u4e2a\u7528\u6237<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim with_items.yml<\/span>\n---\n- hosts: cen7\n  remote_user: root\n\n  tasks:\n   - name: create servel users\n     user: name={{ item }} group=root groups=guo\n     with_items:\n       - xiaoming\n       - xiaowang\n       - xiaoguo\n````\n- \u793a\u4f8b\uff1a\u5c06\u591a\u4e2a\u6587\u4ef6\u8fdb\u884ccopy\u5230\u88ab\u63a7\u7aef\n\n\n\n\n&lt;div class=<span class=\"hljs-string\">\"se-preview-section-delimiter\"<\/span>&gt;&lt;\/div&gt;\n\n```bash\n---\n- hosts: testsrv\n  remote_user: root\n\n  tasks\n  - name: Create rsyncd config\n    copy: src={{ item }} dest=\/etc\/{{ item }}\n    with_items:\n        - rsyncd.secrets\n        - rsyncd.conf<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<li>20<\/li>\n<li>21<\/li>\n<li>22<\/li>\n<li>23<\/li>\n<li>24<\/li>\n<li>25<\/li>\n<li>26<\/li>\n<li>27<\/li>\n<li>28<\/li>\n<li>29<\/li>\n<li>30<\/li>\n<li>31<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">- hosts: websrvs\n  remote_user: root\n  tasks:\n    - name: copy file\n      copy: src={{ item }} dest=\/tmp\/{{ item }}\n      with_items:\n        - file1\n        - file2\n        - file3\n\n    - name: yum install httpd\n      yum: name={{ item }} state=present\n      with_items:\n        - apr\n        - apr-util\n        - httpd<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">- hosts\uff1awebsrvs\n  remote_user: root\n  tasks\n    - name: install some packages\n      yum: name={{ item }} state=present\n      with_items:\n        - nginx\n        - memcached\n        - php-fpm<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<\/ul>\n<h4 id=\"\u793a\u4f8b\u8fed\u4ee3\u5d4c\u5957\u5b50\u53d8\u91cf\">\u793a\u4f8b\uff1a\u8fed\u4ee3\u5d4c\u5957\u5b50\u53d8\u91cf<\/h4>\n<ul>\n<li>\u5f53\u6709\u591a\u4e2a\u53d8\u91cf\u65f6\u53ef\u4ee5\u7528\u5b50\u53d8\u91cf<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">- hosts\uff1awebsrvs\n  remote_user: root\n  tasks:\n    - name: add some groups\n      group: name={{ item }} state=present\n      with_items:\n        - group1\n        - group2\n        - group3\n    - name: add some users\n      user: name={{ item.name }} group={{ item.group }} state=present\n      with_items:\n        - { name: <span class=\"hljs-string\">'user1'<\/span>, group: <span class=\"hljs-string\">'group1'<\/span> }\n        - { name: <span class=\"hljs-string\">'user2'<\/span>, group: <span class=\"hljs-string\">'group2'<\/span> }\n        - { name: <span class=\"hljs-string\">'user3'<\/span>, group: <span class=\"hljs-string\">'group3'<\/span> }<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b2<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># vim qiantaoitem.yml                 <\/span>\n---\n- hosts: cen7\n  remote_user: root\n\n  tasks:\n   - name: create group\n     group: name={{ item }}\n     with_items:\n      - igroup1\n      - igroup2\n      - igroup3\n\n   - name: create user\n     user: name={{item.username }} group={{item.groupname}}\n     with_items:\n      - {username: <span class=\"hljs-string\">'t1'<\/span>, groupname: <span class=\"hljs-string\">'igroup1'<\/span> }   \u4e00\u884c\u6709\u4e24\u4e2a\u53d8\u91cf\uff0c\u7528\u9017\u53f7\u9694\u5f00\u5192\u53f7\u540e\u9762\u6709\u7a7a\u683c\n      - {username: <span class=\"hljs-string\">'t2'<\/span>, groupname: <span class=\"hljs-string\">'igroup2'<\/span> }\n      - {username: <span class=\"hljs-string\">'t3'<\/span>, groupname: <span class=\"hljs-string\">'igroup3'<\/span> }<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<\/ul>\n<h2 id=\"playbook\u4e2dtemplate-for-if\"><a name=\"t52\"><\/a>Playbook\u4e2dtemplate for if<\/h2>\n<ul>\n<li>for\u8bed\u53e5<\/li>\n<li>\u683c\u5f0f<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">{%<span class=\"hljs-keyword\">for<\/span> \u53d8\u91cf <span class=\"hljs-keyword\">in<\/span> \u5217\u8868%}\n    \u5faa\u73af\u4f53\n{%endfor}\n\u8fd9\u79cd\u662f\u7528\u4e8etemplate\u6a21\u677f\u4e2d\uff0c\u5728playbook\u4e2d\u5b9a\u4e49\u5217\u8868<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<\/ul>\n<ul>\n<li>\u8303\u4f8b<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim for.yml<\/span>\n---\n- hosts: cen7\n  remote_user: root\n  vars:\n\u25bd   ports:        \/\/\u8fd9\u91cc\u5b9a\u4e49\u5217\u8868\u540d\uff0c\u91cc\u9762\u6709\u51e0\u4e2a\u503c<span class=\"hljs-keyword\">for<\/span>\u5c31\u5faa\u73af\u51e0\u904d\n      - <span class=\"hljs-number\">81<\/span>\n      - <span class=\"hljs-number\">82<\/span>\n      - <span class=\"hljs-number\">83<\/span>\n\n  tasks:\n    - name: test <span class=\"hljs-keyword\">for<\/span>1\n      template: src=<span class=\"hljs-keyword\">for<\/span>1.conf.j2 dest=\/app\/<span class=\"hljs-keyword\">for<\/span>1.conf\n\n\n[root@ansible ansible]<span class=\"hljs-comment\"># vim templates\/for1.conf.j2   \u5728\u6a21\u677f\u6587\u4ef6\u91cc\u5199for\u5faa\u73af<\/span>\n{%<span class=\"hljs-keyword\">for<\/span> port <span class=\"hljs-keyword\">in<\/span> ports %}     port\u7c7b\u4f3cshell,<span class=\"hljs-keyword\">for<\/span>\u5faa\u73af\u4e2d\u7684i\u53ef\u4ee5\u968f\u4fbf\u5199\uff0cports\u5fc5\u987b\u5199\u5217\u8868\u540d\nserver {                         \u8fd9\u91cc\u662f\u5faa\u73af\u4f53\n        listen {{port}};         \u8fd9\u91cc\u8c03\u7528{{port}}\u8fd9\u91cc\u7684\u503c\u5c31\u662f\u5217\u8868\u91cc\u7684\u503c\n}\n{%endfor%}     \u7ed3\u675f\u5faa\u73af\u4f53\n\n\u8fd0\u884cplaybook\u6587\u4ef6\n[root@ansible-<span class=\"hljs-number\">90<\/span> ~]<span class=\"hljs-comment\"># cat \/app\/for1.conf <\/span>\nserver {\n        listen <span class=\"hljs-number\">81<\/span>;\n}\nserver {\n        listen <span class=\"hljs-number\">82<\/span>;\n}\nserver {\n        listen <span class=\"hljs-number\">83<\/span>;\n}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<li>20<\/li>\n<li>21<\/li>\n<li>22<\/li>\n<li>23<\/li>\n<li>24<\/li>\n<li>25<\/li>\n<li>26<\/li>\n<li>27<\/li>\n<li>28<\/li>\n<li>29<\/li>\n<li>30<\/li>\n<li>31<\/li>\n<li>32<\/li>\n<li>33<\/li>\n<\/ul>\n<ul>\n<li>\u7b2c\u4e8c\u79cdfor\u5199\u6cd5<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim for2.yml<\/span>\n---\n- hosts: cen7\n  remote_user: root\n  vars:\n    ports:  \u8fd9\u662f\u5217\u8868\u540d\n       - http_port: <span class=\"hljs-number\">81<\/span>      \u5199\u6210\u8fd9\u6837\u76f8\u5f53\u4e8e\u5b50\u53d8\u91cf\n       - http_port: <span class=\"hljs-number\">82<\/span>\n       - http_port: <span class=\"hljs-number\">83<\/span>\n\n  tasks:\n\u25bd  - name: template file\n     template: src=<span class=\"hljs-keyword\">for<\/span>2.conf.j2 dest=\/app\/<span class=\"hljs-keyword\">for<\/span>2.conf\n\u6a21\u677f\u6587\u4ef6\n[root@ansible ansible]<span class=\"hljs-comment\"># vim templates\/for2.conf.j2<\/span>\n{%<span class=\"hljs-keyword\">for<\/span> port <span class=\"hljs-keyword\">in<\/span> ports %}\nserver {\n        listen {{port.http_port}};   \u8fd9\u79cd\u7c7b\u4f3c\u4e8eitem\u7684\u5199\u6cd5port\u76f8\u5f53\u4e8ehttp_port,\u7136\u540e\u628ahttp_port\u7684\u53d8\u91cf\u540d\u66ff\u6362\u6210\u503c\n}\n{%endfor%}\n---------------------------------------------------------------------------------------\n[root@ansible-<span class=\"hljs-number\">90<\/span> ~]<span class=\"hljs-comment\"># cat \/app\/for2.conf <\/span>\nserver {\n        listen <span class=\"hljs-number\">81<\/span>;\n}\nserver {\n        listen <span class=\"hljs-number\">82<\/span>;\n}\nserver {\n        listen <span class=\"hljs-number\">83<\/span>;\n}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<li>20<\/li>\n<li>21<\/li>\n<li>22<\/li>\n<li>23<\/li>\n<li>24<\/li>\n<li>25<\/li>\n<li>26<\/li>\n<li>27<\/li>\n<li>28<\/li>\n<li>29<\/li>\n<li>30<\/li>\n<li>31<\/li>\n<\/ul>\n<h2 id=\"\u91cd\u70b9\u7b2c\u4e09\u79cdfor\u5199\u6cd5\"><a name=\"t53\"><\/a>\u91cd\u70b9\u7b2c\u4e09\u79cdfor\u5199\u6cd5<\/h2>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim for3.yml                 <\/span>\n---\n- hosts: cen7\n  remote_user: root\n  vars:\n    hhh:   \u4e00\u4e2a\u5217\u8868\u4e2d\u53ef\u4ee5\u6709\u5bf9\u4e2a\u5143\u7d20\n      - web1:\n        port: <span class=\"hljs-number\">88<\/span>\n        name: web1.server\n        root: \/app\/web1.server\n      - web2:\n        port: <span class=\"hljs-number\">89<\/span>\n        name: web2.server\n        root: \/app\/web2.server\n      - web3:\n        port: <span class=\"hljs-number\">90<\/span>\n        name: web3.server\n        root: \/app\/web3.server\n\n  tasks:\n    - name: test <span class=\"hljs-keyword\">for<\/span>3\n      template: src=<span class=\"hljs-keyword\">for<\/span>3.conf.j2  dest=\/app\/<span class=\"hljs-keyword\">for<\/span>3.con\n\u6a21\u677f\u6587\u4ef6\n{%<span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> hhh%}\nserver {\n        listen {{i.prot}}\n        servername {{i.name}}\n        rootdir {{i.root}}\n}\n{%endfor%}\n\u751f\u6210\u7ed3\u679c\n[root@ansible-<span class=\"hljs-number\">90<\/span> ~]<span class=\"hljs-comment\"># cat \/app\/for3.conf <\/span>\nserver {\n        listen <span class=\"hljs-number\">88<\/span>\n        servername web1.server\n        rootdir \/app\/web1.server\n}\nserver {\n        listen <span class=\"hljs-number\">89<\/span>\n        servername web2.server\n        rootdir \/app\/web2.server\n}\nserver {\n        listen <span class=\"hljs-number\">90<\/span>\n        servername web3.server\n        rootdir \/app\/web3.server\n}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<li>20<\/li>\n<li>21<\/li>\n<li>22<\/li>\n<li>23<\/li>\n<li>24<\/li>\n<li>25<\/li>\n<li>26<\/li>\n<li>27<\/li>\n<li>28<\/li>\n<li>29<\/li>\n<li>30<\/li>\n<li>31<\/li>\n<li>32<\/li>\n<li>33<\/li>\n<li>34<\/li>\n<li>35<\/li>\n<li>36<\/li>\n<li>37<\/li>\n<li>38<\/li>\n<li>39<\/li>\n<li>40<\/li>\n<li>41<\/li>\n<li>42<\/li>\n<li>43<\/li>\n<li>44<\/li>\n<li>45<\/li>\n<li>46<\/li>\n<li>47<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">{% <span class=\"hljs-keyword\">for<\/span> vhost <span class=\"hljs-keyword\">in<\/span> nginx_vhosts %}\nserver {\nlisten {{ vhost.listen | default(<span class=\"hljs-string\">'80 default_server'<\/span>) }};\n\n{% <span class=\"hljs-keyword\">if<\/span> vhost.server_name is defined %}   \/\/\u8fd9\u8868\u793a\u5982\u679cvhost.server_name\u88ab\u5b9a\u4e49\u4e86\u6709\u503c\u4e86\u5219\u5c31\u7528\u8fd9\u91cc\u5b9a\u4e49\u7684\u503c\uff0c\u5982\u679c\u6ca1\u6709\u5b9a\u4e49\u5c31\u4e0d\u7528\nserver_name {{ vhost.server_name }};\n{% endif %}\n\n{% <span class=\"hljs-keyword\">if<\/span> vhost.root is defined %}\nroot {{ vhost.root }};\n{% endif %}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<\/ul>\n<p>-\u8303\u4f8b<\/p>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim for4-if.yml <\/span>\n---\n- hosts: cen7\n  remote_user: root\n  vars:\n    hhh:\n      - web1:\n\u25bd       port: <span class=\"hljs-number\">88<\/span>\n        <span class=\"hljs-comment\">#name: web1.server   \u6211\u4eec\u628aweb1\u4e2dname\u6ce8\u91ca<\/span>\n        root: \/app\/web1.server\n      - web2:\n        port: <span class=\"hljs-number\">89<\/span>\n        name: web2.server\n        root: \/app\/web2.server\n      - web3:\n        port: <span class=\"hljs-number\">90<\/span>\n        <span class=\"hljs-comment\">#name: web3.server  web#\u4e2dname\u6ce8\u91ca   \u8fd9\u6837\u7528if\u65f6\u5c31\u4e0d\u4f1a\u5b9a\u4e49\u8fd9\u4e2aname<\/span>\n        root: \/app\/web3.server\n\n  tasks:\n    - name: test <span class=\"hljs-keyword\">for<\/span>3\n      template: src=<span class=\"hljs-keyword\">for<\/span>4-if.conf.j2  dest=\/app\/<span class=\"hljs-keyword\">for<\/span>4-if.conf\n\u6a21\u677f\u6587\u4ef6\n[root@ansible ansible]<span class=\"hljs-comment\"># vim templates\/for4-if.conf.j2<\/span>\n{%<span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> hhh %}\nservice {\n        listen {{i.port}}\n{%<span class=\"hljs-keyword\">if<\/span> i.name is defined %}            \u4e00\u70b9\u8981\u9876\u5934\u5199\u8fd9\u4e9b\uff0c\u5f53i.name\u88ab\u5b9a\u4e49\u5c31\u8c03\u7528\uff0c\u5982\u679c\u6ca1\u6709\u88ab\u5b9a\u4e49\u5219\u4e0d\u8c03\u7528\n        servername {{i.name}}\n{%endif%}                            <span class=\"hljs-keyword\">if<\/span>\u8fd9\u79cd\u683c\u5f0f\u4e00\u70b9\u8981\u9876\u5934\u5199\n        rootdir {{i.root}}\n}\n{%endfor%}\n\u7ed3\u679c\n[root@ansible-<span class=\"hljs-number\">90<\/span> ~]<span class=\"hljs-comment\"># cat \/app\/for4-if.conf <\/span>\nservice {\n        listen <span class=\"hljs-number\">88<\/span>\n        rootdir \/app\/web1.server\n}\nservice {\n        listen <span class=\"hljs-number\">89<\/span>\n        servername web2.server     \u8fd9\u91cc\u53ea\u6709web2\u7684name\u6709\u540d\u5b57\n        rootdir \/app\/web2.server\n}\nservice {\n        listen <span class=\"hljs-number\">90<\/span>\n        rootdir \/app\/web3.server\n}\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<li>20<\/li>\n<li>21<\/li>\n<li>22<\/li>\n<li>23<\/li>\n<li>24<\/li>\n<li>25<\/li>\n<li>26<\/li>\n<li>27<\/li>\n<li>28<\/li>\n<li>29<\/li>\n<li>30<\/li>\n<li>31<\/li>\n<li>32<\/li>\n<li>33<\/li>\n<li>34<\/li>\n<li>35<\/li>\n<li>36<\/li>\n<li>37<\/li>\n<li>38<\/li>\n<li>39<\/li>\n<li>40<\/li>\n<li>41<\/li>\n<li>42<\/li>\n<li>43<\/li>\n<li>44<\/li>\n<li>45<\/li>\n<li>46<\/li>\n<li>47<\/li>\n<li>48<\/li>\n<li>49<\/li>\n<\/ul>\n<ul>\n<li>\u793a\u52171<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\/\/ temnginx.yml\n---\n- hosts: testweb\n  remote_user: root\n  vars:\n    nginx_vhosts:\n        - listen: <span class=\"hljs-number\">8080<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\/\/templates\/nginx.conf.j2\n{% <span class=\"hljs-keyword\">for<\/span> vhost <span class=\"hljs-keyword\">in<\/span> nginx_vhosts %}\nserver {\nlisten {{ vhost.listen }}\n}\n{% endfor %}\n\n\u751f\u6210\u7684\u7ed3\u679c\nserver {\nlisten <span class=\"hljs-number\">8080<\/span>\n}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<\/ul>\n<ul>\n<li>\u793a\u4f8b2<\/li>\n<\/ul>\n<h2 id=\"temnginxyml\"><a name=\"t54\"><\/a>\/\/ temnginx.yml<\/h2>\n<ul>\n<li>hosts: mageduweb<br \/>\nremote_user: root<br \/>\nvars:<br \/>\nnginx_vhosts:<\/p>\n<ul>\n<li>web1<\/li>\n<li>web2<\/li>\n<li>web3<br \/>\ntasks:<\/li>\n<li>name: template config<br \/>\ntemplate: src=nginx.conf.j2 dest=\/etc\/nginx\/nginx.conf<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"hljs mathematica has-numbering\">```bash\n \/\/ templates\/nginx.conf.j2\n<span class=\"hljs-list\">{% for vhost in nginx_vhosts %}<\/span>\nserver <span class=\"hljs-list\">{\nlisten {{ vhost }<\/span>}\n}\n<span class=\"hljs-list\">{% endfor %}<\/span>\n\n\n\n\u751f\u6210\u7684\u7ed3\u679c\uff1a\nserver <span class=\"hljs-list\">{\nlisten web1\n}<\/span>\nserver <span class=\"hljs-list\">{\nlisten web2\n}<\/span>\nserver <span class=\"hljs-list\">{\nlisten web3\n}<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<li>19<\/li>\n<li>20<\/li>\n<\/ul>\n<p><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180115\/1JJG72ekK9.png\" alt=\"mark\" \/><br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180115\/9jFBF27GJB.png\" alt=\"mark\" \/><\/p>\n<h2 id=\"roles\"><a name=\"t55\"><\/a>roles<\/h2>\n<ul>\n<li>roles\n<ul>\n<li>ansilbe\u81ea1.2\u7248\u672c\u5f15\u5165\u7684\u65b0\u7279\u6027\uff0c\u7528\u4e8e\u5c42\u6b21\u6027\u3001\u7ed3\u6784\u5316\u5730\u7ec4\u7ec7playbook\u3002roles\u80fd\u591f\u6839\u636e\u5c42\u6b21\u578b\u7ed3\u6784\u81ea\u52a8\u88c5\u8f7d\u53d8\u91cf\u6587\u4ef6\u3001tasks\u4ee5\u53cahandlers\u7b49\u3002\u8981\u4f7f\u7528roles\u53ea\u9700\u8981\u5728playbook\u4e2d\u4f7f\u7528include\u6307\u4ee4\u5373\u53ef\u3002\u7b80\u5355\u6765\u8bb2\uff0croles\u5c31\u662f\u901a\u8fc7\u5206\u522b\u5c06\u53d8\u91cf\u3001\u6587\u4ef6\u3001\u4efb\u52a1\u3001\u6a21\u677f\u53ca\u5904\u7406\u5668\u653e\u7f6e\u4e8e\u5355\u72ec\u7684\u76ee\u5f55\u4e2d\uff0c\u5e76\u53ef\u4ee5\u4fbf\u6377\u5730include\u5b83\u4eec\u7684\u4e00\u79cd\u673a\u5236\u3002\u89d2\u8272\u4e00\u822c\u7528\u4e8e\u57fa\u4e8e\u4e3b\u673a\u6784\u5efa\u670d\u52a1\u7684\u573a\u666f\u4e2d\uff0c\u4f46\u4e5f\u53ef\u4ee5\u662f\u7528\u4e8e\u6784\u5efa\u5b88\u62a4\u8fdb\u7a0b\u7b49\u573a\u666f\u4e2d<\/li>\n<\/ul>\n<\/li>\n<li>\u590d\u6742\u573a\u666f\uff1a\u5efa\u8bae\u4f7f\u7528roles\uff0c\u4ee3\u7801\u590d\u7528\u5ea6\u9ad8\n<ul>\n<li>\u53d8\u66f4\u6307\u5b9a\u4e3b\u673a\u6216\u4e3b\u673a\u7ec4<\/li>\n<li>\u5982\u547d\u540d\u4e0d\u89c4\u8303\u7ef4\u62a4\u548c\u4f20\u627f\u6210\u672c\u5927<\/li>\n<li>\u67d0\u4e9b\u529f\u80fd\u9700\u591a\u4e2aPlaybook\uff0c\u901a\u8fc7Includes\u5373\u53ef\u5b9e\u73b0<\/li>\n<\/ul>\n<\/li>\n<li>\u89d2\u8272(roles)\uff1a\u89d2\u8272\u96c6\u5408<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">roles\/    \u8fd9\u4e2a\u6587\u4ef6\u5939\u540d\u6bd4\u8f83\u5c0f\u662froles\u662f\u56fa\u5b9a\u7684\n  mysql\/\n  httpd\/\n  nginx\/\n  memcached\/<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<\/ul>\n<h3 id=\"ansible-roles\u76ee\u5f55\u7f16\u6392\"><a name=\"t56\"><\/a>Ansible Roles\u76ee\u5f55\u7f16\u6392<\/h3>\n<p><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180115\/Fc4b1b21CF.png\" alt=\"mark\" \/><br \/>\n&#8211; git\u662f\u4e00\u4e2a\u89d2\u8272<br \/>\n+ \u4e0b\u9762\u6709\u4e00\u4e2atasks\u76ee\u5f55\uff0c\u91cc\u9762\u653e\u7740\u6240\u6709\u7684\u4efb\u52a1 \u8fd9\u4eba\u6587\u4ef6\u5fc5\u987b\u8981\u6709<br \/>\n+ \u800ctasks\u6587\u4ef6\u5939\u91cc\u5fc5\u8981\u8981\u6709\u4e2a\u6587\u4ef6\u662fmain.yml\u8fd9\u4e2aplaybook\u3002\u5f53\u6211\u4eec\u8c03\u7528git\u8fd9\u4e2a\u670d\u52a1\u89d2\u8272\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u53bb\u627etasks\u4e0b\u7684main.yml\u8fd9\u662f\u6574\u4e2a\u4efb\u52a1\u7684\u5165\u53e3\uff0c\u76f8\u5f53\u4e8e\u4e3b\u8054\u7cfb\u4eba\u3002\u8fd9\u4e2a\u6587\u4ef6\u5fc5\u987b\u5b58\u5728<br \/>\n+ file\u5219\u662f\u653e\u7740\u8981\u7528\u7684\u6587\u4ef6<br \/>\n+ vars\u653e\u7740\u6240\u6709\u7684\u53d8\u91cf<br \/>\n&#8211; \u5f53roles\u5199\u597d\u540e\u8981\u6709\u4eba\u8c03\u7528\uff0c\u5219userconf.yml\u5c31\u76f8\u5f53\u4e8e\u8c03\u7528\u8fd9\u4e9b\u670d\u52a1<\/p>\n<h3 id=\"roles\u76ee\u5f55\u7ed3\u6784\"><a name=\"t57\"><\/a>roles\u76ee\u5f55\u7ed3\u6784<\/h3>\n<ul>\n<li>\u6bcf\u4e2a\u89d2\u8272\uff0c\u4ee5\u7279\u5b9a\u7684\u5c42\u7ea7\u76ee\u5f55\u7ed3\u6784\u8fdb\u884c\u7ec4\u7ec7<\/li>\n<li>roles\u76ee\u5f55\u7ed3\u6784<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">playbook.yml   \u8fd9\u662f\u8c03\u7528\u7684\u5267\u672c\u662f\u548croles\u662f\u5728\u540c\u4e00\u76ee\u5f55\u662f\u5e73\u7ea7\u7684\nroles\/\n project\/\n  tasks\/\n  files\/\n  vars\/ \u4e0d\u5e38\u7528\n  default\/ \u4e0d\u5e38\u7528\n  templates\/\n  handlers\/\n  meta\/ \u4e0d\u5e38\u7528\n- roles\u8fd9\u4e2a\u6587\u4ef6\u5939\u5728\u54ea\u91cc\u521b\u5efa\u90fd\u53ef\u4ee5\uff0c\u4f46\u662f\u5b98\u65b9\u6709\u4e00\u4e2a\u63a8\u8350\u4f4d\u7f6e\/etc\/ansible\/\u76ee\u5f55\u4e0b\u6709\u4e00\u4e2aroles\n- \u800croles\u76ee\u5f55\u521b\u5efa\u5b8c\u91cc\u9762\u7684\u76ee\u5f55\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u6765\u521b\u5efa\uff0c\u4f46\u662ftasks\u5fc5\u987b\u8981\u6709<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<\/ul>\n<ul>&#8211;<\/ul>\n<h3 id=\"roles\u5404\u76ee\u5f55\u4f5c\u7528\"><a name=\"t58\"><\/a>Roles\u5404\u76ee\u5f55\u4f5c\u7528<\/h3>\n<ul>\n<li>\/roles\/project\/ :\u9879\u76ee\u540d\u79f0,\u6709\u4ee5\u4e0b\u5b50\u76ee\u5f55\n<ul>\n<li>files\/ \uff1a\u5b58\u653e\u7531copy\u6216script\u6a21\u5757\u7b49\u8c03\u7528\u7684\u6587\u4ef6<\/li>\n<li>templates\/\uff1atemplate\u6a21\u5757\u67e5\u627e\u6240\u9700\u8981\u6a21\u677f\u6587\u4ef6\u7684\u76ee\u5f55<\/li>\n<li>tasks\/\uff1a\u5b9a\u4e49task,role\u7684\u57fa\u672c\u5143\u7d20\uff0c\u81f3\u5c11\u5e94\u8be5\u5305\u542b\u4e00\u4e2a\u540d\u4e3amain.yml\u7684\u6587\u4ef6\uff1b\u5176\u5b83\u7684\u6587\u4ef6\u9700\u8981\u5728\u6b64\u6587\u4ef6\u4e2d\u901a\u8fc7include\u8fdb\u884c\u5305\u542b<\/li>\n<li>handlers\/\uff1a\u81f3\u5c11\u5e94\u8be5\u5305\u542b\u4e00\u4e2a\u540d\u4e3amain.yml\u7684\u6587\u4ef6\uff1b\u5176\u5b83\u7684\u6587\u4ef6\u9700\u8981\u5728\u6b64\u6587\u4ef6\u4e2d\u901a\u8fc7include\u8fdb\u884c\u5305\u542b<\/li>\n<li>vars\/\uff1a\u5b9a\u4e49\u53d8\u91cf\uff0c\u81f3\u5c11\u5e94\u8be5\u5305\u542b\u4e00\u4e2a\u540d\u4e3amain.yml\u7684\u6587\u4ef6\uff1b\u5176\u5b83\u7684\u6587\u4ef6\u9700\u8981\u5728\u6b64\u6587\u4ef6\u4e2d\u901a\u8fc7include\u8fdb\u884c\u5305\u542b<\/li>\n<li>meta\/\uff1a\u5b9a\u4e49\u5f53\u524d\u89d2\u8272\u7684\u7279\u6b8a\u8bbe\u5b9a\u53ca\u5176\u4f9d\u8d56\u5173\u7cfb,\u81f3\u5c11\u5e94\u8be5\u5305\u542b\u4e00\u4e2a\u540d\u4e3amain.yml\u7684\u6587\u4ef6\uff0c\u5176\u5b83\u6587\u4ef6\u9700\u5728\u6b64\u6587\u4ef6\u4e2d\u901a\u8fc7include\u8fdb\u884c\u5305\u542b<\/li>\n<li>default\/\uff1a\u8bbe\u5b9a\u9ed8\u8ba4\u53d8\u91cf\u65f6\u4f7f\u7528\u6b64\u76ee\u5f55\u4e2d\u7684main.yml\u6587\u4ef6<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3 id=\"\u521b\u5efarole\"><a name=\"t59\"><\/a>\u521b\u5efarole<\/h3>\n<ul>\n<li>\u521b\u5efarole\u7684\u6b65\u9aa4\n<ol>\n<li>\u521b\u5efa\u4ee5roles\u547d\u540d\u7684\u76ee\u5f55<\/li>\n<li>\u5728roles\u76ee\u5f55\u4e2d\u5206\u522b\u521b\u5efa\u4ee5\u5404\u89d2\u8272\u540d\u79f0\u547d\u540d\u7684\u76ee\u5f55\uff0c\u5982webservers\u7b49<\/li>\n<li>\u5728\u6bcf\u4e2a\u89d2\u8272\u547d\u540d\u7684\u76ee\u5f55\u4e2d\u5206\u522b\u521b\u5efafiles\u3001handlers\u3001meta\u3001tasks\u3001templates\u548cvars\u76ee\u5f55\uff1b\u7528\u4e0d\u5230\u7684\u76ee\u5f55\u53ef\u4ee5\u521b\u5efa\u4e3a\u7a7a\u76ee\u5f55\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u521b\u5efa<\/li>\n<li>\u5728playbook\u6587\u4ef6\u4e2d\uff0c\u8c03\u7528\u5404\u89d2\u8272<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h2 id=\"\u9488\u5bf9\u5927\u578b\u9879\u76ee\u4f7f\u7528roles\u8fdb\u884c\u7f16\u6392\"><a name=\"t60\"><\/a>\u9488\u5bf9\u5927\u578b\u9879\u76ee\u4f7f\u7528Roles\u8fdb\u884c\u7f16\u6392<\/h2>\n<p><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180115\/iAGCdjg2mh.png\" alt=\"mark\" \/><br \/>\n&#8211; \u8303\u4f8b<\/p>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">site.yml\nwebservers.yml\ndbservers.yml\nroles\/\n    common\/\n        files\/\n        templates\/\n        tasks\/\n        handlers\/\n        vars\/\n        meta\/\n    webservers\/\n        files\/\n        templates\/\n        tasks\/\n        handlers\/\n        vars\/\n        meta\/<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<li>16<\/li>\n<li>17<\/li>\n<li>18<\/li>\n<\/ul>\n<h3 id=\"playbook\u8c03\u7528\u89d2\u8272\"><a name=\"t61\"><\/a>playbook\u8c03\u7528\u89d2\u8272<\/h3>\n<ul>\n<li>\u8c03\u7528\u89d2\u8272\u65b9\u6cd51<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">- hosts: websrvs\n  remote_user: root\n  roles:\n    - mysql   \u8fd9\u662f\u8c03\u7528roles\uff0c\u5c31\u662froles\u76ee\u5f55\u4e2d\u6240\u521b\u5efa\u7684\n    - memcached\n    - nginx<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<\/ul>\n<ul>\n<li>\u8c03\u7528\u89d2\u8272\u65b9\u6cd52<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\u4f20\u9012\u53d8\u91cf\u7ed9\u89d2\u8272\n- hosts:\n  remote_user:\n  roles:\n    - mysql\n    - { role: nginx, username: nginx }\n    \u952erole\u7528\u4e8e\u6307\u5b9a\u89d2\u8272\u540d\u79f0\n    \u540e\u7eed\u7684k\/v\u7528\u4e8e\u4f20\u9012\u53d8\u91cf\u7ed9\u89d2\u8272<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<\/ul>\n<ul>\n<li>\u8c03\u7528\u89d2\u8272\u65b9\u6cd53\uff1a\u8fd8\u53ef\u57fa\u4e8e\u6761\u4ef6\u6d4b\u8bd5\u5b9e\u73b0\u89d2\u8272\u8c03\u7528<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">roles:\n- { role: nginx, username: nginx, when:ansible_distribution_major_version== <span class=\"hljs-string\">'7'<\/span> }<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<h3 id=\"\u5b8c\u6574\u7684roles\u67b6\u6784\u5b8c\u6574\u7684roles\u67b6\u6784\"><a name=\"t62\"><\/a>\u5b8c\u6574\u7684roles\u67b6\u6784\u5b8c\u6574\u7684roles\u67b6\u6784<\/h3>\n<p><img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180115\/jB552KEF53.png\" alt=\"mark\" \/><\/p>\n<h3 id=\"roles-playbook-tags\u4f7f\u7528\"><a name=\"t63\"><\/a>roles playbook tags\u4f7f\u7528<\/h3>\n<ul>\n<li>roles playbook tags\u4f7f\u7528<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\"><span class=\"hljs-comment\"># ansible-playbook --tags=\"nginx,httpd,mysql\" nginx-role.yml<\/span>\n\/\/ nginx-role.yml\n---\n- hosts: testweb\n  remote_user: root\n  roles:\n    - { role: nginx ,tags: [ <span class=\"hljs-string\">'nginx'<\/span>, <span class=\"hljs-string\">'web'<\/span> ] ,when:ansible_distribution_major_version == <span class=\"hljs-string\">\"6\u201c }\n    - { role: httpd ,tags: [ 'httpd', 'web' ] }\n    - { role: mysql ,tags: [ 'mysql', 'db' ] }\n    - { role: marridb ,tags: [ 'mysql', 'db' ] }\n    - { role: php }<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<\/ul>\n<h3 id=\"\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684roles\"><a name=\"t64\"><\/a>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684roles<\/h3>\n<ol>\n<li>\u9996\u5148\u521b\u5efa\u4e00\u4e2aroles\u76ee\u5f55<\/li>\n<li>\u5728roles\u76ee\u5f55\u4e2d\u521b\u5efa\u670d\u52a1\u76ee\u5f55\uff0c\u8fd9\u91cc\u521b\u5efa\u4e00\u4e2aNGINX\u670d\u52a1\u7684\u5b89\u88c5\u548c\u542f\u52a8\uff0c\u548c\u590d\u5236\u6587\u4ef6\u7684roles<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180117\/iDF0iLc65j.png\" alt=\"mark\" \/><\/li>\n<li>\u90a3\u5c31\u90a3nginx\u670d\u52a1\u6765\u793a\u5217\uff0c\u8fdb\u53bb\u5165\u5230nginx\u76ee\u5f55\u4e2d\uff0c\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u521b\u5efa\u76ee\u5f55\uff0ctasks\u76ee\u5f55\u5fc5\u987b\u521b\u5efa\u8fd9\u91cc\u5b58\u653eplaybook\uff0c\u5176\u5b83\u5982files\u76ee\u5f55\u5982\u679c\u4e0d\u6d89\u53ca\u6587\u4ef6\u7684\u4f20\u8f93\u53ef\u4ee5\u4e0d\u521b\u5efa<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180117\/GjIG6D7lC8.png\" alt=\"mark\" \/><\/li>\n<li>\u8fdb\u5165tasks\u76ee\u5f55\u521b\u5efa\u6dfb\u52a0\u7ec4\uff0c\u6dfb\u52a0\u7528\u6237\uff0c\u5b89\u88c5\u670d\u52a1\uff0c\u590d\u5236\u6a21\u672c\uff0c\u542f\u52a8\u670d\u52a1<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible tasks]<span class=\"hljs-comment\"># ls<\/span>\ngroupadd.yml  install.yml   start.yml  useradd.yml <\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<ol>\n<li>\u5f53\u7136\u8fd9\u91cc\u5199\u7684playbook\u548c\u6211\u4eec\u4e4b\u524d\u5199\u7684\u5355\u4e2aplaybook\u662f\u6709\u533a\u522b\u7684,\u8fd9\u91cc\u66f4\u7b80\u5355\u4e86 \uff0c\u4e0d\u7528\u5199hosts\u548cremote_user\u548ctasks ,\u53ea\u7528\u5199\u5bf9\u5e94\u7684\u4efb\u52a1<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">\n[root@ansible tasks]<span class=\"hljs-comment\"># cat groupadd.yml <\/span>\n- name: add group\n  group: name=nginx\n\n[root@ansible tasks]<span class=\"hljs-comment\"># cat useradd.yml <\/span>\n- name: create user\n  user: name=nginx group=nginx  system=yes shell=\/sbin\/nologin\n[root@ansible tasks]<span class=\"hljs-comment\"># cat install.yml <\/span>\n- name: install package\n  yum: name=nginx\n\n[root@ansible tasks]<span class=\"hljs-comment\"># cat start.yml <\/span>\n- name: start service\n  service: name=nginx state=started enabled=yes<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<li>12<\/li>\n<li>13<\/li>\n<li>14<\/li>\n<li>15<\/li>\n<\/ul>\n<ol>\n<li>\u5f53\u7136\u6211\u4eec\u77e5\u9053\u6bcf\u4e2a\u76ee\u5f55\u4e2d\u90fd\u6709\u4e00\u4e2amain.yml\u8fd9\u4e2a\u6587\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u76f8\u5f53\u4e8e\u8fd9\u4e9b\u4efb\u52a1\u7684\u603b\u5165\u53e3\uff0c\u5f53\u6211\u4eec\u4ece\u5916\u9762\u8c03\u7528\u8fd9\u4e9b\u6a21\u5757\u5316\u7684\u670d\u52a1\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u5230tasks\u76ee\u5f55\u4e2d\u627emain.yml\uff0c\u6240\u6709\u6211\u4eec\u5728\u521b\u5efa\u4e00\u4e2amain.yml<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180117\/Cfje2DBBkL.png\" alt=\"mark\" \/><\/p>\n<ol>\n<li>\u7f16\u5199main.yml\u6587\u4ef6,\u8fd9\u91cc\u662f\u6709\u5148\u540e\u987a\u5e8f\u7684\u4e00\u5b9a\u8981\u6ce8\u610f\uff0c\u81ea\u4e0a\u800c\u4e0b\u7684\u6267\u884c\u3002\u7528- import_tasks: playbookname<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible tasks]<span class=\"hljs-comment\"># cat main.yml <\/span>\n- import_tasks: groupadd.yml\n- import_tasks: useradd.yml\n- import_tasks: install.yml\n- import_tasks: start.yml<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<\/ul>\n<ol>\n<li>\u8fd9\u6837\u5c31\u521b\u5efa\u597d\u4e86\uff0c\u73b0\u5728\u56de\u5230\u548croles\u540c\u7ea7\u7684\u76ee\u5f55\uff0c\u521b\u5efa\u4e00\u4e2a\u8c03\u7528\u8fd9\u4e2a\u89d2\u8272\u7b26playbook\uff0c\u4e00\u5b9a\u8981\u548croles\u8fd9\u76ee\u5f55\u5728\u540c\u7ea7\u76ee\u5f55\u4e2d<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># ls<\/span>\n<span class=\"hljs-number\">1<\/span>  nginx-roles.yml  roles\u8fd9\u4e2a\u662f\u76ee\u5f55<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<ol>\n<li>nginx-roles.yml\u7684\u7f16\u5199<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># cat nginx-roles.yml <\/span>\n- hosts: cen7\n  remote_user: root\n\n  roles:\n    - role: nginx   \u8fd9\u662f\u8c03\u7528\u89d2\u8272<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<\/ul>\n<ol>\n<li>\u8fd9\u6837\u5c31\u662f\u4e00\u4e2a\u7b80\u5355roles\n<ul>\n<li>\u5f53\u7136\u5982\u679c\u6709\u591a\u4e2a\u89d2\u8272\u4e5f\u53ef\u4ee5\u8c03\u7528<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># cat nginx-roles.yml <\/span>\n- hosts: cen7\n  remote_user: root\n\n  roles:\n    - role: nginx   \u8fd9\u662f\u8c03\u7528\u89d2\u8272\n    - role: filecopy  \u4e5f\u53ef\u4ee5\u8c03\u7528\u590d\u5236\u6587\u4ef6\u7684\u89d2\u8272<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<\/ul>\n<ul>\n<li>\u5f53\u4e5f\u53ef\u4ee5\u5355\u72ec\u8c03\u7528\u67d0\u4e00\u4e2a\u89d2\u8272\u4e2d\u5728tasks\u76ee\u5f55\u4e0b\u67d0\u4e2a\u5267\u672c\uff0c\u6bd4\u5982\u5728filecopy\u89d2\u8272\u4e2d\u8c03\u7528\u5b89\u88c5nginx\u670d\u52a1\u5f53\u7136\uff0c\u9996\u5148\u6211\u4eec\u628a\u521a\u521a\u5b89\u88c5\u7684nginx\u670d\u52a1\u5378\u8f7d\u4e86\u3002\n<ul>\n<li>\u9996\u5148\u8981filecopy\u76ee\u5f55\u4e0b\u4e2d\u7684tasks\u76ee\u5f55\u4e2d\u7684main.yml\u5267\u672c\u4e2d\u6dfb\u52a0\u4e00\u884c<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">- name: file copy\n  copy: src=fstab dest=\/app\/\n\n- name: file create\n  file: name=\/app\/testfile state=touch mode=<span class=\"hljs-number\">600<\/span>\n\n- import_tasks: roles\/nginx\/tasks\/install.yml      \/\/\u8fd9\u662f\u8c03\u7528\u53e6\u4e00\u4e2a\u89d2\u8272\u4e2d\u7684\u4e00\u4e2a\u5267\u672c ,\u4e00\u822c\u4e0d\u63a8\u8350\u8fd9\u6837\u4f7f\u7528               <\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<\/ul>\n<h3 id=\"\u89d2\u8272\u4e2d\u52a0\u6807\u7b7etags\"><a name=\"t65\"><\/a>\u89d2\u8272\u4e2d\u52a0\u6807\u7b7etags<\/h3>\n<ul>\n<li>\u5728\u521b\u5efa\u4e00\u4e2a\u89d2\u8272\u7528\u4e8e\u5b89\u88c5\u548c\u521b\u5efahttpd\u670d\u52a1\u7684<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible roles]<span class=\"hljs-comment\"># ls<\/span>\nfilecopy  httpd  memcached  nginx<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<ul>\n<li>\u8fd9\u91cc\u67094\u4e2a\u670d\u52a1\u89d2\u8272\uff0c\u5728roles\u7684\u7236\u76ee\u5f55\u4e2d\u5199\u4e00\u4e2a\u8c03\u7528\u89d2\u8272\u7684\u811a\u672c\u53ebtags-roles.yml<\/li>\n<li>\u7528\u5927\u62ec\u53f7\u5f15\u8d77\u6765<\/li>\n<\/ul>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ansible]<span class=\"hljs-comment\"># vim tags-roles.yml<\/span>\n---\n- hosts: cen7\n  remote_user: root\n\n  roles:\n    - { role: nginx, tags: [<span class=\"hljs-string\">'nginx'<\/span>,<span class=\"hljs-string\">'web'<\/span> ] }  \u4e00\u4e2a\u670d\u52a1\u53ef\u4ee5\u6709\u591a\u4e2a\u6807\u7b7e\uff0c\u4e5f\u53ef\u4ee5\u548c\u5176\u5b83\u6807\u7b7e\u540d\u76f8\u540ctags\uff1a\u540e\u4e00\u5b9a\u8981\u6709\u7a7a\u683c\uff0c\u6807\u7b7e\u7528\u5355\u5f15\u53f7\uff0c\u591a\u4e2a\u6807\u7b7e\u7528\u4e2d\u62ec\u53f7\u5206\u9694\n    - { role: httpd, tags: [<span class=\"hljs-string\">'httpd'<\/span>,<span class=\"hljs-string\">'web'<\/span> ] }\n    - { role: filecopy, tags: <span class=\"hljs-string\">'copyfile'<\/span> }<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<\/ul>\n<ul>\n<li>\u7136\u540e\u5c31\u53ef\u4ee5\u6839\u636e\u6807\u7b7e\u6765\u6267\u884c\uff0c\u8c03\u7528\u65f6\u548c\u4efb\u52a1\u6807\u7b7e\u8bed\u6cd5\u4e00\u6837\u3002<\/li>\n<\/ul>\n<h3 id=\"\u521b\u5efa\u4e00\u4e2a\u5927\u578b\u7684\u89d2\u8272\u793a\u5217\"><a name=\"t66\"><\/a>\u521b\u5efa\u4e00\u4e2a\u5927\u578b\u7684\u89d2\u8272\u793a\u5217<\/h3>\n<ol>\n<li>\u521b\u5efaroles\u76ee\u5f55\u548c\u76ee\u5f55\u91cc\u7684nginx\u89d2\u8272<\/li>\n<li>\u5728nginx\u4e2d\u521b\u5efa\u8981\u7528\u7684\u76ee\u5f55<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bahs hljs mathematica has-numbering\">mkdir <span class=\"hljs-list\">{tasks,templates,handlers,vars}<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<\/ul>\n<ol>\n<li>\u60f3tasks\u76ee\u5f55\u4e2d\u521b\u5efamain.yml\u5267\u672c<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># cat \/root\/ansible\/roles\/nginx\/tasks\/main.yml <\/span>\n- name: install package    \u5b89\u88c5nginx\u5305\n  yum: name=nginx\n\n- name: template\n  template: src=nginx.conf.j2 dest=\/etc\/nginx\/nginx.conf\n  tags\uff1a <span class=\"hljs-number\">123<\/span>  \u8fd9\u91cc\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u6807\u7b7e\n  notify: restart service   \u5f53template\u4efb\u52a1\u53d1\u751f\u53d8\u5316\u65f6\u5219\u6267\u884crestart service\n\n- name: start service\n  service: name=nginx state=started<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<li>10<\/li>\n<li>11<\/li>\n<\/ul>\n<ol>\n<li>\u5728handlers\u76ee\u5f55\u4e2d\u521b\u5efamain.yml\u5e76\u7f16\u8f91<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># cat \/root\/ansible\/roles\/nginx\/handlers\/main.yml <\/span>\n- name: restart service    \u8fd9\u4e2a\u540d\u5b57\u5fc5\u987b\u548cnotify\u6240\u8868\u793a\u7684\u540d\u5b57\u4e00\u6837\uff0c\u5f53template\u4efb\u52a1\u53d1\u751f\u53d8\u5316\u65f6\u6267\u884c\u8fd9\u91cc\u7684\u4efb\u52a1\n  service: name=nginx state=restarted<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<\/ul>\n<ol>\n<li>\u590d\u5236\u4e00\u4efdnginx\u6587\u4ef6\u5230template\u5f53\u6a21\u677f\u6587\u4ef6<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">cp \/etc\/nginx\/nginx.conf templates\/nginx.conf.j2<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<\/ul>\n<ol>\n<li>\u4fee\u6539\u6a21\u677f\u6587\u4ef6<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180117\/BB8dd1iF46.png\" alt=\"mark\" \/><\/p>\n<ul>\n<li>\u8fd9\u6837\u5c31\u4f1a\u4e0d\u540c\u4e3b\u673a\u4f1a\u751f\u6210\u4e0d\u540c\u7684\u914d\u7f6e\u6587\u4ef6<\/li>\n<\/ul>\n<\/li>\n<li>\u5728vars\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2amain.yml\u6587\u4ef6\u6765\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u53d8\u91cf<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># cat \/root\/ansible\/roles\/nginx\/vars\/main.yml <\/span>\nlport: <span class=\"hljs-number\">8080<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<\/ul>\n<ol>\n<li>\u5728\u6a21\u677f\u6587\u4ef6\u4e2d\u8c03\u7528\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6<br \/>\n<img decoding=\"async\" title=\"\" src=\"http:\/\/p15m9hv8j.bkt.clouddn.com\/images\/180117\/cgfdB1CiE1.png\" alt=\"mark\" \/><\/li>\n<li>\u5728roles\u540c\u7ea7\u76ee\u5f55\u4e2d\u5199\u8c03\u7528\u89d2\u8272\u7684playbook\u6587\u4ef6<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-bash hljs  has-numbering\">[root@ansible ~]<span class=\"hljs-comment\"># cat ansible\/testnginx.yml <\/span>\n- hosts: all\n  remote_user: root\n\n  roles:\n    - { role: nginx, when: ansible_distribution_major_version==<span class=\"hljs-string\">'7'<\/span> }  \u5224\u65ad\u5982\u679c\u7cfb\u7edf\u7248\u672c\u53f7\u4e3a<span class=\"hljs-number\">7<\/span>\u5c31\u6267\u884c\u8fd9\u4e2a\u89d2\u8272\n      lport: <span class=\"hljs-number\">9090<\/span>   \u5728\u8fd9\u91cc\u4e5f\u5b9a\u4e49\u4e86\u53d8\u91cf\uff0c\u8fd9\u91cc\u7684\u53d8\u91cf\u4f18\u5148\u7ea7\u6bd4vars\u76ee\u5f55\u4e2d\u7684\u53d8\u91cf\u4f18\u5148\u7ea7\u9ad8\u8fd9\u91cc\u4f1a\u751f\u6548\n    - role: filecopy  \u8fd8\u53ef\u4ee5\u8c03\u7528\u591a\u4e2a\u89d2\u8272\n      when: ansible_nodename==<span class=\"hljs-string\">'ansible-90'<\/span>  \u4e5f\u662f\u5224\u65ad\uff0c\u5f53\u4e3b\u673a\u540d\u4e3aansible-<span class=\"hljs-number\">90<\/span>\u5c31\u6267\u884c\u8fd9\u4e2a\u89d2\u8272<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li>1<\/li>\n<li>2<\/li>\n<li>3<\/li>\n<li>4<\/li>\n<li>5<\/li>\n<li>6<\/li>\n<li>7<\/li>\n<li>8<\/li>\n<li>9<\/li>\n<\/ul>\n<ol>\n<li>\u7528-C\u68c0\u67e5\u7136\u540e\u6267\u884c<\/li>\n<\/ol>\n<h3 id=\"\u63a8\u8350\u8d44\u6599\"><a name=\"t67\"><\/a>\u63a8\u8350\u8d44\u6599<\/h3>\n<ul>\n<li><a href=\"http:\/\/galaxy.ansible.com\/\" target=\"_blank\" rel=\"noopener\">http:\/\/galaxy.ansible.com<\/a><\/li>\n<li><a href=\"https:\/\/galaxy.ansible.com\/explore#\/\" target=\"_blank\" rel=\"noopener\">https:\/\/galaxy.ansible.com\/explore#\/<\/a><\/li>\n<li><a href=\"http:\/\/github.com\/\" target=\"_blank\" rel=\"noopener\">http:\/\/github.com\/<\/a><\/li>\n<li><a href=\"http:\/\/ansible.com.cn\/\" target=\"_blank\" rel=\"noopener\">http:\/\/ansible.com.cn\/<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/ansible\/ansible\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/ansible\/ansible<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/ansible\/ansible-examples\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/ansible\/ansible-examples<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/blog.csdn.net\/itlinuxp\/article\/details\/79561184 \u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177ansible \u8fd0\u7ef4\u81ea\u52a8\u5316\u53d1\u5c55\u5386\u7a0b\u53ca\u6280\u672f\u5e94\u7528 \u4e91\u8ba1\u7b97\u5de5\u7a0b\u5e08\u6838\u5fc3\u804c\u80fd Linux\u8fd0\u7ef4\u5de5\u7a0b\u5e08\u804c\u80fd\u5212\u5206 \u81ea\u52a8\u5316\u52a8\u7ef4\u5e94\u7528\u573a\u666f \u6587\u4ef6\u4f20\u8f93 \u547d\u4ee4\u6267\u884c \u5e94\u7528\u90e8\u7f72 \u914d\u7f6e\u7ba1\u7406 \u4efb\u52a1\u6d41\u7f16\u6392 \u4f01\u4e1a\u5b9e\u9645\u5e94\u7528\u573a\u666f\u5206\u6790 1 Dev\u5f00\u53d1\u73af\u5883 \u4f7f\u7528\u8005\uff1a\u7a0b\u5e8f\u5458 \u529f\u80fd\uff1a\u7a0b\u5e8f\u5458\u5f00\u53d1\u8f6f\u4ef6\uff0c\u6d4b\u8bd5BUG\u7684\u73af\u5883 \u7ba1\u7406\u8005\uff1a\u7a0b\u5e8f\u5458 1 2 3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-776","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=776"}],"version-history":[{"count":0,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/776\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}