{"id":203,"date":"2023-08-28T17:52:43","date_gmt":"2023-08-28T09:52:43","guid":{"rendered":"http:\/\/8.130.131.184\/wordpress\/?p=203"},"modified":"2023-08-28T17:53:12","modified_gmt":"2023-08-28T09:53:12","slug":"ansible-ad-hoc","status":"publish","type":"post","link":"https:\/\/xianyijitan.top\/?p=203","title":{"rendered":"Ansible ad-hoc"},"content":{"rendered":"<h2>\u547d\u4ee4\u884c\u8bed\u6cd5\u683c\u5f0f<\/h2>\n<p><strong>\u5bf9Ansible\u8fdb\u884c\u8fdc\u7a0b\u7ba1\u7406\u7684\u4e24\u4e2a\u65b9\u6cd5<\/strong><\/p>\n<p><strong>ad-hoc\u4e34\u65f6\u547d\u4ee4\uff0c\u5c31\u662f\u5728\u547d\u4ee4\u884c\u4e0a\u6267\u884c\u7ba1\u7406\u547d\u4ee4<\/strong><br \/>\n<strong>playbook\u5267\u672c\uff0c\u628a\u7ba1\u7406\u4efb\u52a1\u7528\u7279\u5b9a\u683c\u5f0f\u5199\u5230\u6587\u4ef6\u4e2d<\/strong><br \/>\n<strong>\u65e0\u8bba\u54ea\u79cd\u65b9\u5f0f\uff0c\u90fd\u662f\u901a\u8fc7\u6a21\u5757\u52a0\u53c2\u6570\u8fdb\u884c\u7ba1\u7406\u3002\u901a\u5e38\u4f7f\u7528ad-hoc\u547d\u4ee4\u884c\u7ba1\u7406\u65b9\u5f0f\uff0c\u6765\u5b9e\u73b0\u4e00\u4e9b\u65e5\u5e38\u4e34\u65f6\u6027\u7684\u6279\u91cf\u7ba1\u7406\u7684\u4efb\u52a1\u3002\u9700\u8981\u5b9e\u73b0\u8bbe\u7f6e\u76f8\u5bf9\u590d\u6742\uff0c\u9700\u8981\u5927\u91cf\u7684\u64cd\u4f5c\u4efb\u52a1\uff0c\u4f7f\u7528playbook\u5267\u672c\u7ba1\u7406\u65b9\u5f0f\u3002<\/strong><\/p>\n<pre><code class=\"language-shell\"># ad-hoc\u547d\u4ee4\u884c\u57fa\u672c\u683c\u5f0f\uff1aansible \u4e3b\u673a\u96c6\u5408 -m \u6a21\u5757\u540d  -a &quot;\u53c2\u6570&quot; <\/code><\/pre>\n<p><strong>\u5176\u4e2d\uff1a<\/strong><\/p>\n<p><strong>\u4e3b\u673a\u96c6\u5408\uff1a\u6307\u5b9a\u7ba1\u7406\u4e3b\u673a\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u662f\u4e3b\u673a\u540d\u3001\u4e3b\u673aIP\u5730\u5740\u6216\u8005\u662fall\u8868\u793a\u6240\u6709\u4e3b\u673a<\/strong><br \/>\n<strong>\u6a21\u5757\u540d\uff1a\u6307\u5b9a\u529f\u80fd\u6a21\u5757\u540d\u79f0<\/strong><br \/>\n<strong>\u53c2\u6570\uff1a\u5e94\u7528\u6a21\u5757\u529f\u80fd\u4e2d\u7684\u54ea\u4e9b\u529f\u80fd\uff0c\u6807\u51c6\u53c2\u6570\u5b9a\u4e49<\/strong><br \/>\n<strong>\u5176\u4ed6\u53c2\u6570\uff1a-k \u4f7f\u7528\u5bc6\u7801\u8fdc\u7a0b\uff0c\u8fdc\u7a0b\u4e3b\u673aSSH\u8fde\u63a5\u5bc6\u7801\u4fe1\u606f<\/strong><br \/>\n<strong>\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre><code class=\"language-shell\">[root@pubserver ~]# cd  ~\/ansible            \n[root@pubserver ansible]# ansible all --list-hosts         #\u67e5\u770b\u6240\u6709\u4e3b\u673a\u5217\u8868\n  hosts (3):\n    server1\n    server2\n    server3\n#\u8c03\u7528ping\u6a21\u5757         \n[root@pubserver ansible]# ansible  all  -m  ping            #\u6d4b\u8bd5\u8fdc\u7a0b\u4e3b\u673a\u662f\u5426\u8fde\u901a<\/code><\/pre>\n<p><strong>\u901a\u8fc7ping\u6a21\u5757\u6d4b\u8bd5\u5230\u8fdc\u7a0b\u4e3b\u673a\u7684\u8fde\u901a\u6027\uff0c\u68c0\u67e5\u662f\u5426\u53ef\u4ee5\u7ba1\u7406\u8fdc\u7a0b\u4e3b\u673a\u3002<\/strong><\/p>\n<p><strong>ping\u7ed3\u679c\u4e3a\u7eff\u8272\u7684SUCCESS\uff0c\u5219\u8868\u793a\u6210\u529f\uff1b\u662f\u7ea2\u8272\u7684UNREACHABLE\uff0c\u5219\u68c0\u67e5\u7f51\u7edc\u662f\u5426\u53ef\u901a\uff0c\u662f\u5426\u5df2\u7ecf\u53ef\u4ee5\u514d\u5bc6\u767b\u9646\u3002<\/strong><\/p>\n<h2>\u5e38\u7528\u6a21\u5757<\/h2>\n<h3>shell\u6a21\u5757<\/h3>\n<p><strong>\u5728\u8fdc\u7a0b\u4e3b\u673a\u6267\u884c\u547d\u4ee4\u65f6\uff0ccommand\u6a21\u5757\u4e3a\u9ed8\u8ba4\u6a21\u5757\u3002<\/strong><\/p>\n<p><strong>Shell\u6a21\u5757\u529f\u80fd\u540ccommand\u529f\u80fd\u7c7b\u4f3c\uff0c\u4e8c\u8005\u7684\u533a\u522b\u5728\u4e8e\uff1a<\/strong><\/p>\n<p><strong>command\u4e0d\u652f\u6301bash\u7684\u7279\u6027\uff0c\u5982\u7ba1\u9053\u548c\u91cd\u5b9a\u5411\u7b49\u529f\u80fd\uff0c\u6240\u6709\u9700\u8981\u8c03\u7528shell\u7684\u529f\u80fd\u90fd\u65e0\u6cd5\u4f7f\u7528<\/strong><\/p>\n<pre><code class=\"language-shell\"># \u67e5\u770b\u8fdb\u7a0b\u6570\u91cf\n[root@localhost ansible]# ansible webservers -m shell -a &quot;ps aux | wc -l&quot;\nserver1 | CHANGED | rc=0 &gt;&gt;\n144\n\nserver2 | CHANGED | rc=0 &gt;&gt;\n144<\/code><\/pre>\n<h3>scrpt\u6a21\u5757<\/h3>\n<p><strong>\u5982\u9047\u5230\u590d\u6742\u7684\u547d\u4ee4\uff0cscript\u5141\u8bb8\u5728Ansible\u4e3b\u673a\u4e2d\u5199\u597d\u811a\u672c\u5728\u88ab\u7ba1\u63a7\u4e3b\u673a\u7aef\u6267\u884c\uff0c\u7f16\u5199\u7684\u811a\u672c\u53ef\u4ee5\u4e0d\u662fshell\u811a\u672c\uff0c\u5982python\u3001perl\u811a\u672c\u7b49\u5747\u53ef\uff0c\u811a\u672c\u4e5f\u53ef\u4ee5\u6ca1\u6709\u6267\u884c\u6743\u9650\u3002<\/strong><\/p>\n<pre><code class=\"language-shell\"># \u7f16\u5199\u811a\u672c\u521b\u5efa\u7528\u6237\uff0c\u5e76\u8bbe\u7f6e\u5bc6\u7801\u4e3a\uff1a123456\n[root@localhost ansible]# vim  test.sh  \n#!\/bin\/bash\nfor user in user{1..5}\ndo\n    useradd $user\n    echo &#039;123456&#039; | passwd --stdin $user\ndone\n[root@pubserver ansible]# ansible  webservers  -m script  -a  &quot;test.sh&quot;<\/code><\/pre>\n<h3>file\u6a21\u5757<\/h3>\n<p><strong>Ansible\u4f7f\u7528file\u6a21\u5757\u6765\u521b\u5efa\u6587\u4ef6\u3001\u76ee\u5f55\u3001\u94fe\u63a5\uff0c\u4fee\u6539\u6743\u9650\u4e0e\u8bbe\u7f6e\u6587\u4ef6\u7684\u5c5e\u6027\u7b49\u64cd\u4f5c\u3002<\/strong><\/p>\n<p><strong>\u5e42\u7b49\u6027\uff0c\u662f\u6307\u4efb\u610f\u6b21\u6267\u884c\u6240\u4ea7\u751f\u7684\u5f71\u54cd\u5747\u4e0e\u4e00\u6b21\u6267\u884c\u7684\u5f71\u54cd\u76f8\u540c\u3002file\u6a21\u5757\u5177\u6709\u5e42\u7b49\u6027\uff0cAnsible\u4e2d\u5927\u591a\u6570\u7684\u6a21\u5757\u90fd\u5177\u6709\u5e42\u7b49\u6027\u3002<\/strong><\/p>\n<p><strong>file\u6a21\u5757\u5e38\u7528\u7684\u9009\u9879\uff1a<\/strong><\/p>\n<p>\u200b   <strong>path\uff1a\u6307\u5b9a\u6587\u4ef6\u8def\u5f84<\/strong><br \/>\n\u200b           <strong>owner\uff1a\u8bbe\u7f6e\u6587\u4ef6\u6240\u6709\u8005<\/strong><br \/>\n\u200b           <strong>group\uff1a\u8bbe\u7f6e\u6587\u4ef6\u6240\u5c5e\u7ec4<\/strong><br \/>\n\u200b           <strong>state\uff1a\u72b6\u6001\u3002touch\u8868\u793a\u521b\u5efa\u6587\u4ef6\uff0cdirectory\u8868\u793a\u521b\u5efa\u76ee\u5f55\uff0clink\u8868\u793a\u521b\u5efa\u8f6f\u94fe\u63a5\uff0cabsent\u8868\u793a\u5220\u9664<\/strong><br \/>\n\u200b           <strong>mode\uff1a\u8bbe\u7f6e\u6743\u9650<\/strong><br \/>\n\u200b           <strong>src\uff1asource\u7684\u7b80\u5199\uff0c\u6307\u6e90<\/strong><br \/>\n\u200b           <strong>dest\uff1adestination\u7684\u7b80\u5199\uff0c\u6307\u76ee\u6807<\/strong><\/p>\n<pre><code class=\"language-shell\">[root@pubserver ansible]# ansible webservers -m file -a &quot;path=\/tmp\/file.txt state=touch&quot;   #\u65b0\u5efa\u6587\u4ef6\n[root@pubserver ansible]# ansible webservers -m file -a &quot;path=\/tmp\/mydir state=directory&quot;   #\u521b\u5efa\u76ee\u5f55<\/code><\/pre>\n<h3>copy\u6a21\u5757<\/h3>\n<p><strong>Ansible\u4f7f\u7528copy\u6a21\u5757\u5c06\u6587\u4ef6\u62f7\u8d1d\u5230\u8fdc\u7a0b\u7ba1\u63a7\u4e3b\u673a\u4e0a\u3002<\/strong><\/p>\n<p><strong>copy\u6a21\u5757\u5e38\u7528\u9009\u9879\uff1a<\/strong><\/p>\n<p>\u200b   <strong>src\uff1a\u6e90\uff0c\u6307\u63a7\u5236\u7aef\u7684\u6587\u4ef6\u8def\u5f84<\/strong><br \/>\n\u200b           <strong>dest\uff1a\u76ee\u6807\uff0c\u6307\u88ab\u63a7\u5236\u7aef\u7684\u6587\u4ef6\u8def\u5f84<\/strong><br \/>\n\u200b           <strong>content\uff1a\u5185\u5bb9\uff0c\u6307\u9700\u8981\u5199\u5230\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9<\/strong><\/p>\n<pre><code class=\"language-shell\"># \u5c06\u5bb6\u76ee\u5f55\u4e0b\u9762\u7684test.sh\u6587\u4ef6\u590d\u5236\u7ed9webserver\u7ec4\n[root@localhost ansible]# ansible webservers -m copy -a &quot;src=.\/test.sh dest=\/root&quot;<\/code><\/pre>\n<h3>fetch\u6a21\u5757<\/h3>\n<p><strong>fetch\u6a21\u5757\u4e0ecopy\u7c7b\u4f3c\uff0c\u4f46\u662f\u4f5c\u7528\u76f8\u53cd\uff0c\u662f\u5c06\u8fdc\u7a0b\u7ba1\u63a7\u4e3b\u673a\u7684\u6587\u4ef6\u62f7\u8d1d\u5230Ansible\u4e3b\u673a\uff0c\u4f46\u4e0d\u652f\u6301\u76ee\u5f55\u7684\u62f7\u8d1d\u3002<\/strong><\/p>\n<p><strong>fetch\u6a21\u5757\u5e38\u7528\u9009\u9879\uff1a<\/strong><\/p>\n<p>\u200b   <strong>src\uff1a\u6e90\uff0c\u6307\u88ab\u63a7\u5236\u7aef\u7684\u6587\u4ef6\u8def\u5f84<\/strong><\/p>\n<p>\u200b   <strong>dest\uff1a\u76ee\u6807\uff0c\u6307\u63a7\u5236\u7aef\u7684\u6587\u4ef6\u8def\u5f84<\/strong><\/p>\n<pre><code class=\"language-shell\"># \u5c06webserver\u7ec4\u4e2d\u7684\/etc\/hostname\u6587\u4ef6\u90fd\u590d\u5236\u7ed9\u672c\u673a\n[root@localhost ansible]# ansible webservers -m fetch -a &quot;src=\/etc\/hostname dest=~\/&quot;<\/code><\/pre>\n<h3>lineinfile\/replace\u6a21\u5757<\/h3>\n<p><strong>Ansible\u4f7f\u7528lineinfile\u6a21\u5757\u66ff\u6362\u5355\u4e2a\u6587\u4ef6\u4e2d\u7684\u4e00\u6574\u884c\u5185\u5bb9\uff0c\u5982\u679c\u8fdb\u884c\u591a\u884c\u5173\u952e\u5b57\u5339\u914d\u7684\u66ff\u6362\u9700\u8981\u4f7f\u7528replace\u6a21\u5757\u3002<\/strong><\/p>\n<p><strong>lineinfile\u6a21\u5757\u5e38\u7528\u9009\u9879\uff1a<\/strong><\/p>\n<p>\u200b   <strong>path\uff1a\u5f85\u4fee\u6539\u7684\u6587\u4ef6\u8def\u5f84<\/strong><\/p>\n<p>\u200b   <strong>line\uff1a\u5199\u5165\u6587\u4ef6\u7684\u4e00\u884c\u5185\u5bb9<\/strong><\/p>\n<p>\u200b   <strong>regexp\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u67e5\u627e\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9<\/strong><\/p>\n<p><strong>replace\u6a21\u5757\u5e38\u7528\u9009\u9879\uff1a<\/strong><\/p>\n<p>\u200b   <strong>path\uff1a\u5f85\u4fee\u6539\u7684\u6587\u4ef6\u8def\u5f84<\/strong><\/p>\n<p>\u200b   <strong>replace\uff1a\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u5230\u7684\u5185\u5bb9\uff0c\u66ff\u6362\u6210replace\u7684\u5185\u5bb9<\/strong><\/p>\n<p>\u200b   <strong>regexp\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u67e5\u627e\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9<\/strong><\/p>\n<pre><code class=\"language-shell\"># \u5728webserver\u7684\u7528\u6237\u4e2d\u7684\/root\/test.txt \u6587\u4ef6\u4e2d\u8ffd\u52a0 &#039;hi sha bi&#039;\n[root@localhost ansible]# ansible webservers -m lineinfile -a &quot;path=\/root\/test.txt line=&#039;hi sha bi&#039;&quot;\n\n# \u5728webserver\u7684\u7528\u6237\u4e2d\u7684\/root\/test.txt \u6587\u4ef6\u4e2d\u5c06&#039;sha&#039; \u66ff\u6362\u4e3a &#039;si&#039;\n[root@localhost ansible]# ansible webservers -m replace -a &quot;path=\/root\/test.txt regexp=sha replace=si&quot;\n<\/code><\/pre>\n<h3>user\u6a21\u5757<\/h3>\n<p><strong>Ansible\u4f7f\u7528user\u6a21\u5757\u5e2e\u52a9\u7ba1\u7406\u8fdc\u7a0b\u7ba1\u63a7\u4e3b\u673a\u7684\u7cfb\u7edf\u8d26\u6237\uff0c\u5982\u521b\u5efa\u7528\u6237\u3001\u4fee\u6539\u7528\u6237\u3001\u5220\u9664\u7528\u6237\u3001\u4e3a\u7528\u6237\u521b\u5efa\u5bc6\u94a5\u5bf9\u7b49\u64cd\u4f5c\u3002<\/strong><\/p>\n<p><strong>user\u6a21\u5757\u5e38\u7528\u9009\u9879\uff1a<\/strong><\/p>\n<p>\u200b   <strong>name\uff1a\u5f85\u521b\u5efa\u7684\u7528\u6237\u540d<\/strong><\/p>\n<p>\u200b   <strong>uid\uff1a\u7528\u6237ID<\/strong><\/p>\n<p>\u200b   <strong>group\uff1a\u8bbe\u7f6e\u4e3b\u7ec4<\/strong><\/p>\n<p>\u200b   <strong>groups\uff1a\u8bbe\u7f6e\u9644\u52a0\u7ec4<\/strong><\/p>\n<p>\u200b   <strong>home\uff1a\u8bbe\u7f6e\u5bb6\u76ee\u5f55<\/strong><\/p>\n<p>\u200b   <strong>password\uff1a\u8bbe\u7f6e\u7528\u6237\u5bc6\u7801<\/strong><\/p>\n<p>\u200b   <strong>state\uff1a\u8bbe\u7f6e\u72b6\u6001\u3002present\u8868\u793a\u521b\u5efa\uff0c\u5b83\u662f\u9ed8\u8ba4\u9009\u9879\uff0cabsent\u8868\u793a\u5220\u9664<\/strong><\/p>\n<p>\u200b   <strong>remove\uff1a\u5220\u9664\u5bb6\u76ee\u5f55\u3001\u90ae\u7bb1\u7b49\uff0c\u503c\u4e3ayes\u6216true\u90fd\u53ef\u4ee5<\/strong><\/p>\n<pre><code class=\"language-shell\"># \u521b\u5efa\u7528\u6237zhansan\n[root@localhost ansible]# ansible webservers -m user -a &quot;name=zhansan&quot;\n\n# \u521b\u5efa\u7528\u6237lisi uid=2020 \u4e3b\u7ec4\u4e3a\uff1azhansan  \u9644\u52a0\u7ec4\uff1asshd \u5bb6\u76ee\u5f55\uff1a\/home\/lisi \u5bc6\u7801\uff1a123456\n[root@localhost ansible]# ansible webservers -m user -a &quot;name=lisi uid=2020 group=zhansan groups=sshd home=\/home\/lisi password={{&#039;123456&#039;|password_hash(&#039;sha512&#039;)}} state=present&quot; \n<\/code><\/pre>\n<h3>group\u6a21\u5757<\/h3>\n<p><strong>Ansible\u4f7f\u7528group\u6a21\u5757\u5e2e\u52a9\u7ba1\u7406\u8fdc\u7a0b\u7ba1\u63a7\u4e3b\u673a\u7684\u7cfb\u7edf\u7ec4\u8d26\u6237\uff0c\u5982\u521b\u5efa\u3001\u5220\u9664\u7ec4\u7b49\u64cd\u4f5c\u3002<\/strong><\/p>\n<p><strong>group\u6a21\u5757\u5e38\u7528\u9009\u9879\uff1a<\/strong><\/p>\n<p>\u200b   <strong>name\uff1a\u5f85\u521b\u5efa\u7684\u7ec4\u540d<\/strong><\/p>\n<p>\u200b   <strong>gid\uff1a\u7ec4\u7684ID\u53f7<\/strong><\/p>\n<p>\u200b   <strong>state\uff1apresent\u8868\u793a\u521b\u5efa\uff0c\u5b83\u662f\u9ed8\u8ba4\u9009\u9879\u3002absent\u8868\u793a\u5220\u9664<\/strong><\/p>\n<pre><code class=\"language-shell\"># \u521b\u5efa\u7ec4devops \n[root@localhost ansible]# ansible webservers -m group -a &quot;name=devops&quot;\n\n# \u5220\u9664\u7ec4devops\n[root@localhost ansible]# ansible webservers -m group -a &quot;name=devops state=absent&quot;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u547d\u4ee4\u884c\u8bed\u6cd5\u683c\u5f0f \u5bf9Ansible\u8fdb\u884c\u8fdc\u7a0b\u7ba1\u7406\u7684\u4e24\u4e2a\u65b9\u6cd5 ad-hoc\u4e34\u65f6\u547d\u4ee4\uff0c\u5c31\u662f\u5728\u547d\u4ee4\u884c\u4e0a\u6267\u884c\u7ba1\u7406\u547d\u4ee4 playbook\u5267\u672c\uff0c\u628a\u7ba1\u7406\u4efb\u52a1\u7528\u7279\u5b9a\u683c\u5f0f\u5199\u5230\u6587\u4ef6\u4e2d \u65e0\u8bba\u54ea\u79cd\u65b9\u5f0f\uff0c\u90fd\u662f\u901a\u8fc7\u6a21\u5757\u52a0\u53c2\u6570\u8fdb\u884c\u7ba1\u7406\u3002\u901a\u5e38\u4f7f\u7528ad-hoc\u547d\u4ee4\u884c\u7ba1\u7406\u65b9\u5f0f\uff0c\u6765\u5b9e\u73b0\u4e00\u4e9b\u65e5\u5e38\u4e34\u65f6\u6027\u7684\u6279\u91cf\u7ba1\u7406\u7684\u4efb\u52a1\u3002\u9700\u8981\u5b9e\u73b0\u8bbe\u7f6e\u76f8\u5bf9\u590d\u6742\uff0c\u9700\u8981\u5927\u91cf\u7684\u64cd\u4f5c\u4efb\u52a1\uff0c\u4f7f\u7528playbook\u5267\u672c\u7ba1\u7406\u65b9\u5f0f\u3002 # ad-hoc\u547d\u4ee4\u884c\u57fa\u672c\u683c\u5f0f\uff1aansible \u4e3b\u673a\u96c6\u5408 -m \u6a21\u5757\u540d -a &quot;\u53c2\u6570&quot; \u5176\u4e2d\uff1a \u4e3b\u673a\u96c6\u5408\uff1a\u6307\u5b9a\u7ba1\u7406\u4e3b\u673a\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u662f\u4e3b\u673a\u540d\u3001\u4e3b\u673aIP\u5730\u5740\u6216\u8005\u662fall\u8868\u793a\u6240\u6709\u4e3b\u673a \u6a21\u5757\u540d\uff1a\u6307\u5b9a\u529f\u80fd\u6a21\u5757\u540d\u79f0 \u53c2\u6570\uff1a\u5e94\u7528\u6a21\u5757\u529f\u80fd\u4e2d\u7684\u54ea\u4e9b\u529f\u80fd\uff0c\u6807\u51c6\u53c2\u6570\u5b9a\u4e49 \u5176\u4ed6\u53c2\u6570\uff1a-k \u4f7f\u7528\u5bc6\u7801\u8fdc\u7a0b\uff0c\u8fdc\u7a0b\u4e3b\u673aSSH\u8fde\u63a5\u5bc6\u7801\u4fe1\u606f \u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a [root@pubserver ~]# cd ~\/ansible [root@pubserver ansible]# ansible all &#8211;list-hosts #\u67e5\u770b\u6240\u6709\u4e3b\u673a\u5217\u8868 hosts (3): server1 server2 server3 #\u8c03\u7528ping\u6a21\u5757 [root@pubserver ansible]# ansible all -m ping #\u6d4b\u8bd5\u8fdc\u7a0b\u4e3b\u673a\u662f\u5426\u8fde\u901a \u901a\u8fc7ping\u6a21\u5757\u6d4b\u8bd5\u5230\u8fdc\u7a0b\u4e3b\u673a\u7684\u8fde\u901a\u6027\uff0c\u68c0\u67e5\u662f\u5426\u53ef\u4ee5\u7ba1\u7406\u8fdc\u7a0b\u4e3b\u673a\u3002 ping\u7ed3\u679c\u4e3a\u7eff\u8272\u7684SUCCESS\uff0c\u5219\u8868\u793a\u6210\u529f\uff1b\u662f\u7ea2\u8272\u7684UNREACHABLE\uff0c\u5219\u68c0\u67e5\u7f51\u7edc\u662f\u5426\u53ef\u901a\uff0c\u662f\u5426\u5df2\u7ecf\u53ef\u4ee5\u514d\u5bc6\u767b\u9646\u3002 \u5e38\u7528\u6a21\u5757 shell\u6a21\u5757 \u5728\u8fdc\u7a0b\u4e3b\u673a\u6267\u884c\u547d\u4ee4\u65f6\uff0ccommand\u6a21\u5757\u4e3a\u9ed8\u8ba4\u6a21\u5757\u3002 Shell\u6a21\u5757\u529f\u80fd\u540ccommand\u529f\u80fd\u7c7b\u4f3c\uff0c\u4e8c\u8005\u7684\u533a\u522b\u5728\u4e8e\uff1a command\u4e0d\u652f\u6301bash\u7684\u7279\u6027\uff0c\u5982\u7ba1\u9053\u548c\u91cd\u5b9a\u5411\u7b49\u529f\u80fd\uff0c\u6240\u6709\u9700\u8981\u8c03\u7528shell\u7684\u529f\u80fd\u90fd\u65e0\u6cd5\u4f7f\u7528 # \u67e5\u770b\u8fdb\u7a0b\u6570\u91cf [root@localhost ansible]# ansible webservers -m shell -a &quot;ps aux | wc -l&quot; server1 | CHANGED | rc=0 &gt;&gt; 144 server2 | CHANGED | rc=0 &gt;&gt; 144 scrpt\u6a21\u5757 \u5982\u9047\u5230\u590d\u6742\u7684\u547d\u4ee4\uff0cscript\u5141\u8bb8\u5728Ansible\u4e3b\u673a\u4e2d\u5199\u597d\u811a\u672c\u5728\u88ab\u7ba1\u63a7\u4e3b\u673a\u7aef\u6267\u884c\uff0c\u7f16\u5199\u7684\u811a\u672c\u53ef\u4ee5\u4e0d\u662fshell\u811a\u672c\uff0c\u5982python\u3001perl\u811a\u672c\u7b49\u5747\u53ef\uff0c\u811a\u672c\u4e5f\u53ef\u4ee5\u6ca1\u6709\u6267\u884c\u6743\u9650\u3002 # \u7f16\u5199\u811a\u672c\u521b\u5efa\u7528\u6237\uff0c\u5e76\u8bbe\u7f6e\u5bc6\u7801\u4e3a\uff1a123456 [root@localhost ansible]# vim test.sh #!\/bin\/bash for user in user{1..5} do useradd $user echo &#039;123456&#039; | passwd &#8211;stdin $user done [root@pubserver ansible]# ansible webservers -m script -a &quot;test.sh&quot; file\u6a21\u5757 Ansible\u4f7f\u7528file\u6a21\u5757\u6765\u521b\u5efa\u6587\u4ef6\u3001\u76ee\u5f55\u3001\u94fe\u63a5\uff0c\u4fee\u6539\u6743\u9650\u4e0e\u8bbe\u7f6e\u6587\u4ef6\u7684\u5c5e\u6027\u7b49\u64cd\u4f5c\u3002 \u5e42\u7b49\u6027\uff0c\u662f\u6307\u4efb\u610f\u6b21\u6267\u884c\u6240\u4ea7\u751f\u7684\u5f71\u54cd\u5747\u4e0e\u4e00\u6b21\u6267\u884c\u7684\u5f71\u54cd\u76f8\u540c\u3002file\u6a21\u5757\u5177\u6709\u5e42\u7b49\u6027\uff0cAnsible\u4e2d\u5927\u591a\u6570\u7684\u6a21\u5757\u90fd\u5177\u6709\u5e42\u7b49\u6027\u3002 file\u6a21\u5757\u5e38\u7528\u7684\u9009\u9879\uff1a \u200b path\uff1a\u6307\u5b9a\u6587\u4ef6\u8def\u5f84 \u200b owner\uff1a\u8bbe\u7f6e\u6587\u4ef6\u6240\u6709\u8005 \u200b group\uff1a\u8bbe\u7f6e\u6587\u4ef6\u6240\u5c5e\u7ec4 \u200b state\uff1a\u72b6\u6001\u3002touch\u8868\u793a\u521b\u5efa\u6587\u4ef6\uff0cdirectory\u8868\u793a\u521b\u5efa\u76ee\u5f55\uff0clink\u8868\u793a\u521b\u5efa\u8f6f\u94fe\u63a5\uff0cabsent\u8868\u793a\u5220\u9664 \u200b mode\uff1a\u8bbe\u7f6e\u6743\u9650 \u200b src\uff1asource\u7684\u7b80\u5199\uff0c\u6307\u6e90 \u200b dest\uff1adestination\u7684\u7b80\u5199\uff0c\u6307\u76ee\u6807 [root@pubserver ansible]# ansible webservers -m file -a &quot;path=\/tmp\/file.txt state=touch&quot; #\u65b0\u5efa\u6587\u4ef6 [root@pubserver ansible]# ansible webservers -m file -a &quot;path=\/tmp\/mydir state=directory&quot; #\u521b\u5efa\u76ee\u5f55 copy\u6a21\u5757 Ansible\u4f7f\u7528copy\u6a21\u5757\u5c06\u6587\u4ef6\u62f7\u8d1d\u5230\u8fdc\u7a0b\u7ba1\u63a7\u4e3b\u673a\u4e0a\u3002 copy\u6a21\u5757\u5e38\u7528\u9009\u9879\uff1a \u200b src\uff1a\u6e90\uff0c\u6307\u63a7\u5236\u7aef\u7684\u6587\u4ef6\u8def\u5f84 \u200b dest\uff1a\u76ee\u6807\uff0c\u6307\u88ab\u63a7\u5236\u7aef\u7684\u6587\u4ef6\u8def\u5f84 \u200b content\uff1a\u5185\u5bb9\uff0c\u6307\u9700\u8981\u5199\u5230\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9 # \u5c06\u5bb6\u76ee\u5f55\u4e0b\u9762\u7684test.sh\u6587\u4ef6\u590d\u5236\u7ed9webserver\u7ec4 [root@localhost ansible]# ansible webservers -m copy -a &quot;src=.\/test.sh de&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-lixx"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=203"}],"version-history":[{"count":1,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/203\/revisions"}],"predecessor-version":[{"id":204,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/203\/revisions\/204"}],"wp:attachment":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}