{"id":124434,"date":"2020-06-09T00:34:44","date_gmt":"2020-06-08T16:34:44","guid":{"rendered":"http:\/\/4563.org\/?p=124434"},"modified":"2020-06-09T00:34:44","modified_gmt":"2020-06-08T16:34:44","slug":"mitogen-for-ansible-%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%e6%8f%92%e4%bb%b6%e6%8e%a8%e8%8d%90%ef%bc%8c%e6%9c%89%e6%95%88%e6%8f%90%e5%8d%87-1-25x-7x-%e5%80%8d%e6%89%a7%e8%a1%8c%e6%95%88%e7%8e%87","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=124434","title":{"rendered":"Mitogen for Ansible \u6027\u80fd\u4f18\u5316\u63d2\u4ef6\u63a8\u8350\uff0c\u6709\u6548\u63d0\u5347 1.25x &#8211; 7x \u500d\u6267\u884c\u6548\u7387"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Mitogen for Ansible \u6027\u80fd\u4f18\u5316\u63d2\u4ef6\u63a8\u8350\uff0c\u6709\u6548\u63d0\u5347 1.25x &#8211; 7x \u500d\u6267\u884c\u6548\u7387               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wsgzao <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/06\/20200627_5ef7b905b8010.png\" alt=\"Mitogen for Ansible \u6027\u80fd\u4f18\u5316\u63d2\u4ef6\u63a8\u8350\uff0c\u6709\u6548\u63d0\u5347 1.25x - 7x \u500d\u6267\u884c\u6548\u7387\" \/><\/p>\n<p>\u4f17\u6240\u5468\u77e5\uff0cAnsible \u662f\u57fa\u4e8e ssh(\u5f53\u7136\u8fd8\u6709 telnet\uff0cwinrm \u7b49\u8fde\u63a5\u63d2\u4ef6)\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7ba1\u7406\u5de5\u5177\uff0c\u5176\u7b80\u5355\u6613\u7528\uff0c\u65e0 agent \u5f0f\u7684\u5de5\u4f5c\u65b9\u5f0f\u5728\u5f88\u591a\u573a\u666f\u4e2d\u90fd\u6709\u4e0d\u5c11\u4f18\u52bf\uff0c\u4e0d\u8fc7\u4e5f\u662f\u7531\u4e8e\u8fd9\u79cd\u5de5\u4f5c\u65b9\u5f0f\u5bfc\u81f4\u4e86\u5b83\u6ca1\u6709\u5176\u4ed6 c\/s \u7c7b\u7684\u5de5\u5177\u6267\u884c\u6548\u7387\u9ad8\uff0c\u9971\u53d7\u5176\u4ed6 C\/S \u7c7b\u5de5\u5177\u4f7f\u7528\u8005\u7684\u8ba5\u8bbd\uff0c\u5bf9\u6b64\uff0cAnsible \u5b98\u65b9\u4e5f\u5bf9 Ansible \u7684\u901f\u5ea6\u6548\u7387\u505a\u4e86\u4e0d\u5c11\u4f18\u5316\u624b\u6bb5\uff1a<\/p>\n<p>| \u53c2\u6570\u540d \/\u4f18\u5316\u7c7b\u522b | \u8bf4\u660e |<\/p>\n<p>&#8212;|&#8212;<\/p>\n<p>| fact cache | \u5c06 facts \u4fe1\u606f\u7b2c\u4e00\u6b21\u6536\u96c6\u540e\u7f13\u5b58\u5230<code>memory<\/code>\u6216\u8005<code>redis<\/code>\u6216\u8005\u6587\u4ef6\u4e2d\u3002 |<\/p>\n<p>| gather_subset | \u53ef\u9009\u62e9\u6027\u7684\u6536\u96c6<code>network<\/code>,<code>hardware<\/code>\u7b49\u4fe1\u606f\uff0c\u800c\u4e0d\u662f\u5168\u90e8 |<\/p>\n<p>| control_path | \u5f00\u542f<code>ssh socket<\/code>\u6301\u4e45\u5316\uff0c\u590d\u7528 ssh \u8fde\u63a5 |<\/p>\n<p>| pipelinling | \u5f00\u542f<code>ssh pipelining<\/code>,\u5ba2\u6237\u7aef\u4ece\u7ba1\u9053\u4e2d\u8bfb\u53d6\u6267\u884c\u6e32\u67d3\u540e\u7684\u811a\u672c\uff0c\u800c\u4e0d\u662f\u5728\u5ba2\u6237\u7aef\u521b\u5efa\u4e34\u65f6\u6587\u4ef6 |<\/p>\n<p>| fork | \u63d0\u9ad8\u5e76\u884c\u6267\u884c\u4e3b\u673a\u7684\u6570\u91cf |<\/p>\n<p>| serial | \u5c06<code>play_hosts``\u2460<\/code>\u4e2d\u4e3b\u673a\u518d\u5206\u6279\u6267\u884c |<\/p>\n<p>| strategy | \u9ed8\u8ba4<code>linear<\/code>,\u6bcf\u4e2a\u4e3b\u673a\u7684\u5355\u4e2a task \u6267\u884c\u5b8c\u6210\u4f1a\u7b49\u5f85\u5176\u4ed6\u90fd\u5b8c\u6210\u540e\u518d\u6267\u884c\u4e0b\u4e2a\u4efb\u52a1\uff0c\u8bbe\u7f6e<code>free<\/code>\u53ef\u4e0d\u7b49\u5f85\u5176\u4ed6\u4e3b\u673a\uff0c\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c(\u770b\u8d77\u6765\u4f1a\u6bd4\u8f83\u4e71)\uff0c\u8fd8\u6709\u4e00\u4e2a\u9009\u9879<code>host_pinned<\/code>\uff0c\u6211\u4e5f\u4e0d\u77e5\u9053\u5e72\u561b\u7684 |<\/p>\n<h2>Overview<\/h2>\n<p>\u65e0\u610f\u53d1\u73b0\u4e86\u4e00\u4e2a Mitogen \u7684 Ansible plugin \uff08 strategy plugin \uff09\uff0c\u5f53\u524d\u5df2\u8fed\u4ee3\u5230 0.29 \u7248\u672c\uff0c\u770b\u4ecb\u7ecd\u8bf4\u80fd\u63d0\u5347 1.2x ~ 7x \u4ee5\u4e0a\u7684\u6267\u884c\u6548\u7387\uff0c\u7740\u5b9e\u60ca\u4eba\uff01<\/p>\n<p>\u5b83\u901a\u8fc7\u9ad8\u6548\u7684\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\u6765\u53d6\u4ee3 ansible \u9ed8\u8ba4\u7684\u5d4c\u5165\u5f0f\u4e0e\u7eaf python shell \u8c03\u7528\uff0c\u5b83\u4e0d\u4f1a\u4f18\u5316\u6a21\u5757\u672c\u8eab\u7684\u6267\u884c\u6548\u7387\uff0c\u53ea\u4f1a\u5c3d\u53ef\u80fd\u5feb\u7684\u2461\u53bb\u6267\u884c\u6a21\u5757\u83b7\u53d6\u8fd4\u56de(\u6267\u884c\u6a21\u5757\u524d\u4e5f\u662f\u6709\u4e00\u7cfb\u5217\u8fde\u63a5\uff0c\u53d1\u9001\u6570\u636e\uff0c\u4f20\u8f93\u6e32\u67d3\u811a\u672c\u7b49\u64cd\u4f5c\u7684)\u6765\u63d0\u9ad8\u6574\u4f53\u7684\u6548\u7387\uff0c\u7279\u6027\u5982\u4e0b<\/p>\n<p><strong>Expect a 1.25x &#8211; 7x speedup<\/strong> and a <strong>CPU usage reduction of at least 2x<\/strong>, depending on network conditions, modules executed, and time already spent by targets on useful work. Mitogen cannot improve a module once it is executing, it can only ensure the module executes as quickly as possible.<\/p>\n<ul>\n<li>\n<p><strong>One connection is used per target<\/strong>, in addition to one sudo invocation per user account. This is much better than SSH multiplexing combined with pipelining, as significant state can be maintained in RAM between steps, and system logs aren\u2019t spammed with repeat authentication events.<\/p>\n<\/li>\n<li>\n<p><strong>A single network roundtrip is used<\/strong> to execute a step whose code already exists in RAM on the target. Eliminating multiplexed SSH channel creation saves 4 ms runtime per 1 ms of network latency for every playbook step.<\/p>\n<\/li>\n<li>\n<p><strong>Processes are aggressively reused<\/strong>, avoiding the cost of invoking Python and recompiling imports, saving 300-800 ms for every playbook step.<\/p>\n<\/li>\n<li>\n<p>Code is ephemerally cached in RAM, <strong>reducing bandwidth usage by an order of magnitude<\/strong> compared to SSH pipelining, with around 5x fewer frames traversing the network in a typical run.<\/p>\n<\/li>\n<li>\n<p><strong>Fewer writes to the target filesystem occur<\/strong>. In typical configurations, Ansible repeatedly rewrites and extracts ZIP files to multiple temporary directories on the target. Security issues relating to temporary files in cross-account scenarios are entirely avoided.<\/p>\n<\/li>\n<\/ul>\n<p>The effect is most potent on playbooks that execute many <strong>short-lived actions<\/strong>, where Ansible\u2019s overhead dominates the cost of the operation, for example when executing large <code>with_items<\/code> loops to run simple commands or write files.<\/p>\n<p>\u5927\u4f53\u5c31\u662f\u6267\u884c\u8fc7\u7a0b\u4e2d\u4e3b\u673a\u4f7f\u7528\u4e00\u4e2a\u8fde\u63a5(\u9ed8\u8ba4\u6bcf\u6267\u884c\u4e00\u4e2a<code>task<\/code>\u6216\u8005<code>loop<\/code>\u5faa\u73af\u90fd\u4f1a\u91cd\u65b0\u6253\u5f00\u4e00\u6b21\u8fde\u63a5\u7684)\uff1b\u6e32\u67d3\u7684\u6267\u884c\u4ee3\u7801\u6682\u5b58\u4e8e\u5185\u5b58\u4e2d\uff1b\u51cf\u5c11\u591a\u8def\u590d\u7528<code>ssh<\/code>\u96a7\u9053\u7684\u65f6\u95f4\u6d88\u8017\uff1b\u51cf\u5c11\u4e34\u65f6\u6587\u4ef6\u4f20\u8f93\u7684\u5e26\u5bbd\uff1b\u4ee3\u7801\u91cd\u7528\uff0c\u907f\u514d\u4ee3\u7801\u7684\u91cd\u65b0\u7f16\u8bd1\u6210\u672c\u7b49<\/p>\n<p>\u5b9e\u73b0\u539f\u7406\u7684\u8bdd\uff0c\u53ef\u4ee5\u53bb\u770b\u4e0b\u5b98\u7f51\u89e3\u91ca<\/p>\n<p><em>\u2460 .\u00a0<code>play_hosts<\/code>\u4e3a\u5185\u7f6e\u53c2\u6570\uff0c\u6307\u5f53\u524d\u6b63\u5728\u6267\u884c\u7684 playbook \u4e2d\u7684\u4e3b\u673a\u5217\u8868<\/em><\/p>\n<p><em>\u2461.\u00a0<code>\u5c3d\u53ef\u80fd\u5feb\u7684<\/code> \u6307\u5230\u8fd0\u884c\u6a21\u5757\u524d\u7684\u9636\u6bb5<\/em><\/p>\n<h2>Installation<\/h2>\n<ol>\n<li>\n<p>Download and extract mitogen-0.2.9.tar.gz.<\/p>\n<\/li>\n<li>\n<p>Modify <code>ansible.cfg<\/code><\/p>\n<\/li>\n<\/ol>\n<pre><code>[defaults] strategy_plugins = \/path\/to\/mitogen-0.2.9\/ansible_mitogen\/plugins\/strategy strategy = mitogen_linear <\/code><\/pre>\n<p>The <code>strategy<\/code> key is optional. If omitted, the <code>ANSIBLE_STRATEGY=mitogen_linear<\/code> environment variable can be set on a per-run basis. Like <code>mitogen_linear<\/code>, the <code>mitogen_free<\/code> and <code>mitogen_host_pinned<\/code> strategies exists to mimic the <code>free<\/code> and <code>host_pinned<\/code> strategies.<\/p>\n<h2>Reference<\/h2>\n<p>https:\/\/networkgenomics.com\/ansible\/<\/p>\n<p>https:\/\/mitogen.networkgenomics.com\/ansible_detailed.html#overview<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Mitogen for Ansib&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/124434"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=124434"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/124434\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=124434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=124434"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=124434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}