Ansible Pilot

Leveraging Poetry for Efficient Virtual Environment Management

Unveiling the Verses: Navigating Virtual Environments with Poetry and Ansible.

November 17, 2023
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

Leveraging Poetry for Efficient Virtual Environment Management

In the ever-evolving landscape of software development, managing dependencies and creating a controlled environment for project execution is crucial. Python developers often find themselves juggling with virtual environments to isolate project dependencies, ensuring consistency and reproducibility across different setups. While there are several tools available for this task, one standout option that combines simplicity with power is Poetry.

Understanding Poetry

Poetry is not just a literary form; it’s also a Python packaging and dependency management tool. It streamlines the process of defining and installing project dependencies, making the management of virtual environments a seamless experience. To embark on this journey, let’s explore how Poetry can be employed for managing a virtual environment, taking Ansible as a case study.

Setting the Stage

Firstly, create a directory for your project and navigate into it:

$ mkdir ansible-project
$ cd ansible-project

Next, initiate a Poetry project within the directory:

$ poetry init

This command will prompt you to provide details about your project, such as its name, version, and dependencies.

This command will guide you through creating your pyproject.toml config.

Package name [ansible-project]:  
Version [0.1.0]:  
Description []:  
Author [Luca Berton <[email protected]>, n to skip]:  
License []:  
Compatible Python versions [^3.12]:  

Would you like to define your main dependencies interactively? (yes/no) [yes] 
You can specify a package in the following forms:
  - A single name (requests): this will search for matches on PyPI
  - A name and a constraint (requests@^2.23.0)
  - A git url (git+
  - A git url with a revision (git+
  - A file path (../my-package/my-package.whl)
  - A directory (../my-package/)
  - A url (

Package to add or search for (leave blank to skip): 

Would you like to define your development dependencies interactively? (yes/no) [yes] 
Package to add or search for (leave blank to skip): 

Generated file

name = "ansible-project"
version = "0.1.0"
description = ""
authors = ["Luca Berton <[email protected]>"]
readme = ""

python = "^3.12"

requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

Do you confirm generation? (yes/no) [yes]

The Best Resources For Ansible


Video Course

Printed Book


Adding Dependencies

With the project initialized, add Ansible to the list of dependencies using the poetry add command:

$ poetry add ansible

Poetry will handle the installation of Ansible and any additional dependencies required.

Creating virtualenv ansible-project-wMMHKn40-py3.12 in /Users/lberton/Library/Caches/pypoetry/virtualenvs
Using version ^8.6.1 for ansible

Updating dependencies
Resolving dependencies... Downloading (Resolving dependencies... Downloading dependencies... Downloading dependencies... Downloading dependencies... Downloading dependencies... Downloading dependencies... Downloading dependencies... Downloading dependencies... Downloading dependencies... (2.5s)

Package operations: 10 installs, 0 updates, 0 removals

  • Installing pycparser (2.21)
  • Installing cffi (1.16.0)
  • Installing markupsafe (2.1.3)
  • Installing cryptography (41.0.5)
  • Installing jinja2 (3.1.2)
  • Installing packaging (23.2)
  • Installing pyyaml (6.0.1)
  • Installing resolvelib (1.0.1)
  • Installing ansible-core (2.15.6)
  • Installing ansible (8.6.1)

Writing lock file

Verifying the Installation

Once the installation is complete, you can verify the Ansible version within the virtual environment. Start the Poetry shell:

$ poetry shell

Inside the shell, check the Ansible version:

$ ansible --version

This will display information about the installed Ansible version, its configuration file location, module search path, and other relevant details.

Spawning shell within /Users/lberton/Library/Caches/pypoetry/virtualenvs/ansible-project-wMMHKn40-py3.12
lberton@Lucas-MacBook-Pro ansible-project % emulate bash -c '. /Users/lberton/Library/Caches/pypoetry/virtualenvs/ansible-project-wMMHKn40-py3.12/bin/activate'
(ansible-project-py3.12) lberton@Lucas-MacBook-Pro ansible-project % ansible --version
ansible [core 2.15.6]
  config file = None
  configured module search path = ['/Users/lberton/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/lberton/Library/Caches/pypoetry/virtualenvs/ansible-project-wMMHKn40-py3.12/lib/python3.12/site-packages/ansible
  ansible collection location = /Users/lberton/.ansible/collections:/usr/share/ansible/collections
  executable location = /Users/lberton/Library/Caches/pypoetry/virtualenvs/ansible-project-wMMHKn40-py3.12/bin/ansible
  python version = 3.12.0 (main, Oct  2 2023, 12:03:24) [Clang 15.0.0 (clang-1500.0.40.1)] (/Users/lberton/Library/Caches/pypoetry/virtualenvs/ansible-project-wMMHKn40-py3.12/bin/python)
  jinja version = 3.1.2
  libyaml = True

Poetry Shell Magic

By using poetry shell, you activate a virtual environment specific to your project, isolating its dependencies from the global Python environment. This ensures that your project’s dependencies do not interfere with other projects or the system-wide Python installation.

The output of the ansible --version command also provides insight into the Python version, module locations, and executable locations specific to the virtual environment created by Poetry.


Poetry not only simplifies dependency management but also enhances the overall development experience by providing a clean and isolated virtual environment for your projects. It eliminates the hassle of manual environment setup, making the process more efficient and developer-friendly.

As the Python ecosystem continues to evolve, tools like Poetry become invaluable for maintaining order in the ever-expanding landscape of dependencies. Embrace the poetic simplicity of Poetry in managing your virtual environments, and let the verses of dependency resolution and isolation harmonize with the symphony of your development workflow. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.


Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases