{"id":1145,"date":"2023-05-30T15:24:49","date_gmt":"2023-05-30T07:24:49","guid":{"rendered":"https:\/\/www.qiangzhenshuai.com\/?p=1145"},"modified":"2023-08-14T13:26:52","modified_gmt":"2023-08-14T05:26:52","slug":"ansible%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"https:\/\/www.qiangzhenshuai.com\/?p=1145","title":{"rendered":"Ansible\u5165\u95e8"},"content":{"rendered":"<h1>1.Ansible\u4ecb\u7ecd<\/h1>\n<p>ansible\u662f\u65b0\u51fa\u73b0\u7684\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177\uff0c\u57fa\u4e8ePython\u5f00\u53d1\uff0c\u96c6\u5408\u4e86\u4f17\u591a\u8fd0\u7ef4\u5de5\u5177\uff08puppet\u3001chef\u3001func\u3001fabric\uff09\u7684\u4f18\u70b9\uff0c\u5b9e\u73b0\u4e86\u6279\u91cf\u7cfb\u7edf\u914d\u7f6e\u3001\u6279\u91cf\u7a0b\u5e8f\u90e8\u7f72\u3001\u6279\u91cf\u8fd0\u884c\u547d\u4ee4\u7b49\u529f\u80fd\u3002<br \/>\nansible\u662f\u57fa\u4e8e paramiko \u5f00\u53d1\u7684,\u5e76\u4e14\u57fa\u4e8e\u6a21\u5757\u5316\u5de5\u4f5c\uff0c\u672c\u8eab\u6ca1\u6709\u6279\u91cf\u90e8\u7f72\u7684\u80fd\u529b\u3002\u771f\u6b63\u5177\u6709\u6279\u91cf\u90e8\u7f72\u7684\u662fansible\u6240\u8fd0\u884c\u7684\u6a21\u5757\uff0cansible\u53ea\u662f\u63d0\u4f9b\u4e00\u79cd\u6846\u67b6\u3002ansible\u4e0d\u9700\u8981\u5728\u8fdc\u7a0b\u4e3b\u673a\u4e0a\u5b89\u88c5client\/agents\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u57fa\u4e8essh\u6765\u548c\u8fdc\u7a0b\u4e3b\u673a\u901a\u8baf\u7684\u3002ansible\u76ee\u524d\u5df2\u7ecf\u5df2\u7ecf\u88ab\u7ea2\u5e3d\u5b98\u65b9\u6536\u8d2d\uff0c\u662f\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177\u4e2d\u5927\u5bb6\u8ba4\u53ef\u5ea6\u6700\u9ad8\u7684\uff0c\u5e76\u4e14\u4e0a\u624b\u5bb9\u6613\uff0c\u5b66\u4e60\u7b80\u5355\u3002<\/p>\n<h2><a href=\"https:\/\/docs.ansible.com\/index.html\">\u5b98\u7f51<\/a><\/h2>\n<h1>2.Ansible\u53ef\u4ee5\u5b9e\u73b0\u4ee5\u4e0b\u529f\u80fd<\/h1>\n<p>\u81ea\u52a8\u5316\u914d\u7f6e\u7ba1\u7406\uff1a\u53ef\u4ee5\u4f7f\u7528Ansible\u5728\u591a\u53f0\u670d\u52a1\u5668\u4e0a\u81ea\u52a8\u914d\u7f6e\u8f6f\u4ef6\u3001\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\u3001\u5b89\u88c5\u66f4\u65b0\u7b49\u64cd\u4f5c\uff0c\u63d0\u9ad8\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<p>\u5e94\u7528\u7a0b\u5e8f\u90e8\u7f72\uff1a\u53ef\u4ee5\u4f7f\u7528Ansible\u5728\u591a\u53f0\u670d\u52a1\u5668\u4e0a\u81ea\u52a8\u90e8\u7f72\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f8b\u5982Web\u5e94\u7528\u7a0b\u5e8f\u3001\u6570\u636e\u5e93\u7b49\u3002<\/p>\n<p>\u4e91\u90e8\u7f72\uff1a\u53ef\u4ee5\u4f7f\u7528Ansible\u5728\u591a\u4e2a\u4e91\u5e73\u53f0\u4e0a\u81ea\u52a8\u90e8\u7f72\u548c\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f8b\u5982AWS\u3001Azure\u3001Google Cloud\u7b49\u3002<\/p>\n<p>\u7cfb\u7edf\u7ba1\u7406\uff1a\u53ef\u4ee5\u4f7f\u7528Ansible\u7ba1\u7406\u591a\u53f0\u670d\u52a1\u5668\uff0c\u4f8b\u5982\u6267\u884c\u547d\u4ee4\u3001\u67e5\u770b\u7cfb\u7edf\u4fe1\u606f\u3001\u76d1\u63a7\u670d\u52a1\u5668\u7b49<\/p>\n<h1>3.Ansible\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9<\/h1>\n<p>\u7b80\u5355\u6613\u7528\uff1aAnsible\u4f7f\u7528YAML\u8bed\u8a00\u8fdb\u884c\u914d\u7f6e\u548c\u7f16\u5199\u4efb\u52a1\uff0c\u6613\u4e8e\u5b66\u4e60\u548c\u4f7f\u7528\u3002<\/p>\n<p>\u8de8\u5e73\u53f0\u652f\u6301\uff1aAnsible\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\uff0c\u4f8b\u5982Linux\u3001Windows\u7b49\u3002<\/p>\n<p>\u6269\u5c55\u6027\u5f3a\uff1aAnsible\u53ef\u4ee5\u4f7f\u7528\u63d2\u4ef6\u6269\u5c55\u529f\u80fd\uff0c\u4f8b\u5982\u652f\u6301\u4e0d\u540c\u7684\u4e91\u5e73\u53f0\u3001\u6570\u636e\u5e93\u7b49\u3002<\/p>\n<p>\u5b89\u5168\u6027\u9ad8\uff1aAnsible\u4f7f\u7528SSH\u534f\u8bae\u8fdb\u884c\u901a\u4fe1\u548c\u6267\u884c\u547d\u4ee4\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u6570\u636e\u4f20\u8f93\u7684\u5b89\u5168\u6027<\/p>\n<h1>4.Ansible\u7279\u70b9<\/h1>\n<p>\u90e8\u7f72\u7b80\u5355\uff0c\u53ea\u9700\u5728\u4e3b\u63a7\u7aef\u90e8\u7f72Ansible\u73af\u5883\uff0c\u88ab\u63a7\u7aef\u65e0\u9700\u505a\u4efb\u4f55\u64cd\u4f5c\uff1b<\/p>\n<p>\u9ed8\u8ba4\u4f7f\u7528SSH\u534f\u8bae\u5bf9\u8bbe\u5907\u8fdb\u884c\u7ba1\u7406\uff1b<\/p>\n<p>\u6709\u5927\u91cf\u5e38\u89c4\u8fd0\u7ef4\u64cd\u4f5c\u6a21\u5757\uff0c\u53ef\u5b9e\u73b0\u65e5\u5e38\u7edd\u5927\u90e8\u5206\u64cd\u4f5c\uff1b<\/p>\n<p>\u914d\u7f6e\u7b80\u5355\u3001\u529f\u80fd\u5f3a\u5927\u3001\u6269\u5c55\u6027\u5f3a\uff1b<\/p>\n<p>\u652f\u6301API\u53ca\u81ea\u5b9a\u4e49\u6a21\u5757\uff0c\u53ef\u901a\u8fc7Python\u8f7b\u677e\u6269\u5c55\uff1b<\/p>\n<p>\u901a\u8fc7Playbooks\u6765\u5b9a\u5236\u5f3a\u5927\u7684\u914d\u7f6e\u3001\u72b6\u6001\u7ba1\u7406\uff1b<\/p>\n<p>\u8f7b\u91cf\u7ea7\uff0c\u65e0\u9700\u5728\u5ba2\u6237\u7aef\u5b89\u88c5agent\uff0c\u66f4\u65b0\u65f6\uff0c\u53ea\u9700\u5728\u64cd\u4f5c\u673a\u4e0a\u8fdb\u884c\u4e00\u6b21\u66f4\u65b0\u5373\u53ef\uff1b<\/p>\n<p>\u63d0\u4f9b\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u3001\u64cd\u4f5c\u6027\u5f3a\u7684Web\u7ba1\u7406\u754c\u9762\u548cREST API\u63a5\u53e3\u2014\u2014AWX\u5e73\u53f0<\/p>\n<h1>5.Ansible\u67b6\u6784<\/h1>\n<p><a href=\"https:\/\/www.qiangzhenshuai.com\/?attachment_id=1147\" rel=\"attachment wp-att-1147\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-1147\" src=\"https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/05\/ansible\u67b6\u6784\u56fe.png?resize=640%2C340&#038;ssl=1\" alt=\"\" width=\"640\" height=\"340\" srcset=\"https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/05\/ansible\u67b6\u6784\u56fe.png?w=945&amp;ssl=1 945w, https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/05\/ansible\u67b6\u6784\u56fe.png?resize=300%2C159&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/05\/ansible\u67b6\u6784\u56fe.png?resize=768%2C408&amp;ssl=1 768w\" sizes=\"(max-width: 640px) 100vw, 640px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>Ansible\u901a\u8fc7\u4e3b\u63a7\u7aef\uff08\u5de6\u4fa7\uff09\u6765\u63a7\u5236\u5176\u4ed6\u4e3b\u673a\uff08\u53f3\u4fa7\uff09\uff0c\u5927\u81f4\u53ef\u4ee5\u5206\u4e3a\u4e09\u4e2a\u5c42\u6b21\uff1a\u7528\u6237\u3001\u4e3b\u63a7\u7aef\u548c\u88ab\u63a7\u7aef\u3002\u5176\u4e2d\uff1a<\/p>\n<p>\u6838\u5fc3\uff1aAnsible<\/p>\n<p>\u6838\u5fc3\u6a21\u5757\uff08core modeles\uff09\uff1aansible\u81ea\u5e26\u7684\u6a21\u5757\uff0cAnsible\u6a21\u5757\u8d44\u6e90\u5206\u53d1\u5230\u8fdc\u7a0b\u8282\u70b9\u4f7f\u5176\u6267\u884c\u7279\u5b9a\u4efb\u52a1\u6216\u8005\u5339\u914d\u4e00\u4e2a\u7279\u5b9a\u72b6\u6001<\/p>\n<p>\u6269\u5c55\u6a21\u5757\uff08custom modules\uff09\uff1a\u5982\u679c\u7528\u6237\u6709\u81ea\u5df1\u7684\u7279\u6b8a\u9700\u6c42\uff0c\u53ef\u4ee5\u81ea\u5df1\u6dfb\u52a0\u6a21\u5757\uff0c\u5c5e\u4e8e\u6269\u5c55\u6a21\u5757<\/p>\n<p>\u63d2\u4ef6\uff08plugins\uff09\uff1a\u5b8c\u6210\u6a21\u5757\u7684\u8865\u5145\u529f\u80fd\uff0c\u501f\u52a9\u4e8e\u63d2\u4ef6\u5b8c\u6210\u8bb0\u5f55\u65e5\u5fd7\uff0c\u90ae\u4ef6\u7b49\u529f\u80fd<\/p>\n<p>\u5267\u672c\uff08playbook\uff09\uff1aansible\u7684\u4efb\u52a1\u914d\u6587\u4ef6\uff0c\u5c06\u591a\u4e2a\u4efb\u52a1\u5b9a\u4e49\u5728\u5267\u672c\u4e2d\uff0c\u7531ansible\u81ea\u52a8\u6267\u884c\u3002<\/p>\n<p>\u8fde\u63a5\u63d2\u4ef6\uff08connection plugins\uff09\uff1aansible\u57fa\u4e8e\u8fde\u63a5\u63d2\u4ef6\u8fde\u63a5\u5230\u5404\u4e2a\u4e3b\u673a\uff0c\u867d\u7136ansible\u662f\u4f7f\u7528ssh\u8fde\u63a5\u5230\u4e3b\u673a\u7684\uff0c\u4f46\u662f\u4ed6\u8fd8\u6709\u5176\u4ed6\u8fde\u63a5\u65b9\u6cd5\uff08\u6bd4\u5982ZeroMQ\uff09\uff0c\u6240\u4ee5\u9700\u8981\u8fde\u63a5\u63d2\u4ef6\u3002<\/p>\n<p>\u4e3b\u673a\u5217\u8868\uff08host inventory\uff09\uff1a\u5b9a\u4e49Ansible\u7ba1\u7406\u7684\u4e3b\u673a\u7b56\u7565\uff0c\u9ed8\u8ba4\u662f\u5728Ansible hosts\u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\u88ab\u7ba1\u8282\u70b9\uff0c\u540c\u5fd7\u4e5f\u652f\u6301\u81ea\u5b9a\u4e49\u52a8\u6001\u4e3b\u673a\u6e05\u5355\u548c\u6307\u5b9a\u914d\u7f6e\u6587\u4ef6\u7684\u4f4d\u7f6e<\/p>\n<h1>6.Ansible\u547d\u4ee4\u4ecb\u7ecd<\/h1>\n<p>-a\uff1a\u6a21\u5757\u7684\u53c2\u6570,\u5982\u679c\u6267\u884c\u9ed8\u8ba4COMMAND\u7684\u6a21\u5757<br \/>\n-k\uff1a\u5bc6\u7801\u8ba4\u8bc1,\u9ed8\u8ba4\u4f7f\u7528\u79d8\u94a5\u8ba4\u8bc1<br \/>\n-K\uff1a\u63d0\u793a\u5bc6\u7801\u4f7f\u7528sudo,sudo\u8868\u793a\u63d0\u6743\u64cd\u4f5c<br \/>\n-B\uff1a\u540e\u53f0\u8fd0\u884c\u8d85\u65f6\u65f6\u95f4<br \/>\n-C\uff1a\u6a21\u62df\u6267\u884c\u6d4b\u8bd5\uff0c\u4f46\u4e0d\u4f1a\u771f\u7684\u6267\u884c<br \/>\n-f\uff1a\u5e76\u884c\u4efb\u52a1\u6570\u3002NUM\u88ab\u6307\u5b9a\u4e3a\u4e00\u4e2a\u6574\u6570,\u9ed8\u8ba4\u662f5<br \/>\n-h\uff1a\u6253\u5f00\u5e2e\u52a9\u6587\u6863API<br \/>\n-i\uff1a\u6307\u5b9a\u5e93\u5b58\u4e3b\u673a\u6587\u4ef6\u7684\u8def\u5f84,\u9ed8\u8ba4\u4e3a\/etc\/ansible\/hosts<br \/>\n-l\uff1a\u8fdb\u4e00\u6b65\u9650\u5236\u6240\u9009\u4e3b\u673a\/\u7ec4\u6a21\u5f0f &#8211;limit=192.168.91.135 \u53ea\u5bf9\u8fd9\u4e2aip\u6267\u884c<br \/>\n-m\uff1a\u6267\u884c\u6a21\u5757\u7684\u540d\u5b57\uff0c\u9ed8\u8ba4\u4f7f\u7528command\u6a21\u5757<br \/>\n-M\uff1a\u8981\u6267\u884c\u7684\u6a21\u5757\u7684\u8def\u5f84\uff0c\u9ed8\u8ba4\u4e3a\/usr\/share\/ansible\/<br \/>\n-T\uff1a\u6307\u5b9aSSH\u9ed8\u8ba4\u8d85\u65f6\u65f6\u95f4\uff0c \u9ed8\u8ba4\u662f10S<br \/>\n-u\uff1a\u8fdc\u7a0b\u7528\u6237\uff0c\u9ed8\u8ba4\u662froot\u7528\u6237<br \/>\n-v\uff1a\u663e\u793a\u8be6\u7ec6\u4fe1\u606f \u7528\u4e8e\u6392\u9519<br \/>\n&#8211;version\uff1a\u8f93\u51faansible\u7684\u7248\u672c<\/p>\n<p>ansible-doc<br \/>\n-h\uff1a\u793a\u547d\u4ee4\u53c2\u6570API\u6587\u6863<br \/>\n-M\uff1a\u67e5\u8be2\u6a21\u5757\uff0c&#8211;module-path=MODULE_PATH \u6307\u5b9a\u6a21\u5757\u7684\u8def\u5f84<br \/>\n-l\uff1a\u663e\u793a\u5df2\u5b58\u5728\u7684\u6240\u6709\u6a21\u5757\u5217\u8868<br \/>\n-s\uff1a\u663e\u793aplaybook\u5236\u5b9a\u6a21\u5757\u7684\u7528\u6cd5<\/p>\n<h1>7.Ansible\u5b89\u88c5\u914d\u7f6e<\/h1>\n<h4>\u5b89\u88c5<\/h4>\n<p>[root@ansible ~]$ yum install -y ansible<\/p>\n<h4>\u67e5\u770bansible\u7684\u7248\u672c<\/h4>\n<p>[root@ansible ~]$ ansible &#8211;version<br \/>\nansible 2.9.27<br \/>\nconfig file = \/etc\/ansible\/ansible.cfg \u00a0\u00a0#\u914d\u7f6e\u6587\u4ef6\u7684\u4f4d\u7f6e<br \/>\nconfigured module search path = [u&#8217;\/root\/.ansible\/plugins\/modules&#8217;, u&#8217;\/usr\/share\/ansible\/plugins\/modules&#8217;]<br \/>\nansible python module location = \/usr\/lib\/python2.7\/site-packages\/ansible<br \/>\nexecutable location = \/usr\/bin\/ansible<br \/>\npython version = 2.7.5 (default, Apr 2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]<\/p>\n<h4>\u914d\u7f6e\u6587\u4ef6<\/h4>\n<p>Ansible\u7684\u914d\u7f6e\u6587\u4ef6\u53ef\u4ee5\u5b58\u653e\u5728\u4efb\u4f55\u4f4d\u7f6e\uff0c\u4f46\u914d\u7f6e\u6587\u4ef6\u6709\u8bfb\u53d6\u987a\u5e8f\uff0c\u5148Ansible\u914d\u7f6e\u505a\u4e00\u4e2a\u57fa\u672c\u4e86\u89e3\u3002<\/p>\n<p>ansible\u7684\u914d\u7f6e\u6587\u4ef6\u6709\u67e5\u627e\u987a\u5e8f\uff1a<\/p>\n<p>\u6700\u5148\u67e5\u627e$ANSIBLE_CONFIG\u53d8\u91cf<\/p>\n<p>\u5176\u6b21\u67e5\u627e\u5f53\u524d\u76ee\u5f55\u4e0bansible.cfg<\/p>\n<p>\u7136\u540e\u67e5\u627e\u7528\u6237\u5bb6\u76ee\u5f55\u4e0b\u7684.ansible.cfg<\/p>\n<p>\u6700\u540e\u67e5\u627e\/etc\/ansible\/ansible.cfg(\u9ed8\u8ba4)<\/p>\n<p>[root@ansible ~]$cat \/etc\/ansible\/ansible.cfg<\/p>\n<p>#inventory = \/etc\/ansible\/hosts\u00a0 \u00a0#\u4e3b\u673a\u5217\u8868\u914d\u7f6e\u6587\u4ef6<br \/>\n#library = \/usr\/share\/my_modules\/\u00a0 \u00a0 #\u5e93\u6587\u4ef6\u5b58\u653e\u76ee\u5f55<br \/>\n#remote_tmp = ~\/.ansible\/tmp\u00a0 \u00a0 #\u4e34\u65f6py\u6587\u4ef6\u5b58\u653e\u5728\u8fdc\u7a0b\u4e3b\u673a\u76ee\u5f55<br \/>\n#local_tmp = ~\/.ansible\/tmp\u00a0 \u00a0 #\u672c\u673a\u7684\u4e34\u65f6\u6267\u884c\u76ee\u5f55<br \/>\n#forks = 5\u00a0#\u9ed8\u8ba4\u5e76\u53d1\u6570<br \/>\n#sudo_user = root\u00a0#\u9ed8\u8ba4sudo\u7528\u6237<br \/>\n#ask_sudo_pass = True\u00a0 \u00a0#\u6bcf\u6b21\u6267\u884c\u662f\u5426\u8be2\u95eesudo\u7684ssh\u5bc6\u7801<br \/>\n#ask_pass = True\u00a0#\u6bcf\u6b21\u6267\u884c\u662f\u5426\u8be2\u95eessh\u5bc6\u7801<br \/>\n#remote_port = 22\u00a0 \u00a0 #\u8fdc\u7a0b\u4e3b\u673a\u7aef\u53e3 \u00a0 ssh\u7aef\u53e3\u53f7<br \/>\nhost_key_checking = False\u00a0 \u00a0#\u8df3\u8fc7\u68c0\u67e5\u4e3b\u673a\u6307\u7eb9<br \/>\nlog_path = \/var\/log\/ansible.log\u00a0 \u00a0#ansible\u65e5\u5fd7<\/p>\n<p>[privilege_escalation]\u00a0#\u5982\u679c\u662f\u666e\u901a\u7528\u6237\u5219\u9700\u8981\u914d\u7f6e\u63d0\u6743<br \/>\n#become=True<br \/>\n#become_method=sudo<br \/>\n#become_user=root<br \/>\n#become_ask_pass=False<\/p>\n<p>[root@ansible ~]$ egrep -v &#8216;#|^$&#8217; \/etc\/ansible\/ansible.cfg<br \/>\n[defaults]<br \/>\nhost_key_checking = False<br \/>\nlog_path = \/var\/log\/ansible.log<br \/>\n[inventory]<br \/>\n[privilege_escalation]<br \/>\n[paramiko_connection]<br \/>\n[ssh_connection]<br \/>\n[persistent_connection]<br \/>\n[accelerate]<br \/>\n[selinux]<br \/>\n[colors]<br \/>\n[diff]<\/p>\n<h1>8.Ansible Inventory<\/h1>\n<p data-pid=\"S-ZPTdqs\">ansible\u7684inventory\u662f\u4e00\u79cd\u5bf9ansible\u7ba1\u7406\u7684\u4e3b\u673a\u6761\u76ee\u8fdb\u884c\u7ec4\u7ec7\u7684\u6587\u4ef6\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u628a\u5b83\u5f53\u4f5c\u4e00\u4e2aExcel\u8868\u683c\uff0c\u8868\u683c\u4e2d\u5355\u5143\u683c\u7684\u5185\u5bb9\u5c31\u662f\u4f60\u8981\u7ba1\u7406\u7684\u4e3b\u673a\uff0c\u4e00\u822c\u6765\u8bf4\u662fIP\u5730\u5740\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528group\u7684\u6982\u5ff5\u5bf9\u5355\u5143\u683c\u5185\u5bb9\u8fdb\u884c\u5206\u7ec4\u3002<\/p>\n<p data-pid=\"5fesAna4\">hosts\u662finventory\u7684\u57fa\u672c\u5355\u5143\uff0c\u4e00\u822c\u4ee5IP\u5730\u5740\u7684\u5f62\u5f0f\u5c55\u73b0\u3002 group\u662finventory\u7684\u7ec4\uff0c\u53ef\u4ee5\u628ahosts\u8fdb\u884c\u5f52\u7c7b\uff0c\u4ece\u800c\u65b9\u4fbf\u8fdb\u884c\u7ba1\u7406\u3002 inventory\u7684\u6587\u4ef6\u4f4d\u7f6e\uff1a \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cinventory\u7684\u4f4d\u7f6e\u4f4d\u4e8e\/etc\/ansible\/hosts\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u81ea\u5df1\u5b9a\u4e49\u4e00\u4e2a\u4f4d\u7f6e\u3002\u4fee\u6539\/etc\/ansible\/cfg\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684inventory=inventory_file_path\uff0c\u5373\u53ef\u81ea\u5b9a\u4e49inventory\u7684\u4f4d\u7f6e\u3002<\/p>\n<h3><b>\u683c\u5f0f<\/b><\/h3>\n<p data-pid=\"lNW3nJeC\">inventory\u6709\u4e24\u79cd\u683c\u5f0f\uff0c\u4e00\u79cd\u662fyaml\u683c\u5f0f\uff0c\u4e00\u79cd\u662fini\u683c\u5f0f\u3002 inventory\u7684\u4e3b\u673a\u6240\u6709\u4e3b\u673a\u90fd\u5f52\u4e8eall\u7684\u9ed8\u8ba4\u5206\u7ec4\u91cc\u9762\uff0c\u5e76\u4e14\u4e00\u4e2a\u4e3b\u673a\u8981\u4e48\u4f4d\u4e8e\u67d0\u4e2a\u4f60\u5b9a\u4e49\u7684\u7ec4\u4e2d\uff0c\u8981\u4e48\u5c5e\u4e8eUngrouped\u7684\u72b6\u6001\uff0c\u4e5f\u5c31\u662f\u65e0\u5bb6\u53ef\u5f52\u7684\u72b6\u6001\uff0c\u7c7b\u4f3c\u4e8e\u4f60\u5c5e\u4e8e\u67d0\u4e2a\u516c\u53f8\uff0c\u6216\u8005\u4f60\u5c5e\u4e8e\u81ea\u7531\u804c\u4e1a\u8005\u7684\u610f\u601d\uff0c\u4f46\u662f\u4e0d\u7ba1\u81ea\u7531\u804c\u4e1a\u8005\uff0c\u8fd8\u662f\u975e\u81ea\u7531\u804c\u4e1a\u8005\uff0c\u90fd\u5f52\u4e8eall\u8fd9\u4e2a\u5168\u4eba\u7c7b\u5927\u5bb6\u5ead\u3002<\/p>\n<p>yaml\u662f\u4e00\u79cd\u6587\u4ef6\u683c\u5f0f\uff0cyaml\u662fyet another xml\u7684\u7b80\u5199\uff0c\u7c7b\u4f3c\u4e8exml\u6216\u8005json\u7684\u6837\u5b50\u7528\u4e8e\u5bf9\u6761\u76ee\u8fdb\u884c\u5206\u7ec4\u7ba1\u7406\u3002\u5982\u679c\u91c7\u7528yaml\u7684\u65b9\u5f0f\uff0c\u5e76\u4e0d\u662f\u968f\u4fbf\u5199\u5c31\u53ef\u4ee5\uff0c\u800c\u662f\u8981\u9075\u5b88\u4ee5\u4e0b\u89c4\u5b9a\uff0c\u57fa\u672c\u5355\u5143\u5fc5\u987b\u7528hosts\u6765\u5305\u56f4\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0cip\u5730\u5740\u7684\u524d\u9762\u65e0\u8bba\u5982\u4f55\u4e5f\u5f97\u6709\u4e2ahosts\u6253\u5934\uff0c\u7136\u540e\u5982\u679c\u4f60\u7684\u5206\u7ec4\u4e0b\u9762\u4e0d\u662f\u76f4\u63a5\u662fhosts\u7684\u8bdd\uff0c\u90a3\u5c31\u5f97\u5199\u4e2achildren\uff0c\u7136\u540echildren\u4e0b\u9762\u518d\u5199\u4f60\u7684\u5206\u7ec4\u540d\uff0c\u8fd9\u79cd\u89c4\u5b9a\u611f\u89c9\u6709\u70b9\u9ebb\u70e6\uff0c\u4f46\u662f\u89c4\u5b9a\u5c31\u662f\u89c4\u5b9a\u3002\u4ee5\u4e0b\u662f\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u770b\u5230\u4efb\u4f55\u4e00\u4e2ahttp:\/\/example.com\u57df\u4e0b\u7684\u4e3b\u673a\u524d\u9762\u90fd\u6709\u4e2aHosts,\u7136\u540e\u7ec4\u4e2d\u6709\u7ec4\u7684\u8bdd\uff0c\u8bb0\u5f97\u5199children<\/p>\n<p>ini\u683c\u5f0f\u7684\u683c\u5f0f\u5c31\u6bd4\u8f83\u7b80\u5355\uff0c\u76f4\u63a5\u4e00\u4e2a\u65b9\u62ec\u53f7\uff0c\u91cc\u9762\u5199\u7ec4\u540d\u5373\u53ef\uff0c\u7ec4\u4e0b\u9762\u5982\u679c\u6709\u7ec4\u7684\u8bdd\uff0c\u5c31\u5f97\u8fd9\u6837\u5199[\u7ec4\u7684\u7ec4\u540d\uff1a\u7ec4\u540d]\uff0c\u6bd4\u8f83\u7ed5\u53e3<\/p>\n<h2>\u57fa\u4e8e\u5bc6\u7801\u8fde\u63a5<\/h2>\n<h4>\u65b9\u5f0f\u4e00<\/h4>\n<p>[root@ansible ~]$ tail -3 \/etc\/ansible\/hosts<br \/>\n172.16.1.45 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=&#8217;1&#8242;<br \/>\n172.16.1.145 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=&#8217;1&#8242;<\/p>\n<h4>\u6d4b\u8bd5<\/h4>\n<p>[root@ansible ~]$ ansible web -a &#8216;hostname&#8217;<br \/>\n172.16.1.45 | CHANGED | rc=0 &gt;&gt;<br \/>\nweb01<br \/>\n172.16.1.145 | CHANGED | rc=0 &gt;&gt;<br \/>\nweb02<\/p>\n<h4>\u65b9\u5f0f\u4e8c<\/h4>\n<p>[root@ansible ~]$ tail -3 \/etc\/ansible\/hosts<\/p>\n<p>[webserver]<br \/>\nweb[1:2].linux.com ansible_ssh_pass=&#8217;1&#8242;<\/p>\n<h4>\u6d4b\u8bd5<\/h4>\n<p>[root@ansible ~]$ ansible webserver -a &#8216;hostname&#8217;<br \/>\nweb2.linux.com | CHANGED | rc=0 &gt;&gt;<br \/>\nweb02<br \/>\nweb1.linux.com | CHANGED | rc=0 &gt;&gt;<br \/>\nweb01<\/p>\n<h4>\u65b9\u5f0f\u4e09<\/h4>\n<p>[webservers]<br \/>\nweb[1:2].linux.com<br \/>\n[webservers:vars] \u00a0 \u00a0#\u7ed9webservers \u4e3b\u673a\u7ec4 \u8bbe\u7f6e\u5171\u7528 \u53d8\u91cf<br \/>\nansible_ssh_pass=&#8217;1&#8242;<\/p>\n<h2>\u57fa\u4e8e\u5bc6\u94a5\u8fde\u63a5\uff0c\u9700\u8981\u5148\u521b\u5efa\u516c\u94a5\u548c\u79c1\u94a5\uff0c\u5e76\u4e0b\u53d1\u516c\u94a5\u81f3\u88ab\u63a7\u7aef<\/h2>\n<h4>\u521b\u5efa\u5bc6\u94a5<\/h4>\n<p>[root@ansible ~]$ ssh-keygen<\/p>\n<h4>\u53d1\u9001\u5bc6\u94a5<\/h4>\n<p>[root@ansible ~]$ ssh-copy-id -i \/root\/.ssh\/id_rsa.pub 172.16.1.45<\/p>\n<p>[root@ansible ~]$ ssh-copy-id -i \/root\/.ssh\/id_rsa.pub 172.16.1.145<\/p>\n<p>[root@ansible ~]$ ssh-copy-id -i \/root\/.ssh\/id_rsa.pub 172.16.1.163<\/p>\n<h4>\u914d\u7f6e\u6587\u4ef6<\/h4>\n<p>[root@ansible ~]$ cat hosts<br \/>\n[web]<br \/>\n172.16.1.45<br \/>\n172.16.1.145<br \/>\n[lb]<br \/>\n172.16.1.163<\/p>\n<p>[root@ansible ~]$ ansible all -i hosts -a &#8216;hostname&#8217;<br \/>\n172.16.1.145 | CHANGED | rc=0 &gt;&gt;<br \/>\nweb02<br \/>\n172.16.1.45 | CHANGED | rc=0 &gt;&gt;<br \/>\nweb01<br \/>\n172.16.1.163 | CHANGED | rc=0 &gt;&gt;<br \/>\nlb01<\/p>\n<h4>\u4e3b\u673a\u7ec4\u4f7f\u7528<\/h4>\n<p>[root@ansible ~]$ cat hosts<br \/>\n[web]<br \/>\n172.16.1.45<br \/>\n172.16.1.145<br \/>\n[lb]<br \/>\n172.16.1.163<br \/>\n[server:children]<br \/>\nlb<br \/>\nweb<\/p>\n<p>[root@ansible ~]$ ansible server -i hosts -a &#8216;hostname&#8217;<br \/>\n172.16.1.145 | CHANGED | rc=0 &gt;&gt;<br \/>\nweb02<br \/>\n172.16.1.163 | CHANGED | rc=0 &gt;&gt;<br \/>\nlb01<br \/>\n172.16.1.45 | CHANGED | rc=0 &gt;&gt;<br \/>\nweb01<\/p>\n<h4>\u5217\u51fa\u6bcf\u4e2a\u4e3b\u673a\u7ec4\u4e0b\u9762\u7684\u4e3b\u673a\u60c5\u51b5<\/h4>\n<p>[root@ansible ~]$ ansible all -i hosts &#8211;list-hosts<br \/>\nhosts (3):<br \/>\n172.16.1.163<br \/>\n172.16.1.45<br \/>\n172.16.1.145<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.Ansible\u4ecb\u7ecd ansible\u662f\u65b0\u51fa\u73b0\u7684\u81ea\u52a8\u5316\u8fd0\u7ef4\u5de5\u5177\uff0c\u57fa\u4e8ePython\u5f00\u53d1\uff0c\u96c6\u5408\u4e86\u4f17\u591a\u8fd0\u7ef4\u5de5\u5177\uff08pu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[24],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/posts\/1145"}],"collection":[{"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1145"}],"version-history":[{"count":8,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/posts\/1145\/revisions"}],"predecessor-version":[{"id":1207,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/posts\/1145\/revisions\/1207"}],"wp:attachment":[{"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}