{"id":2509,"date":"2023-02-27T19:28:26","date_gmt":"2023-02-27T19:28:26","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=2509"},"modified":"2023-02-28T17:36:30","modified_gmt":"2023-02-28T17:36:30","slug":"venv-ansible","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2023\/02\/27\/venv-ansible\/","title":{"rendered":"VENV: Ansible"},"content":{"rendered":"\n<div class=\"wp-block-columns are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"299\" height=\"121\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2023\/02\/an-venv.png\" alt=\"\" class=\"wp-image-2510\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>Ansible and Molecule are popular tools for configuration management and testing in the world of DevOps. To avoid conflicts between different projects and their dependencies, it is recommended to use a virtual environment for each project. In this blog post, we&#8217;ll show you how to set up and use a virtual environment using Python&#8217;s built-in <code>venv<\/code> module on <code>macOS<\/code> and <code>CentOS 7<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Pre-requisites<\/h2>\n\n\n\n<p>Before we begin, you&#8217;ll need the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python 3 installed on your system<\/li>\n\n\n\n<li><code>pip<\/code> installed on your system<\/li>\n\n\n\n<li>Basic knowledge of the command line interface<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">On Centos7<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open a terminal window and create a directory for your project:\n<ul class=\"wp-block-list\">\n<li><code>mkdir my-ansible-project <\/code><\/li>\n\n\n\n<li><code>cd my-ansible-project<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Install the <code>python3-venv<\/code> package:\n<ul class=\"wp-block-list\">\n<li><code>sudo yum install python3-venv<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Create a new virtual environment:\n<ul class=\"wp-block-list\">\n<li><code>python3 -m venv venv <\/code>\n<ul class=\"wp-block-list\">\n<li>This will create a new directory named <code>venv<\/code> in your project directory, which contains the virtual environment.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Activate the virtual environment:\n<ul class=\"wp-block-list\">\n<li><code>source venv\/bin\/activate<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Install Ansible and Molecule:\n<ul class=\"wp-block-list\">\n<li><code>pip install ansible molecule<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Verify that Ansible and Molecule are installed:\n<ul class=\"wp-block-list\">\n<li><code>ansible --version <\/code><\/li>\n\n\n\n<li><code>molecule --version <\/code>\n<ul class=\"wp-block-list\">\n<li>This should display the version of Ansible and Molecule installed in the virtual environment.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>When you&#8217;re done working with Ansible and Molecule, you can deactivate the virtual environment:\n<ul class=\"wp-block-list\">\n<li><code>deactivate<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">On OSX<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open a terminal window and create a directory for your project:\n<ul class=\"wp-block-list\">\n<li><code>mkdir my-ansible-project <\/code><\/li>\n\n\n\n<li><code>cd my-ansible-project<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Create a new virtual environment:\n<ul class=\"wp-block-list\">\n<li><code>python3 -m venv venv <\/code>\n<ul class=\"wp-block-list\">\n<li>This will create a new directory named <code>venv<\/code> in your project directory, which contains the virtual environment.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Activate the virtual environment:\n<ul class=\"wp-block-list\">\n<li><code>source venv\/bin\/activate<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Install Ansible and Molecule:\n<ul class=\"wp-block-list\">\n<li><code>pip install ansible molecule<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Verify that Ansible and Molecule are installed:\n<ul class=\"wp-block-list\">\n<li><code>ansible --version <\/code><\/li>\n\n\n\n<li><code>molecule --version <\/code>\n<ul class=\"wp-block-list\">\n<li>This should display the version of Ansible and Molecule installed in the virtual environment.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>When you&#8217;re done working with Ansible and Molecule, you can deactivate the virtual environment:\n<ul class=\"wp-block-list\">\n<li><code>deactivate<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Using the Virtual Environment<\/h2>\n\n\n\n<p>To use Ansible and Molecule inside the virtual environment, you need to activate the environment first. <\/p>\n\n\n\n<p>This is done with the <code>source<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><code>source venv\/bin\/activate\n<\/code><\/code><\/pre>\n\n\n\n<p>Once the virtual environment is activated, you can use Ansible and Molecule as you normally would. When you&#8217;re done, you can deactivate the virtual environment with the <code>deactivate<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><code>deactivate\n<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this blog post, we showed you how to set up and use a virtual environment using Python&#8217;s built-in <code>venv<\/code> module on macOS and CentOS 7 for Ansible and Molecule. Using a virtual environment helps keep your projects isolated and avoids conflicts between different dependencies.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ansible and Molecule are popular tools for configuration management and testing in the world of DevOps. To avoid conflicts between different projects and their dependencies, it is recommended to use a virtual environment for each project. In this blog post, we&#8217;ll show you how to set up and use a virtual environment using Python&#8217;s built-in [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2510,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[278,270,290],"class_list":["post-2509","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical","tag-ansible","tag-python","tag-venv"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=2509"}],"version-history":[{"count":2,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2509\/revisions"}],"predecessor-version":[{"id":2512,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2509\/revisions\/2512"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/2510"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=2509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=2509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=2509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}