{"id":18140,"date":"2022-07-29T09:00:00","date_gmt":"2022-07-29T07:00:00","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/?p=18140"},"modified":"2022-08-05T08:32:31","modified_gmt":"2022-08-05T06:32:31","slug":"using-docker-containers-for-ansible-testing","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/","title":{"rendered":"Using Docker containers for Ansible testing"},"content":{"rendered":"\n<p>I&#8217;d like to share in this post a quick setup using Docker containers for Ansible testing.<\/p>\n\n\n\n<p>I wanted to create a small lab for my Ansible scripts testing. The natural option that came to my mind was to create some virtual machines with Virtualbox using Vagrant in order to automate that process. However as I&#8217;m using a Macbook with a chip Apple M1, Virtualbox is not supported on that hardware. I then decided to explore using containers instead of virtual machines.<\/p>\n\n\n\n<p>The first step is to look after an existing docker image that would already have Ansible and Openssh installed. There is no need to reinvente the wheel in the age of space travelling so my colleague Jean-Philippe Clapot (<a href=\"https:\/\/www.dbi-services.com\/en\/courses\/docker-and-kubernetes-essential-skills\/\" target=\"_blank\" rel=\"noreferrer noopener\">our Docker guru<\/a>!) quickly spotted the perfect image for my need: <a href=\"https:\/\/hub.docker.com\/r\/jcpowermac\/alpine-ansible-ssh\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/hub.docker.com\/r\/jcpowermac\/alpine-ansible-ssh<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The launch<\/h2>\n\n\n\n<p>As docker and Docker desktop are already installed on my laptop I just have to pull that image:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n% docker pull jcpowermac\/alpine-ansible-ssh\n<\/pre><\/div>\n\n\n<p>Then I can run some containers:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n% docker run --name=controller --platform linux\/amd64 -d jcpowermac\/alpine-ansible-ssh\n% docker run --name=target1 --platform linux\/amd64 -d jcpowermac\/alpine-ansible-ssh\n% docker run --name=target2 --platform linux\/amd64 -d jcpowermac\/alpine-ansible-ssh\n<\/pre><\/div>\n\n\n<p>The idea for now is to have one container named &#8220;controller&#8221; on which I&#8217;ll write and run my Ansible scripts. Then, two target containers named &#8220;target1&#8221; and &#8220;target2&#8221; will be the targeting hosts of my scripts. Using containers instead of Virtual Machine give me plenty of spare resources to run much more of those bad boys when doing more advanced Ansible testing. Note the parameter <code>--platform linux\/amd64 <\/code>which specify the platform to use on my Apple M1 chip. Without this parameter you get a warning but the container is properly created anyway.<\/p>\n\n\n\n<p>All three containers are now up and running:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n% docker ps\nCONTAINER ID   IMAGE                           COMMAND                  CREATED          STATUS          PORTS     NAMES\n786309da3987   jcpowermac\/alpine-ansible-ssh   &quot;\/bin\/ash -c &#039;\/usr\/s\u2026&quot;   5 seconds ago    Up 4 seconds              target2\nabe685b1f1ab   jcpowermac\/alpine-ansible-ssh   &quot;\/bin\/ash -c &#039;\/usr\/s\u2026&quot;   28 seconds ago   Up 27 seconds             target1\ne063d4e9267d   jcpowermac\/alpine-ansible-ssh   &quot;\/bin\/ash -c &#039;\/usr\/s\u2026&quot;   44 seconds ago   Up 43 seconds             controller\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">The network<\/h2>\n\n\n\n<p>As this setup is only for temporary tests, I didn&#8217;t create a dedicated network for those containers. They all use the default bridge network in the default range 172.17.0.0\/16.<\/p>\n\n\n\n<p>The first container &#8220;controller&#8221; get the first free IP Address in this range which is 172.17.0.2\/16 (172.17.0.1\/16 is taken by the bridge interface. Each target will get the next free IP Address. We can check the IP Addresses assigned to the containers with the one-liner below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n% for i in $(docker ps|awk &#039;{print $1}&#039;|tail -n +2); do docker exec $i ip a|grep 172.17;done\n    inet 172.17.0.4\/16 brd 172.17.255.255 scope global eth0\n    inet 172.17.0.3\/16 brd 172.17.255.255 scope global eth0\n    inet 172.17.0.2\/16 brd 172.17.255.255 scope global eth0\n<\/pre><\/div>\n\n\n<p>We can now connect to the controller container and check the ansible version installed in this image:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n% docker exec -it controller \/bin\/sh\n\/ # id\nuid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),11(floppy),20(dialout),26(tape),27(video)\n\n\/ # ansible --version\nansible 2.7.2\n  config file = None\n  configured module search path = &#x5B;u&#039;\/root\/.ansible\/plugins\/modules&#039;, u&#039;\/usr\/share\/ansible\/plugins\/modules&#039;]\n  ansible python module location = \/usr\/lib\/python2.7\/site-packages\/ansible\n  executable location = \/usr\/bin\/ansible\n  python version = 2.7.15 (default, Aug 16 2018, 14:17:09) &#x5B;GCC 6.4.0]\n<\/pre><\/div>\n\n\n<p>This Docker image comes with the user ansible already created so let&#8217;s use it:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\/ # su ansible\n~\/projects $ pwd\n\/home\/ansible\/projects\n~\/projects $ id\nuid=1000(ansible) gid=1000(ansible) groups=1000(ansible)\n~\/projects $\n<\/pre><\/div>\n\n\n<p>We can first check the connectivity:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n~\/projects $ ssh 172.17.0.3\nThe authenticity of host &#039;172.17.0.3 (172.17.0.3)&#039; can&#039;t be established.\nECDSA key fingerprint is SHA256:ntBTgrAxi9bUSIb47U31BFzD4rE5ktFnwRxztqXFICE.\nAre you sure you want to continue connecting (yes\/no)? yes\n\n~\/projects $ ssh 172.17.0.4\nThe authenticity of host &#039;172.17.0.4 (172.17.0.4)&#039; can&#039;t be established.\nECDSA key fingerprint is SHA256:ntBTgrAxi9bUSIb47U31BFzD4rE5ktFnwRxztqXFICE.\nAre you sure you want to continue connecting (yes\/no)? yes\n\n~\/projects $\n<\/pre><\/div>\n\n\n<p>From the controller I can ssh to target1 and target2, my setup is now completed and I can start playing with Ansible.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Ansible test<\/h2>\n\n\n\n<p>The first step is to create an inventory file in the home folder:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n~\/projects $ cat &lt;&lt;EOF &gt; inventory.txt\ntarget1 ansible_host=172.17.0.3\ntarget2 ansible_host=172.17.0.4\nEOF\n<\/pre><\/div>\n\n\n<p>We can then run a basic Ansible command against our both target:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n~\/projects $ ansible target* -m ping -i inventory.txt\ntarget2 | SUCCESS =&gt; {\n    &quot;changed&quot;: false,\n    &quot;ping&quot;: &quot;pong&quot;\n}\ntarget1 | SUCCESS =&gt; {\n    &quot;changed&quot;: false,\n    &quot;ping&quot;: &quot;pong&quot;\n}\n<\/pre><\/div>\n\n\n<p>All is working as expected, our test platform is now ready for more advanced Ansible testing.<\/p>\n\n\n\n<p>Learn more on Ansible with our Training course: <a href=\"https:\/\/www.dbi-services.com\/en\/courses\/ansible-basics\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.dbi-services.com\/en\/courses\/ansible-basics\/ <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;d like to share in this post a quick setup using Docker containers for Ansible testing. I wanted to create a small lab for my Ansible scripts testing. The natural option that came to my mind was to create some virtual machines with Virtualbox using Vagrant in order to automate that process. However as I&#8217;m [&hellip;]<\/p>\n","protected":false},"author":109,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1321,1320,1504],"tags":[2600,601],"type_dbi":[],"class_list":["post-18140","post","type-post","status-publish","format-standard","hentry","category-ansible","category-devops","category-docker","tag-ansible-2","tag-docker"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Using Docker containers for Ansible testing - dbi Blog<\/title>\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.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Docker containers for Ansible testing\" \/>\n<meta property=\"og:description\" content=\"I&#8217;d like to share in this post a quick setup using Docker containers for Ansible testing. I wanted to create a small lab for my Ansible scripts testing. The natural option that came to my mind was to create some virtual machines with Virtualbox using Vagrant in order to automate that process. However as I&#8217;m [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-29T07:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-08-05T06:32:31+00:00\" \/>\n<meta name=\"author\" content=\"DevOps\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DevOps\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/\"},\"author\":{\"name\":\"DevOps\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4cd1b5f8a3de93f05a16ab8d7d2b7735\"},\"headline\":\"Using Docker containers for Ansible testing\",\"datePublished\":\"2022-07-29T07:00:00+00:00\",\"dateModified\":\"2022-08-05T06:32:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/\"},\"wordCount\":464,\"commentCount\":0,\"keywords\":[\"Ansible\",\"Docker\"],\"articleSection\":[\"Ansible\",\"DevOps\",\"Docker\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/\",\"name\":\"Using Docker containers for Ansible testing - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"datePublished\":\"2022-07-29T07:00:00+00:00\",\"dateModified\":\"2022-08-05T06:32:31+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4cd1b5f8a3de93f05a16ab8d7d2b7735\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Docker containers for Ansible testing\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4cd1b5f8a3de93f05a16ab8d7d2b7735\",\"name\":\"DevOps\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/cdd2dd7441774355062c0f0f68612296b059cd1e2ff6c7af0b15dba0ed64a85f?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cdd2dd7441774355062c0f0f68612296b059cd1e2ff6c7af0b15dba0ed64a85f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cdd2dd7441774355062c0f0f68612296b059cd1e2ff6c7af0b15dba0ed64a85f?s=96&d=mm&r=g\",\"caption\":\"DevOps\"},\"url\":\"https:\/\/www.dbi-services.com\/blog\/author\/devops\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Using Docker containers for Ansible testing - dbi Blog","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.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/","og_locale":"en_US","og_type":"article","og_title":"Using Docker containers for Ansible testing","og_description":"I&#8217;d like to share in this post a quick setup using Docker containers for Ansible testing. I wanted to create a small lab for my Ansible scripts testing. The natural option that came to my mind was to create some virtual machines with Virtualbox using Vagrant in order to automate that process. However as I&#8217;m [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/","og_site_name":"dbi Blog","article_published_time":"2022-07-29T07:00:00+00:00","article_modified_time":"2022-08-05T06:32:31+00:00","author":"DevOps","twitter_card":"summary_large_image","twitter_misc":{"Written by":"DevOps","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/"},"author":{"name":"DevOps","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4cd1b5f8a3de93f05a16ab8d7d2b7735"},"headline":"Using Docker containers for Ansible testing","datePublished":"2022-07-29T07:00:00+00:00","dateModified":"2022-08-05T06:32:31+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/"},"wordCount":464,"commentCount":0,"keywords":["Ansible","Docker"],"articleSection":["Ansible","DevOps","Docker"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/","url":"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/","name":"Using Docker containers for Ansible testing - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2022-07-29T07:00:00+00:00","dateModified":"2022-08-05T06:32:31+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4cd1b5f8a3de93f05a16ab8d7d2b7735"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/using-docker-containers-for-ansible-testing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Using Docker containers for Ansible testing"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4cd1b5f8a3de93f05a16ab8d7d2b7735","name":"DevOps","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/cdd2dd7441774355062c0f0f68612296b059cd1e2ff6c7af0b15dba0ed64a85f?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/cdd2dd7441774355062c0f0f68612296b059cd1e2ff6c7af0b15dba0ed64a85f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cdd2dd7441774355062c0f0f68612296b059cd1e2ff6c7af0b15dba0ed64a85f?s=96&d=mm&r=g","caption":"DevOps"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/devops\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/18140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/109"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=18140"}],"version-history":[{"count":21,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/18140\/revisions"}],"predecessor-version":[{"id":18310,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/18140\/revisions\/18310"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=18140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=18140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=18140"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=18140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}