{"id":981921,"date":"2024-12-27T07:06:31","date_gmt":"2024-12-26T23:06:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/981921.html"},"modified":"2024-12-27T07:06:34","modified_gmt":"2024-12-26T23:06:34","slug":"python%e5%a6%82%e4%bd%95%e5%bc%95%e7%94%a8ansible%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/981921.html","title":{"rendered":"python\u5982\u4f55\u5f15\u7528ansible\u6a21\u5757"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210636\/ad996173-1011-40f3-ab5f-934c42de6161.webp\" alt=\"python\u5982\u4f55\u5f15\u7528ansible\u6a21\u5757\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5f15\u7528Ansible\u6a21\u5757\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5Ansible\u7684Python API\u3001\u4f7f\u7528Ansible Runner\u3001\u5bfc\u5165\u7279\u5b9a\u6a21\u5757\u548c\u6267\u884cAnsible Playbook\u6765\u5b9e\u73b0\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u800cAnsible\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684IT\u81ea\u52a8\u5316\u5de5\u5177\u3002\u7ed3\u5408Python\u548cAnsible\uff0c\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u81ea\u52a8\u5316\u4efb\u52a1\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u7684\u81ea\u52a8\u5316\u811a\u672c\u548c\u63d2\u4ef6\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u5f15\u7528Ansible\u6a21\u5757\uff0c\u5305\u62ec\u5404\u79cd\u65b9\u6cd5\u548c\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5Ansible\u7684Python API<\/p>\n<\/p>\n<p><p>\u8981\u5728Python\u4e2d\u4f7f\u7528Ansible\u6a21\u5757\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Ansible\u7684Python API\u3002\u8fd9\u4e2aAPI\u662fAnsible\u4e3a\u5f00\u53d1\u4eba\u5458\u63d0\u4f9b\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u4ee3\u7801\u6765\u8c03\u7528Ansible\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Ansible\u53ca\u5176\u4f9d\u8d56<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5728\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86Ansible\u53ca\u5176\u76f8\u5173\u7684\u4f9d\u8d56\u9879\u3002\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5Ansible\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install ansible<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fddAnsible\u7684\u6838\u5fc3\u529f\u80fd\u548c\u5e93\u5728\u4f60\u7684Python\u73af\u5883\u4e2d\u53ef\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5Python\u4f9d\u8d56<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86Ansible\u672c\u8eab\uff0c\u8fd8\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u5176\u4ed6\u7684Python\u5e93\u4ee5\u4fbf\u652f\u6301Ansible API\u7684\u4f7f\u7528\uff0c\u4f8b\u5982Jinja2\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install jinja2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Jinja2\u662f\u4e00\u4e2a\u6a21\u677f\u5f15\u64ce\uff0cAnsible\u901a\u5e38\u7528\u5b83\u6765\u6e32\u67d3\u914d\u7f6e\u6587\u4ef6\u548c\u5176\u4ed6\u6a21\u677f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528Ansible Runner<\/p>\n<\/p>\n<p><p>Ansible Runner\u662f\u4e00\u4e2a\u7528\u4e8e\u6267\u884cAnsible\u4efb\u52a1\u7684Python\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u5728Python\u7a0b\u5e8f\u4e2d\u5d4c\u5165Ansible\u4efb\u52a1\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Ansible Runner<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5Ansible Runner\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install ansible-runner<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Ansible Runner\u6267\u884c\u4efb\u52a1<\/strong><\/p>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u4f7f\u7528Ansible Runner\u6765\u6267\u884cAnsible Playbook\u6216\u547d\u4ee4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ansible_runner<\/p>\n<p>r = ansible_runner.run(private_data_dir=&#39;\/tmp\/demo&#39;, playbook=&#39;site.yml&#39;)<\/p>\n<p>print(&quot;{}: {}&quot;.format(r.status, r.rc))<\/p>\n<h2><strong>\u67e5\u770b\u8be6\u7ec6\u7684stdout\u65e5\u5fd7<\/strong><\/h2>\n<p>print(r.stdout.read())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>ansible_runner.run<\/code>\u65b9\u6cd5\u6267\u884c\u4e86\u4f4d\u4e8e<code>\/tmp\/demo<\/code>\u76ee\u5f55\u4e2d\u7684<code>site.yml<\/code> Playbook\uff0c\u5e76\u8f93\u51fa\u6267\u884c\u72b6\u6001\u548c\u8fd4\u56de\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5bfc\u5165\u7279\u5b9aAnsible\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u53ea\u9700\u8981\u4f7f\u7528Ansible\u7684\u67d0\u4e2a\u7279\u5b9a\u6a21\u5757\u3002Ansible\u7684Python\u6a21\u5757\u63d0\u4f9b\u4e86\u8fd9\u79cd\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u76f4\u63a5\u5bfc\u5165\u4f7f\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165Ansible\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7Ansible\u7684API\u76f4\u63a5\u5bfc\u5165\u5e76\u4f7f\u7528\u7279\u5b9a\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from ansible.module_utils.basic import AnsibleModule<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    module = AnsibleModule(<\/p>\n<p>        argument_spec=dict(<\/p>\n<p>            name=dict(type=&#39;str&#39;, required=True)<\/p>\n<p>        )<\/p>\n<p>    )<\/p>\n<p>    response = {&quot;message&quot;: f&quot;Hello, {module.params[&#39;name&#39;]}!&quot;}<\/p>\n<p>    module.exit_json(changed=False, meta=response)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684Ansible\u6a21\u5757\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u63a5\u6536\u8f93\u5165\u5e76\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6267\u884cAnsible\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u4f60\u7684\u6a21\u5757\u53ef\u4ee5\u6b63\u786e\u6267\u884c\uff0c\u5e76\u4e14\u53c2\u6570\u548c\u8f93\u51fa\u7b26\u5408Ansible\u6a21\u5757\u7684\u89c4\u8303\u3002\u53ef\u4ee5\u5728Ansible Playbook\u4e2d\u8c03\u7528\u8fd9\u4e2a\u6a21\u5757\uff0c\u6216\u901a\u8fc7\u547d\u4ee4\u884c\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u6267\u884cAnsible Playbook<\/p>\n<\/p>\n<p><p>\u901a\u8fc7Python\u811a\u672c\u6267\u884cAnsible Playbook\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06Ansible\u7684\u529f\u80fd\u96c6\u6210\u5230Python\u9879\u76ee\u4e2d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f16\u5199Ansible Playbook<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2aAnsible Playbook\u6587\u4ef6\uff0c\u4f8b\u5982<code>site.yml<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-yaml\">---<\/p>\n<p>- name: Ensure Apache is installed<\/p>\n<p>  hosts: webservers<\/p>\n<p>  tasks:<\/p>\n<p>    - name: Install Apache<\/p>\n<p>      yum:<\/p>\n<p>        name: httpd<\/p>\n<p>        state: present<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728Python\u4e2d\u6267\u884cPlaybook<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7Python\u811a\u672c\u8c03\u7528Ansible\u547d\u4ee4\u6765\u6267\u884c\u8fd9\u4e2aPlaybook\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def run_playbook(playbook_path):<\/p>\n<p>    process = subprocess.Popen([&#39;ansible-playbook&#39;, playbook_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<p>    stdout, stderr = process.communicate()<\/p>\n<p>    return stdout, stderr<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    out, err = run_playbook(&#39;site.yml&#39;)<\/p>\n<p>    print(&quot;Output:\\n&quot;, out.decode())<\/p>\n<p>    if err:<\/p>\n<p>        print(&quot;Errors:\\n&quot;, err.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528Python\u7684<code>subprocess<\/code>\u6a21\u5757\u6765\u8fd0\u884cAnsible Playbook\uff0c\u5e76\u6355\u83b7\u5176\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5f15\u7528Ansible\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06\u81ea\u52a8\u5316\u4efb\u52a1\u96c6\u6210\u5230Python\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5f3a\u5927\u65b9\u5f0f\u3002\u901a\u8fc7\u5b89\u88c5Ansible\u7684Python API\u3001\u4f7f\u7528Ansible Runner\u3001\u5bfc\u5165\u7279\u5b9a\u6a21\u5757\u548c\u6267\u884cAnsible Playbook\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u81ea\u52a8\u5316\u4efb\u52a1\u3002\u65e0\u8bba\u662f\u5f00\u53d1\u81ea\u5b9a\u4e49\u81ea\u52a8\u5316\u5de5\u5177\uff0c\u8fd8\u662f\u5b9e\u73b0\u590d\u6742\u7684IT\u57fa\u7840\u8bbe\u65bd\u7ba1\u7406\uff0cPython\u4e0eAnsible\u7684\u7ed3\u5408\u90fd\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528Ansible\u6a21\u5757\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8c03\u7528Ansible\u6a21\u5757\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Ansible\u5e93\u3002\u4f7f\u7528<code>pip install ansible<\/code>\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>ansible.module_utils<\/code>\u6765\u5bfc\u5165\u6240\u9700\u6a21\u5757\u3002\u786e\u4fdd\u5728\u8c03\u7528\u6a21\u5757\u4e4b\u524d\uff0c\u6b63\u786e\u914d\u7f6eAnsible\u7684\u73af\u5883\u548c\u4e3b\u673a\u6e05\u5355\u6587\u4ef6\uff0c\u4ee5\u4fbf\u6a21\u5757\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<p><strong>\u4f7f\u7528Ansible\u6a21\u5757\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Ansible\u6a21\u5757\u65f6\uff0c\u786e\u4fdd\u4f60\u6709\u76f8\u5e94\u7684\u6743\u9650\u8bbf\u95ee\u76ee\u6807\u4e3b\u673a\u3002\u8fd8\u9700\u68c0\u67e5Ansible\u7248\u672c\u662f\u5426\u4e0e\u6a21\u5757\u8981\u6c42\u517c\u5bb9\u3002\u6b64\u5916\uff0c\u5efa\u8bae\u5728\u6d4b\u8bd5\u73af\u5883\u4e2d\u8fdb\u884c\u5b9e\u9a8c\uff0c\u4ee5\u907f\u514d\u5bf9\u751f\u4ea7\u73af\u5883\u9020\u6210\u5f71\u54cd\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u811a\u672c\u4e2d\u5904\u7406Ansible\u6a21\u5757\u7684\u8fd4\u56de\u503c\uff1f<\/strong><br \/>Ansible\u6a21\u5757\u901a\u5e38\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\u5bf9\u8c61\uff0c\u5305\u542b\u6267\u884c\u7ed3\u679c\u548c\u72b6\u6001\u3002\u53ef\u4ee5\u901a\u8fc7\u89e3\u6790\u8fd4\u56de\u503c\u4e2d\u7684<code>changed<\/code>\u3001<code>failed<\/code>\u548c<code>msg<\/code>\u7b49\u5b57\u6bb5\u6765\u5224\u65ad\u6267\u884c\u60c5\u51b5\u3002\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>json<\/code>\u5e93\u6765\u5904\u7406\u8fd9\u4e9b\u8fd4\u56de\u503c\uff0c\u53ef\u4ee5\u8f7b\u677e\u63d0\u53d6\u6240\u9700\u4fe1\u606f\u5e76\u8fdb\u884c\u540e\u7eed\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5f15\u7528Ansible\u6a21\u5757\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5Ansible\u7684Python API\u3001\u4f7f\u7528Ansible R [&hellip;]","protected":false},"author":3,"featured_media":981927,"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\/981921"}],"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=981921"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981921\/revisions"}],"predecessor-version":[{"id":981928,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981921\/revisions\/981928"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/981927"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=981921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=981921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=981921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}