{"id":1175,"date":"2021-04-13T07:47:24","date_gmt":"2021-04-13T05:47:24","guid":{"rendered":"https:\/\/www.netexpertise.eu\/en\/?p=1175"},"modified":"2021-08-24T19:32:04","modified_gmt":"2021-08-24T17:32:04","slug":"make-ansible-6x-faster-with-these-3-tips","status":"publish","type":"post","link":"http:\/\/www.netexpertise.eu\/en\/devops\/ansible\/make-ansible-6x-faster-with-these-3-tips.html","title":{"rendered":"Make Ansible 6X Faster with these 3 Tips"},"content":{"rendered":"\n<p><a href=\"\/en\/category\/devops\/ansible\">Ansible<\/a> is generally slow because it connects to the remote host for every task it runs. Let&#8217;s do some checks on a tiny role that gets the latest kubectl version (Kubernetes client) from a URL and installs the binary.<br>Let&#8217;s see 3 easy ways how to speed up Ansible and get better execution times and overall performance.<br><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"yaml\" class=\"language-yaml\">- name: get kubectl last version\n &nbsp; uri:\n &nbsp; &nbsp; url: \"{{kubectl_url}}\/stable.txt\"\n &nbsp; &nbsp; return_content: yes\n &nbsp; &nbsp; status_code: 200\n &nbsp; register: kubectl_latest_version\n \n\n - name: Download kubectl binary\n &nbsp; get_url:\n &nbsp; &nbsp; url: \"{{kubectl_url}}\/v{{ kubectl_version |\n       default(kubectl_latest_version.content | \n       regex_replace('^v', '')) }}\/bin\/{{kubectl_os}}\/{{kubectl_arch}}\/kubectl\"\n &nbsp; &nbsp; dest: \"\/usr\/local\/bin\"\n &nbsp; &nbsp; mode: '755'\n &nbsp; &nbsp; owner: \"{{ kubectl_owner }}\"\n &nbsp; &nbsp; group: \"{{ kubectl_group }}\"<\/code><\/pre>\n\n\n\n<p><br>Execution time with default settings: <span class=\"has-inline-color has-vivid-red-color\"><strong>32,2s<\/strong><\/span><br><br><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><a href=\"\/images\/Speedup_Ansible.png\"><img decoding=\"async\" loading=\"lazy\" src=\"\/images\/Speedup_Ansible.png\" alt=\"Ansible Speed up\" width=\"554\" height=\"341\"\/><\/a><figcaption>Speed up Ansible<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><br>Ansible Fact Cache<\/h2>\n\n\n\n<p>Gathering facts is the first task Ansible runs when connecting to a host and the least we can say, it is slow. Very slow. Performance is also an issue when writing a playbook that you are testing over and over.<br><br>Luckily, it can be tweaked to save some time by adding these lines in ansible.cfg:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\"># implement fact caching and a smaller subset of facts gathered \n# for improved performance\n\ngathering = smart\ngather_subset = !hardware,!facter,!ohai\n\nfact_caching_connection = \/tmp\/ansible_fact_cache\nfact_caching = jsonfile\n\n# expire the fact cache after 2 hours\nfact_caching_timeout = 7200<\/code><\/pre>\n\n\n\n<p><br>According to the <a href=\"https:\/\/github.com\/ansible\/ansible\/blob\/devel\/examples\/ansible.cfg\" target=\"_blank\" rel=\"noreferrer noopener\">ansible.cfg<\/a> example available online, smart gathering &#8220;gathers by default, but doesn&#8217;t regather if already gathered&#8221;.<br><br>Hardware facts are the longest facts to retrieve but you may need them especially if you build roles based on network interfaces. You may get &#8220;ansible_eth0 is undefined&#8221; for example.<br>Facter and ohai are related to Puppet and Chef clients.<br><br>And the most efficient is fact caching of course that stores information in a JSON file. But it could also fit in memory, or even in a shared Redis database.<\/p>\n\n\n\n<p>Facts can also be disabled within a playbook if you don&#8217;t need them for that specific playbook. That&#8217;s a potential significant speed up that you can&#8217;t use too often though, most playbooks need facts.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"yaml\" class=\"language-yaml\">- name: my_playbook\n    hosts: *\n    gather_facts: no\n    tasks:<\/code><\/pre>\n\n\n\n<p><br>Execution time: <span class=\"has-inline-color has-vivid-red-color\"><strong>19,2s<\/strong><\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><br>SSH Speedup with Pipelining<\/h2>\n\n\n\n<p>Enabling pipelining reduces the number of SSH operations required to execute a module on the remote server. This improves performance but &#8216;requiretty&#8217; must first be disabled in \/etc\/sudoers on all managed hosts, reason why pipelining is disabled by default. Add in ansible.cfg:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"vim\" class=\"language-vim\">[ssh_connection]\npipelining = True<\/code><\/pre>\n\n\n\n<p><br>If requiretty&nbsp;is set, sudo will only run when the user is logged in to a real tty.&nbsp; When this flag is set, sudo can only be run from a login session and not via other means such as cron or cgi-bin scripts.&nbsp;However, this flag is off by default according to the man page on Debian and Ubuntu at least.<br>It is safe to use pipelining is this case. Check your Linux distro sudo man page.<br><br>Execution time: <strong><span class=\"has-inline-color has-vivid-red-color\">11,6s<\/span><\/strong><br><br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Delegate_to localhost<\/h2>\n\n\n\n<p>Most improvements are dependant on the way you write Ansible tasks. In this role, you could connect to the URL from any host &#8211; localhost? &#8211; and spare an SSH connection.<br>This is the purpose of <em>delegate_to<\/em> that is usually set to localhost. The first task becomes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"yaml\" class=\"language-yaml\">- name: get kubectl last version\n &nbsp; delegate_to: localhost\n &nbsp; become: false\n &nbsp; uri:\n &nbsp; &nbsp; url: \"{{kubectl_url}}\/stable.txt\"\n &nbsp; &nbsp; return_content: yes\n &nbsp; &nbsp; status_code: 200\n &nbsp; register: kubectl_latest_version<\/code><\/pre>\n\n\n\n<p><br>This is a non neglectable optimisation that you can use anytime the task can be run anywhere.<br><br>You&#8217;d better set <em>become: false<\/em> or you might get this error message if Ansible tries to sudo as root on your local host.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">fatal: [backup]: FAILED! =&gt; {\"changed\": false, \"module_stderr\": \"sudo: a password is required\\n\", \"module_stdout\": \"\", \"msg\": \"MODULE FAILURE\\nSee stdout\/stderr for the exact error\", \"rc\": 1}<\/code><\/pre>\n\n\n\n<p><br>Execution time: <span class=\"has-inline-color has-vivid-red-color\"><strong>4,7s<\/strong><\/span><\/p>\n\n\n\n<p><br>Execution time is the mean time of 10 runs in a row, and tests were conducted through a VPN link that was far from good.<br><br>Of course, results are not linear, all playbooks will not run 6 times faster every time but you get the idea. Fact cache saves a few seconds at the start of the playbook while delegation to localhost is only applicable to a small bunch of cases.<br>There are other possible improvements to speed up Ansible such as <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/user_guide\/playbooks_async.html\" target=\"_blank\" rel=\"noreferrer noopener\">async<\/a> tasks to launch a task and move on immediately to the next one, but your best bet is to run Ansible on a server as close as possible to the target hosts, because people often experience slow SSH connections.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Speed up Ansible with 3 easy tweaks and more<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[427],"tags":[429],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Netexpertise - Make Ansible 6X Faster with these 3 Tips<\/title>\n<meta name=\"description\" content=\"Ansible is slow at times? Speed up Ansible with 3 easy tweaks and other performance optimisations to make it faster\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.netexpertise.eu\/en\/devops\/ansible\/make-ansible-6x-faster-with-these-3-tips.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Netexpertise - Make Ansible 6X Faster with these 3 Tips\" \/>\n<meta property=\"og:description\" content=\"Ansible is slow at times? Speed up Ansible with 3 easy tweaks and other performance optimisations to make it faster\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.netexpertise.eu\/en\/devops\/ansible\/make-ansible-6x-faster-with-these-3-tips.html\" \/>\n<meta property=\"og:site_name\" content=\"Netexpertise\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-13T05:47:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-24T17:32:04+00:00\" \/>\n<meta name=\"author\" content=\"dave\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@netexpertise\" \/>\n<meta name=\"twitter:site\" content=\"@netexpertise\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/devops\/ansible\/make-ansible-6x-faster-with-these-3-tips.html\",\"url\":\"https:\/\/www.netexpertise.eu\/en\/devops\/ansible\/make-ansible-6x-faster-with-these-3-tips.html\",\"name\":\"Netexpertise - Make Ansible 6X Faster with these 3 Tips\",\"isPartOf\":{\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#website\"},\"datePublished\":\"2021-04-13T05:47:24+00:00\",\"dateModified\":\"2021-08-24T17:32:04+00:00\",\"author\":{\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa\"},\"description\":\"Ansible is slow at times? Speed up Ansible with 3 easy tweaks and other performance optimisations to make it faster\",\"breadcrumb\":{\"@id\":\"https:\/\/www.netexpertise.eu\/en\/devops\/ansible\/make-ansible-6x-faster-with-these-3-tips.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.netexpertise.eu\/en\/devops\/ansible\/make-ansible-6x-faster-with-these-3-tips.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/devops\/ansible\/make-ansible-6x-faster-with-these-3-tips.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.netexpertise.eu\/en\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Make Ansible 6X Faster with these 3 Tips\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#website\",\"url\":\"https:\/\/www.netexpertise.eu\/en\/\",\"name\":\"Netexpertise\",\"description\":\"Systems \/ Networks \/ DevOps\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.netexpertise.eu\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa\",\"name\":\"dave\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g\",\"caption\":\"dave\"},\"sameAs\":[\"http:\/\/www.netexpertise.eu\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Netexpertise - Make Ansible 6X Faster with these 3 Tips","description":"Ansible is slow at times? Speed up Ansible with 3 easy tweaks and other performance optimisations to make it faster","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.netexpertise.eu\/en\/devops\/ansible\/make-ansible-6x-faster-with-these-3-tips.html","og_locale":"en_US","og_type":"article","og_title":"Netexpertise - Make Ansible 6X Faster with these 3 Tips","og_description":"Ansible is slow at times? Speed up Ansible with 3 easy tweaks and other performance optimisations to make it faster","og_url":"https:\/\/www.netexpertise.eu\/en\/devops\/ansible\/make-ansible-6x-faster-with-these-3-tips.html","og_site_name":"Netexpertise","article_published_time":"2021-04-13T05:47:24+00:00","article_modified_time":"2021-08-24T17:32:04+00:00","author":"dave","twitter_card":"summary_large_image","twitter_creator":"@netexpertise","twitter_site":"@netexpertise","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.netexpertise.eu\/en\/devops\/ansible\/make-ansible-6x-faster-with-these-3-tips.html","url":"https:\/\/www.netexpertise.eu\/en\/devops\/ansible\/make-ansible-6x-faster-with-these-3-tips.html","name":"Netexpertise - Make Ansible 6X Faster with these 3 Tips","isPartOf":{"@id":"https:\/\/www.netexpertise.eu\/en\/#website"},"datePublished":"2021-04-13T05:47:24+00:00","dateModified":"2021-08-24T17:32:04+00:00","author":{"@id":"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa"},"description":"Ansible is slow at times? Speed up Ansible with 3 easy tweaks and other performance optimisations to make it faster","breadcrumb":{"@id":"https:\/\/www.netexpertise.eu\/en\/devops\/ansible\/make-ansible-6x-faster-with-these-3-tips.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.netexpertise.eu\/en\/devops\/ansible\/make-ansible-6x-faster-with-these-3-tips.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.netexpertise.eu\/en\/devops\/ansible\/make-ansible-6x-faster-with-these-3-tips.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.netexpertise.eu\/en"},{"@type":"ListItem","position":2,"name":"Make Ansible 6X Faster with these 3 Tips"}]},{"@type":"WebSite","@id":"https:\/\/www.netexpertise.eu\/en\/#website","url":"https:\/\/www.netexpertise.eu\/en\/","name":"Netexpertise","description":"Systems \/ Networks \/ DevOps","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.netexpertise.eu\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa","name":"dave","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g","caption":"dave"},"sameAs":["http:\/\/www.netexpertise.eu"]}]}},"_links":{"self":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts\/1175"}],"collection":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/comments?post=1175"}],"version-history":[{"count":0,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts\/1175\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/media?parent=1175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/categories?post=1175"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/tags?post=1175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}