{"id":21038,"date":"2021-04-19T23:11:46","date_gmt":"2021-04-19T23:11:46","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=21038"},"modified":"2021-10-28T06:42:17","modified_gmt":"2021-10-28T06:42:17","slug":"what-is-ansible-pull-and-how-can-we-use-it","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/what-is-ansible-pull-and-how-can-we-use-it\/","title":{"rendered":"What is ansible pull and how can we use it?"},"content":{"rendered":"\n<p>ansible-pull pulls playbooks from a VCS repo(git) and executes them for the local host.<\/p>\n\n\n\n<p>The ansible-pull command is a special feature of Ansible that allows you to, all in one go, pull a playbook from a Git repository (for example, GitHub) and then execute it, hence saving the usual steps such as cloning (or updating the working copy of) the repository, then executing the playbook. The great thing about ansible-pull is that it allows you to centrally store and version control your playbooks and then execute them with a single command, hence enabling them to be executed using the cron scheduler without the need to even install the Ansible playbooks on a given box.<\/p>\n\n\n\n<p>ansible-pull is used to up a remote copy of ansible on each managed node, each set to run via cron and update playbook source via a source repository. This inverts the default push architecture of ansible into a pull architecture, which has near-limitless scaling potential.<\/p>\n\n\n\n<p>The setup playbook can be tuned to change the cron frequency, logging locations, and parameters to ansible-pull. This is useful both for extreme scale-out as well as periodic remediation. Usage of the \u2018fetch\u2019 module to retrieve logs from ansible-pull runs would be an excellent way to gather and analyze remote logs from ansible-pull.<\/p>\n\n\n\n<p>The Cronjob<br>This is the easy part, all you will be doing is adding the following as a cronjob to your system.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-number\">0<\/span> <span class=\"hljs-number\">3<\/span> * * * <span class=\"hljs-regexp\">\/usr\/<\/span>local\/bin\/ansible-pull -U https:<span class=\"hljs-comment\">\/\/github.com\/scmgalaxy\/ansible-pull-example -i hosts<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\"><li>If you\u2019re new to cronjobs, all you need to know is this job is being run every day at 3am.<\/li><li>The next part of the command is simply the full path to the ansible-pull command, in this instance, we are using the -U option to make sure our code is updated, if it is already installed.<\/li><li>Then finally, the command also includes a hosts file that our playbook will run against.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Manual Use of ansible-pull<\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">To run the ansible-pull manually <span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">a<\/span> <span class=\"hljs-title\">command<\/span> <span class=\"hljs-title\">like<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">following<\/span>:\n\n<span class=\"hljs-title\">url<\/span>='<span class=\"hljs-title\">https<\/span>:\/\/<span class=\"hljs-title\">github<\/span>.<span class=\"hljs-title\">com<\/span>\/<span class=\"hljs-title\">jschulthess<\/span>\/<span class=\"hljs-title\">ansible<\/span>-<span class=\"hljs-title\">pull<\/span>-<span class=\"hljs-title\">update<\/span>.<span class=\"hljs-title\">git<\/span>' # <span class=\"hljs-title\">URL<\/span> <span class=\"hljs-title\">of<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">playbook<\/span> <span class=\"hljs-title\">repository<\/span>\n<span class=\"hljs-title\">checkout<\/span>='<span class=\"hljs-title\">develop<\/span>'                                            # <span class=\"hljs-title\">branch<\/span>\/<span class=\"hljs-title\">tag<\/span>\/<span class=\"hljs-title\">commit<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">checkout<\/span>\n<span class=\"hljs-title\">directory<\/span>='\/<span class=\"hljs-title\">var<\/span>\/<span class=\"hljs-title\">projects<\/span>\/<span class=\"hljs-title\">ansible<\/span>-<span class=\"hljs-title\">pull<\/span>-<span class=\"hljs-title\">update<\/span>'           # <span class=\"hljs-title\">directory<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">checkout<\/span> <span class=\"hljs-title\">repository<\/span> <span class=\"hljs-title\">to<\/span>\n<span class=\"hljs-title\">logfile<\/span>='\/<span class=\"hljs-title\">var<\/span>\/<span class=\"hljs-title\">log<\/span>\/<span class=\"hljs-title\">ansible<\/span>-<span class=\"hljs-title\">pull<\/span>-<span class=\"hljs-title\">update<\/span>.<span class=\"hljs-title\">log<\/span>'                            # <span class=\"hljs-title\">where<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">put<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">logs<\/span>\n\n<span class=\"hljs-title\">sudo<\/span> <span class=\"hljs-title\">ansible<\/span>-<span class=\"hljs-title\">pull<\/span> -<span class=\"hljs-title\">o<\/span> -<span class=\"hljs-title\">C<\/span> ${<span class=\"hljs-title\">checkout<\/span>} -<span class=\"hljs-title\">d<\/span> ${<span class=\"hljs-title\">directory<\/span>} -<span class=\"hljs-title\">i<\/span> ${<span class=\"hljs-title\">directory<\/span>}\/<span class=\"hljs-title\">inventory<\/span> -<span class=\"hljs-title\">U<\/span> ${<span class=\"hljs-title\">url<\/span>} \\\n  2&gt;&amp;1 | <span class=\"hljs-title\">sudo<\/span> <span class=\"hljs-title\">tee<\/span> -<span class=\"hljs-title\">a<\/span> ${<span class=\"hljs-title\">logfile<\/span>}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Example Code<\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/53f56c9eb7bb08d7200819e335e8661a.js\"><\/script>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_75464\"><iframe loading=\"lazy\"  id=\"_ytid_69107\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_75464\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>ansible-pull pulls playbooks from a VCS repo(git) and executes them for the local host. The ansible-pull command is a special feature of Ansible that allows you to, all in one&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-21038","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/21038","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=21038"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/21038\/revisions"}],"predecessor-version":[{"id":24431,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/21038\/revisions\/24431"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=21038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=21038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=21038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}