<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Thien Ho on Medium]]></title>
        <description><![CDATA[Stories by Thien Ho on Medium]]></description>
        <link>https://medium.com/@thienngho?source=rss-54b5397b696e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*BiZ78GavgdkGaQbl.png</url>
            <title>Stories by Thien Ho on Medium</title>
            <link>https://medium.com/@thienngho?source=rss-54b5397b696e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 23:17:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@thienngho/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Testing Ansible roles with Molecule, Docker and integrate with TravisCI]]></title>
            <link>https://medium.com/@thienngho/testing-ansible-roles-with-molecule-docker-and-integrate-with-travisci-c0c6f42dfa0?source=rss-54b5397b696e------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0c6f42dfa0</guid>
            <category><![CDATA[travis-ci]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[ansible]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[molecule]]></category>
            <dc:creator><![CDATA[Thien Ho]]></dc:creator>
            <pubDate>Tue, 31 Jul 2018 05:23:11 GMT</pubDate>
            <atom:updated>2018-07-31T05:23:11.998Z</atom:updated>
            <content:encoded><![CDATA[<p>While I’m working with Ansible, specifically when writing Ansible roles, I usually launch EC2 instance, run playbook, wait errors appear, fix those errors then rerun playbook until everything is good. This process is really taking time and effort. We need to check not only syntax but also configuration files are applied correct or not, the processes are running or stopped, and also waiting EC2 instance to spin up.</p><p>At the end, I need a end-to-end testing strategy when writing Ansible roles and playbooks, from unit tests to integration tests. After googling around, I found <a href="https://github.com/metacloud/molecule/">Molecule</a>.</p><blockquote>Molecule is designed to aid in the development and testing of Ansible roles. Molecule provides support for testing with multiple instances, operating systems and distributions, virtualization providers, test frameworks and testing scenarios.</blockquote><p>In this article I will show how to test a simple Ansible roles with Molecule and Docker then integrate with TravisCI after we push role to Github.</p><h4>Getting started</h4><ul><li>Ensure Docker, Ansible are installed</li><li>Installing Molecule with pip</li></ul><pre>➤ pip install molecule</pre><pre>➤ molecule — version<br>molecule, version 2.15.0</pre><p>Here are a few features of Molecule explained, though the full toolset offers more.</p><ol><li><em>Init</em>: Initialize a new role or scenario.</li><li><em>Create: </em>This creates a virtualized provider, which in our case is the Docker container.</li><li><em>Converge: </em>Uses the provisioner and runs the Ansible scripts to the target running Docker containers.</li><li><em>Idempotence: </em>Uses the provisioner to check the idempotency of Ansible scripts.</li><li><em>Lint: </em>This does code audits of Ansible scripts, test code, test scripts, etc.</li><li><em>Verify: </em>Runs the test scripts written.</li><li><em>Test: </em>Runs the full steps needed for Molecule, i.e., create, converge, Lint, verify and destroy.</li></ol><ul><li>Creating a new Ansible Nginx role.</li></ul><pre>➤ molecule init role --role-name ansible-role-nginx --driver-name docker<br>--&gt; Initializing new role ansible-role-nginx...<br>Initialized role in /private/tmp/ansible-role-nginx successfully.</pre><p>This will create an Ansible role with the following directory structure.</p><pre>ansible-role-nginx<br>├── README.md<br>├── defaults<br>│   └── main.yml<br>├── handlers<br>│   └── main.yml<br>├── meta<br>│   └── main.yml<br>├── molecule<br>│   └── default<br>│       ├── Dockerfile.j2<br>│       ├── INSTALL.rst<br>│       ├── molecule.yml<br>│       ├── playbook.yml<br>│       └── tests<br>│           ├── test_default.py<br>│           └── test_default.pyc<br>├── tasks<br>│   └── main.yml<br>└── vars<br>    └── main.yml</pre><pre>8 directories, 12 files</pre><p>Molecule internally uses <strong>ansible-galaxy init </strong>command to create a role structure. It then puts following extra files in it.</p><ul><li>molecule.yml | test framework configuration</li><li>playbook.yml | playbook for executing the role in the vagrant</li><li>tests/test_default.py | test infra unit test cases</li></ul><p>By default molecule.yml uses CentOS docker image. I focus on Ubuntu platform so I will change image to use Ubuntu.</p><pre>platforms:<br>  - name: instance<br>    image: ubuntu:16.04<br>    privileged: True<br>    command: /sbin/init</pre><p>We modify tasks/main.yml to install Nginx package and ensure Nginx is started and enabled to start at boot.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b91c1da98cf25ab9c57752c7bfe3aed5/href">https://medium.com/media/b91c1da98cf25ab9c57752c7bfe3aed5/href</a></iframe><p>The next step is writing test in molecule/default/tests/test_default.py</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be96488b8df034f6fd8e765b69633098/href">https://medium.com/media/be96488b8df034f6fd8e765b69633098/href</a></iframe><p>So we’re done with Ansible role and now running test with molecule test command. The result as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f882b9fdb9b93850308f5acc0b7516f3/href">https://medium.com/media/f882b9fdb9b93850308f5acc0b7516f3/href</a></iframe><p>If there is something wrong, the process is stopped and the errors are shown for us to debug. For example we forget adding end of line in file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1769cc894b2cb21e5769c7cb3366f62/href">https://medium.com/media/c1769cc894b2cb21e5769c7cb3366f62/href</a></iframe><p>We’re easily to navigate and fix any issue related.</p><p>After done with local testing, we can push to Github and integrate with TravisCI. We just need to create .travis.yml with content below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38326a2bc39d47aa5abcd93e06c680b3/href">https://medium.com/media/38326a2bc39d47aa5abcd93e06c680b3/href</a></iframe><p>I pushed my Ansible role to Github at: <a href="https://github.com/thienngho/ansible-role-nginx">https://github.com/thienngho/ansible-role-nginx</a> and Travis CI integration: <a href="https://travis-ci.org/thienngho/ansible-role-nginx">https://travis-ci.org/thienngho/ansible-role-nginx</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0c6f42dfa0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>