{"id":962532,"date":"2024-12-27T04:09:06","date_gmt":"2024-12-26T20:09:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/962532.html"},"modified":"2024-12-27T04:09:08","modified_gmt":"2024-12-26T20:09:08","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5ansible%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/962532.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165ansible\u6a21\u5757"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104457\/45b5d73a-52c0-4a26-bad1-888826807748.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165ansible\u6a21\u5757\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5bfc\u5165Ansible\u6a21\u5757\u7684\u6b65\u9aa4\u4e3b\u8981\u5305\u62ec\uff1a\u5b89\u88c5Ansible\u3001\u4f7f\u7528<code>ansible-runner<\/code>\u5e93\u3001\u914d\u7f6eAnsible\u73af\u5883\u3002<\/strong>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fddAnsible\u5df2\u88ab\u6b63\u786e\u5b89\u88c5\uff0c\u5e76\u4e14\u76f8\u5173\u7684Python\u5e93\uff0c\u5982<code>ansible-runner<\/code>\u4e5f\u5df2\u5b89\u88c5\u3002\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7Python\u811a\u672c\u8c03\u7528Ansible\u6a21\u5757\uff0c\u901a\u5e38\u901a\u8fc7<code>ansible-runner<\/code>\u6765\u6267\u884cAnsible\u4efb\u52a1\u3002\u8be6\u7ec6\u6765\u8bf4\uff0c\u4ee5\u4e0b\u662f\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u6b65\u9aa4\u7684\u5177\u4f53\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5Ansible\u548c\u76f8\u5173Python\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5bfc\u5165Ansible\u6a21\u5757\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86Ansible\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Ansible\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get update<\/p>\n<p>sudo apt-get install ansible<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u4f7f\u7528<code>pip<\/code>\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install ansible<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u4f60\u9700\u8981\u5b89\u88c5<code>ansible-runner<\/code>\u5e93\uff0c\u8fd9\u662f\u4e00\u4e2a\u7528\u4e8e\u5728Python\u4e2d\u8fd0\u884cAnsible\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install ansible-runner<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u786e\u4fdd\u5b89\u88c5\u7684\u7248\u672c\u4e0ePython\u517c\u5bb9\uff0c\u4ee5\u907f\u514d\u5728\u8fd0\u884c\u65f6\u9047\u5230\u95ee\u9898\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>ansible-runner<\/code>\u6267\u884cAnsible\u4efb\u52a1<\/p>\n<\/p>\n<p><p><code>ansible-runner<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u4ecePython\u4e2d\u8c03\u7528Ansible\u7684\u5de5\u5177\u3002\u5b83\u7b80\u5316\u4e86\u5728Python\u811a\u672c\u4e2d\u8fd0\u884cAnsible\u4efb\u52a1\u7684\u8fc7\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528<code>ansible-runner<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ansible_runner<\/p>\n<p>def run_ansible_playbook(playbook_path):<\/p>\n<p>    r = ansible_runner.run(private_data_dir=&#39;\/tmp&#39;, playbook=playbook_path)<\/p>\n<p>    print(&quot;{}: {}&quot;.format(r.status, r.rc))<\/p>\n<p>    for each_host_event in r.events:<\/p>\n<p>        print(each_host_event[&#39;event&#39;])<\/p>\n<p>run_ansible_playbook(&#39;\/path\/to\/your\/playbook.yml&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>ansible_runner.run()<\/code>\u65b9\u6cd5\u6765\u6267\u884cAnsible playbook\uff0c\u5e76\u8f93\u51fa\u6267\u884c\u7ed3\u679c\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u81ea\u52a8\u5316\u90e8\u7f72\u3001\u914d\u7f6e\u7ba1\u7406\u7b49\u4efb\u52a1\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u3001\u914d\u7f6eAnsible\u73af\u5883<\/p>\n<\/p>\n<p><p>\u5728\u8fd0\u884cAnsible\u4efb\u52a1\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fddAnsible\u73af\u5883\u5df2\u6b63\u786e\u914d\u7f6e\u3002\u8fd9\u5305\u62ec\u8bbe\u7f6e\u4e3b\u673a\u6e05\u5355\u3001Ansible\u914d\u7f6e\u6587\u4ef6\u4ee5\u53ca\u5fc5\u8981\u7684\u51ed\u636e\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e9b\u6587\u4ef6\u4f4d\u4e8e<code>\/etc\/ansible\/<\/code>\u76ee\u5f55\u4e0b\uff0c\u4f46\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u5b9a\u4e49\u8def\u5f84\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4e3b\u673a\u6e05\u5355<\/strong>\uff1a\u5b9a\u4e49Ansible\u8981\u7ba1\u7406\u7684\u76ee\u6807\u4e3b\u673a\uff0c\u53ef\u4ee5\u662fIP\u5730\u5740\u6216\u57df\u540d\u3002<\/li>\n<li><strong>Ansible\u914d\u7f6e\u6587\u4ef6<\/strong>\uff1a\u901a\u5e38\u662f<code>ansible.cfg<\/code>\uff0c\u7528\u4e8e\u914d\u7f6eAnsible\u7684\u884c\u4e3a\u3002<\/li>\n<li><strong>\u51ed\u636e<\/strong>\uff1a\u5982SSH\u5bc6\u94a5\uff0c\u7528\u4e8e\u8ba4\u8bc1\u548c\u6388\u6743\u3002<\/li>\n<\/ul>\n<p><p>\u786e\u4fdd\u8fd9\u4e9b\u6587\u4ef6\u914d\u7f6e\u6b63\u786e\uff0c\u4ee5\u4fbfAnsible\u80fd\u591f\u987a\u5229\u5730\u6267\u884c\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5728Python\u4e2d\u5904\u7406Ansible\u7684\u8f93\u51fa<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8c03\u7528Ansible\u4efb\u52a1\u540e\uff0c\u5904\u7406\u8f93\u51fa\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6b65\u9aa4\u3002\u901a\u8fc7<code>ansible_runner<\/code>\uff0c\u4f60\u53ef\u4ee5\u6355\u83b7\u4efb\u52a1\u7684\u8be6\u7ec6\u8f93\u51fa\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u89e3\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">r = ansible_runner.run(private_data_dir=&#39;\/tmp&#39;, playbook=&#39;\/path\/to\/your\/playbook.yml&#39;)<\/p>\n<p>if r.status == &#39;successful&#39;:<\/p>\n<p>    print(&quot;Playbook executed successfully!&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Playbook execution f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u89e3\u6790\u8f93\u51fa\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u66f4\u597d\u5730\u4e86\u89e3\u4efb\u52a1\u6267\u884c\u60c5\u51b5\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u8c03\u8bd5\u548c\u4f18\u5316\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6269\u5c55Ansible\u7684\u529f\u80fd<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u4efb\u52a1\u6267\u884c\uff0cAnsible\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6a21\u5757\u548c\u63d2\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u8fdb\u884c\u6269\u5c55\u3002\u4f60\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u6a21\u5757\uff0c\u6216\u8005\u96c6\u6210\u7b2c\u4e09\u65b9\u5e93\uff0c\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u81ea\u52a8\u5316\u9700\u6c42\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u7f16\u5199\u81ea\u5b9a\u4e49\u6a21\u5757<\/strong>\uff1a\u901a\u8fc7Python\u7f16\u5199\u65b0\u7684Ansible\u6a21\u5757\uff0c\u4ee5\u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\u3002<\/li>\n<li><strong>\u96c6\u6210\u7b2c\u4e09\u65b9\u5e93<\/strong>\uff1a\u5229\u7528Python\u7684\u5f3a\u5927\u751f\u6001\u7cfb\u7edf\uff0c\u5c06\u5176\u4ed6\u5e93\u4e0eAnsible\u96c6\u6210\uff0c\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u81ea\u52a8\u5316\u5de5\u4f5c\u6d41\u3002<\/li>\n<\/ol>\n<p><p>\u603b\u4e4b\uff0c\u5728Python\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528Ansible\u6a21\u5757\u6d89\u53ca\u591a\u4e2a\u6b65\u9aa4\uff0c\u5305\u62ec\u5b89\u88c5\u3001\u914d\u7f6e\u548c\u6267\u884c\u4efb\u52a1\u3002\u901a\u8fc7\u7075\u6d3b\u4f7f\u7528Ansible\u548cPython\uff0c\u7528\u6237\u53ef\u4ee5\u5b9e\u73b0\u5f3a\u5927\u7684\u81ea\u52a8\u5316\u529f\u80fd\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5Ansible\u6a21\u5757\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4f7f\u7528Ansible\u6a21\u5757\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Ansible\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5Ansible\uff1a\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip install ansible<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165Ansible\u76f8\u5173\u6a21\u5757\u5e76\u5f00\u59cb\u4f7f\u7528\u4e86\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8c03\u7528Ansible\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8c03\u7528Ansible\u529f\u80fd\u901a\u5e38\u4f7f\u7528<code>ansible_runner<\/code>\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5<code>ansible-runner<\/code>\u6765\u5b9e\u73b0\uff0c\u547d\u4ee4\u4e3a<code>pip install ansible-runner<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165<code>ansible_runner<\/code>\u6a21\u5757\uff0c\u4f7f\u7528\u5176\u63d0\u4f9b\u7684API\u6765\u6267\u884cAnsible\u4efb\u52a1\uff0c\u4f8b\u5982\u8fd0\u884c\u5267\u672c\u3001\u6a21\u5757\u7b49\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528Ansible\u7684\u5267\u672c\uff1f<\/strong><br \/>\u786e\u5b9e\u53ef\u4ee5\u3002\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>ansible.playbook<\/code>\u6a21\u5757\u6765\u52a0\u8f7d\u548c\u6267\u884cAnsible\u5267\u672c\u3002\u4f7f\u7528<code>ansible.playbook.Playbook<\/code>\u7c7b\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5267\u672c\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7\u76f8\u5e94\u7684\u65b9\u6cd5\u8c03\u7528\u6267\u884c\u5267\u672c\uff0c\u4f20\u9012\u5fc5\u8981\u7684\u53c2\u6570\u548c\u53d8\u91cf\u3002\u8fd9\u4f7f\u5f97\u5728\u81ea\u52a8\u5316\u4efb\u52a1\u4e2d\u7ed3\u5408Python\u548cAnsible\u53d8\u5f97\u975e\u5e38\u7075\u6d3b\u548c\u5f3a\u5927\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5bfc\u5165Ansible\u6a21\u5757\u7684\u6b65\u9aa4\u4e3b\u8981\u5305\u62ec\uff1a\u5b89\u88c5Ansible\u3001\u4f7f\u7528ansible-runner\u5e93\u3001 [&hellip;]","protected":false},"author":3,"featured_media":962537,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962532"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=962532"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962532\/revisions"}],"predecessor-version":[{"id":962538,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962532\/revisions\/962538"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/962537"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=962532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=962532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=962532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}