{"id":5294,"date":"2018-09-06T06:51:08","date_gmt":"2018-09-06T06:51:08","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=5294"},"modified":"2021-11-17T10:00:24","modified_gmt":"2021-11-17T10:00:24","slug":"run-ansible-adhoc-commands-or-playbook-in-local-mode","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/run-ansible-adhoc-commands-or-playbook-in-local-mode\/","title":{"rendered":"Run Ansible Adhoc commands or playbook in Local mode"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5315\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/09\/ansible-1.png\" alt=\"\" width=\"500\" height=\"315\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/09\/ansible-1.png 500w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/09\/ansible-1-300x189.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>If you are trying to run Ansible Adhoc commands or playbook in Local mode, these articles will help you.<\/p>\n<p>Ansible can&#8217;t run playbooks locally without ssh if ansible_connection=local is defined in the hosts file, although it can run playbooks locally without ssh with connection: local in the playbook or with flag &#8211;connection=local.<\/p>\n<p><strong># # # # # # # # # # # # Method 1 # # # # # # # # # # # # # # # # # #<\/strong><\/p>\n<p>[code]ansible 127.0.0.1 -m file -a &#8220;dest=\/opt\/a.txt mode=600 state=touch&#8221;<br>\nansible 127.0.0.1 -m file -a &#8220;dest=\/opt\/a.txt mode=600 state=file&#8221;[\/code]<\/p>\n<p><strong># # # # # # # # # # # # Method 2 # # # # # # # # # # # # # # # # # #<\/strong><\/p>\n<p>[code]Inventory file<br>\n127.0.0.1 ansible_connection=local<br>\nlocalhost ansible_connection=local[\/code]<\/p>\n<p><strong># # # # # # # # # # # # Method 3 # # # # # # # # # # # # # # # # # #<\/strong><\/p>\n<p>[code]&#8212;<\/p>\n<p>&#8211; name: run the playbook tasks on the localhost<br>\nhosts: 127.0.0.1<br>\nconnection: local<br>\nbecome: yes<br>\ntasks:<\/p>\n<p>&#8211; name: print out the hostname of target<br>\ncommand: hostname<\/p>\n<p>&#8211; name: ensure aptitude is installed<br>\ncommand: apt-get -y install aptitude<\/p>\n<p>&#8211; name: update the apt package index i.e. apt-get update<br>\napt: update_cache=yes<\/p>\n<p>&#8211; name: upgrade system packages i.e. apt-get upgrade<br>\napt: upgrade=yes<\/p>\n<p>[\/code]<\/p>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_53485\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_22976\"  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_53485\"  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><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>If you are trying to run Ansible Adhoc commands or playbook in Local mode, these articles will help you. Ansible can&#8217;t run playbooks locally without ssh if ansible_connection=local is defined in the hosts file, although it can run playbooks locally without ssh with connection: local in the playbook or with flag &#8211;connection=local. # # #&#8230;<\/p>\n","protected":false},"author":1,"featured_media":5315,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5038],"tags":[5039,639,5041,4921,5040],"class_list":["post-5294","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible","tag-adhoc","tag-ansible","tag-local-mode","tag-localhost","tag-playbook"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5294","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=5294"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5294\/revisions"}],"predecessor-version":[{"id":25581,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5294\/revisions\/25581"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/5315"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=5294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=5294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=5294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}