|
| 1 | +--- |
| 2 | +title: 12.2 Ansible Module Development - Environment Setup |
| 3 | +weight: 122 |
| 4 | +sectionnumber: 12 |
| 5 | +--- |
| 6 | + |
| 7 | +In this section, we'll dive into setting up an isolated Python environment specifically tailored for Ansible module |
| 8 | +development. |
| 9 | +We'll be using `pipenv`, a powerful tool that simplifies dependency management and virtual environment creation. |
| 10 | + |
| 11 | +### Why Use Virtual Environments? |
| 12 | + |
| 13 | +When working on multiple Python projects, it's crucial to isolate their dependencies. |
| 14 | +Different projects might rely on different versions of the same library, or even different Python versions altogether. |
| 15 | +Installing packages globally can lead to conflicts and break existing projects. |
| 16 | +Virtual environments solve this problem by creating isolated spaces where you can install project-specific dependencies |
| 17 | +without affecting other projects or the system-wide Python installation. |
| 18 | + |
| 19 | +{{% alert title="Note" color="primary" %}} |
| 20 | +This section covers a Python environment setup using `pipenv`. If you are already well-versed with Python and any other |
| 21 | +environment management tool like `poetry`, `venv`, `uv`, etc., feel free to skip this section. |
| 22 | +However, keep in mind that you still need to install the necessary package `ansible-dev-tools` within your |
| 23 | +chosen environment. |
| 24 | + |
| 25 | +{{% /alert %}} |
| 26 | + |
| 27 | +### Introducing `pipenv` |
| 28 | + |
| 29 | +`pipenv` is a modern tool designed to handle both virtual environments and dependency management in Python projects. It |
| 30 | +streamlines the process of: |
| 31 | + |
| 32 | +* **Creating and managing virtual environments:** `pipenv` automatically creates a virtual environment for your project |
| 33 | + in the current directory or a specified location and handles all activiation for you. |
| 34 | +* **Managing dependencies:** It uses a `Pipfile` to track your project's dependencies and a `Pipfile.lock` to ensure |
| 35 | + reproducible builds. |
| 36 | +* **Simplified workflow:** It offers intuitive commands for installing, uninstalling, and updating packages. |
| 37 | + |
| 38 | +### Task 1 |
| 39 | + |
| 40 | +Let's have a closer look at the `ansible-dev-tools` package. |
| 41 | + |
| 42 | +The `ansible-dev-tools` package is a curated collection of essential tools for developing, testing, and |
| 43 | +maintaining Ansible content. |
| 44 | +It significantly simplifies the development workflow by bundling together frequently used utilities. |
| 45 | +You can find detailed information about it |
| 46 | +here: [Ansible Development Tools Documentation](https://ansible.readthedocs.io/projects/dev-tools). |
| 47 | + |
| 48 | +What useful ansible-tools does `ansible-dev-tools` contain? |
| 49 | + |
| 50 | +{{% details title="Solution Task 1" %}} |
| 51 | + |
| 52 | +Here's a breakdown of the key packages included: |
| 53 | + |
| 54 | +* **`ansible-builder`:** Automates the process of building Ansible execution environments using schemas and tooling |
| 55 | + defined in Ansible Collections. |
| 56 | +* **`ansible-core`:** The core Ansible automation engine, enabling configuration management, application deployment, and |
| 57 | + task automation. |
| 58 | +* **`ansible-creator`:** A tool designed for efficiently generating Ansible content, helping to streamline development |
| 59 | + and content creation. |
| 60 | +* **`ansible-dev-environment`**: A pip-like install for Ansible collections. |
| 61 | +* **`ansible-lint`:** Analyzes playbooks and identifies areas for improvement in terms of best practices and potential |
| 62 | + issues. |
| 63 | +* **`ansible-navigator`:** Provides a text-based user interface (TUI) for interacting with and managing Ansible. |
| 64 | +* **`ansible-sign`**: Utility for signing and verifying Ansible project directory contents. |
| 65 | +* **`molecule`:** Aids in the development and testing of various Ansible content types, including collections, |
| 66 | + playbooks, and roles. |
| 67 | +* **`pytest-ansible`:** A plugin that allows using Ansible within pytest tests, and it helps in using pytest as a |
| 68 | + collection unit test runner, while also allowing molecule scenarios as pytest fixtures. |
| 69 | +* **`tox-ansible`:** Provides a method to create a matrix of Python and Ansible environments to run tests for Ansible |
| 70 | + collections, both locally and with Github actions. |
| 71 | + |
| 72 | +{{% /details %}} |
| 73 | + |
| 74 | +### Task 2 |
| 75 | + |
| 76 | +If you don't have pipenv installed yet, you can install it globally using `pip` (which is usually installed with your |
| 77 | +Python distribution): |
| 78 | + |
| 79 | +```bash |
| 80 | +pip install pipenv |
| 81 | +``` |
| 82 | + |
| 83 | +Can you tell what pipenv version has been installed on your system? |
| 84 | + |
| 85 | +{{% details title="Solution Task 2" %}} |
| 86 | +You can get the pipenv version by running the following: |
| 87 | + |
| 88 | +```bash |
| 89 | +pipenv --version |
| 90 | +``` |
| 91 | + |
| 92 | +The output may look like this |
| 93 | + |
| 94 | +```bash |
| 95 | + $ pipenv --version |
| 96 | +pipenv, version 2024.1.0 |
| 97 | +``` |
| 98 | + |
| 99 | +{{% /details %}} |
| 100 | + |
| 101 | +### Task 3 |
| 102 | + |
| 103 | +Let's create a new project with a dedicated virtual environment. |
| 104 | + |
| 105 | +1. **Navigate to your project directory:** |
| 106 | + |
| 107 | + ```bash |
| 108 | + cd /home/ansible/techlab/ansible-module-development |
| 109 | + ``` |
| 110 | + |
| 111 | +2. **Create the virtual environment:** |
| 112 | + To create the virtual environment run: |
| 113 | + ```bash |
| 114 | + pipenv install |
| 115 | + ``` |
| 116 | + What happened with this command? |
| 117 | + |
| 118 | +{{% details title="Solution Task 3" %}} |
| 119 | + |
| 120 | +* It created a `Pipfile` and a `Pipfile.lock` in the current directory. |
| 121 | +* A virtual environment has been created at `pipenv --venv` |
| 122 | + {{% /details %}} |
| 123 | + |
| 124 | +### Task 4 |
| 125 | + |
| 126 | +In this techlab we rely on the version 25.2.1 of the `ansible-dev-tools` package. |
| 127 | +Can you find out how to install it in your new pipenv? |
| 128 | + |
| 129 | +{{% details title="Solution Task 4" %}} |
| 130 | + |
| 131 | +```bash |
| 132 | +pipenv install ansible-dev-tools==25.2.1 |
| 133 | +``` |
| 134 | + |
| 135 | +* This will add `ansible-dev-tools` to your `Pipfile` and install it within the project's virtual environment. |
| 136 | +* The version of the package is explicitly set in the `[packages]` directive of the `Pipfile`. |
| 137 | +* `pipenv` also automatically updates the `Pipfile.lock`, so other developers can get the exact same dependency |
| 138 | + versions. |
| 139 | + |
| 140 | +{{% alert title="Note on Version-Specific Dependency Installation" color="warning" %}} |
| 141 | +When installing dependencies with pipenv, it's highly recommended to specify the exact version you need, like this: |
| 142 | +`pipenv install ansible-dev-tools==25.2.1`, instead of just `pipenv install ansible-dev-tools`. |
| 143 | +This ensures version locking, reproducible builds, prevents unexpected breakage, ensures an explicit dependency and |
| 144 | +compatibility. |
| 145 | +In short, always be explicit about the version you want to install. |
| 146 | +You can always update to a newer version later (after testing it!), but starting with a locked version provides a much |
| 147 | +more stable foundation. |
| 148 | +{{% /alert %}} |
| 149 | + |
| 150 | +{{% /details %}} |
| 151 | + |
| 152 | +### Task 5 |
| 153 | + |
| 154 | +To start working within your virtual environment, you need to "activate" it. pipenv makes this super easy: |
| 155 | + |
| 156 | +```bash |
| 157 | +pipenv shell |
| 158 | +``` |
| 159 | + |
| 160 | +* This command will launch a new shell session within the virtual environment. |
| 161 | + |
| 162 | +How can you verify if the shell is active? |
| 163 | + |
| 164 | +{{% details title="Solution Task 4" %}} |
| 165 | + |
| 166 | +* You can verify that the virtual environments python interpreter is used by checking the output of `which python`. If |
| 167 | + the python interpreter is located inside the virtualenv at the location of `pipenv --venv` your virtual environment is |
| 168 | + active. |
| 169 | +* You could also verify if any of the installed dependencies are available by e.g. running `ansible` or any other |
| 170 | + command from the `ansible-dev-tools` package. |
| 171 | + |
| 172 | +{{% /details %}} |
| 173 | + |
| 174 | +### Deactivating the Environment |
| 175 | + |
| 176 | +To exit the virtual environment and return to your system's default shell, simply type: |
| 177 | + |
| 178 | +```bash |
| 179 | +exit |
| 180 | +``` |
| 181 | + |
| 182 | +### All done? |
| 183 | + |
| 184 | +* [Automatic Loading of .env in pipenv](https://pipenv.pypa.io/en/latest/shell.html#automatic-loading-of-env) |
| 185 | +* [Checkout pipenv workflows](https://pipenv.pypa.io/en/latest/workflows.html#pipenv-workflows) |
0 commit comments