<p>Running Ansible with the Virtual Environment
When running Ansible, explicitly reference the virtual environment Python binary to ensure all dependencies use the correct environment.</p>
<p>Option 1: Use the Virtual Environment Python
Specify the path to the virtual environment's python binary:</p>
<pre class="lang-yaml prettyprint-override"><code>- name: Run command using virtualenv Python
  ansible.builtin.command:
    cmd: /home/&lt;user&gt;/ansible_venv/bin/python -m ansible_runner run my_playbook.yml
</code></pre>
<p>Option 2: Use Environment Variables
Set the PATH to include the virtual environment binaries temporarily:</p>
<pre class="lang-yaml prettyprint-override"><code>- name: Set environment for ansible_runner
  ansible.builtin.environment:
    PATH: &quot;/home/&lt;user&gt;/ansible_venv/bin:{{ ansible_env.PATH }}&quot;
</code></pre>

CC BY-SA 4.0
2024-12-05T14:13:15.100Z
Yeah, doesn't seem like there's a way around it other than to boostrap the virtual env with cloud-init or another playbook. Might be better to set the interpreter under defaults to `interpreter_python = "/home/<user>/ansible_venv/bin/python3"` to run the whole playbook/module in the venv.
RiverHeart
CC BY-SA 4.0
2024-11-28T10:43:17.187Z
2024-11-28T10:43:17.187Z
Qodirov Shohijahon
<p>I started playing with Ansible and, since I know some Python, decided to dig in and learn how to create modules. For this module, I need to setup an Apt repository and rather than reinvent the wheel I wanted call an Ansible role from Python. To do that I needed to install <code>ansible_runner</code>.</p>
<p>On the remote machine (Ubuntu 24.04) pip complains that about installing system packages. This is apparently a new thing.</p>
<pre><code>× This environment is externally managed
╰─&gt; To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.

    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.
</code></pre>
<p>Problem is, I can't find a way of making Ansible execute in a virtualenv. I can force the install using <code>pip install ansible_runner --break-system-packages</code> but that's frowned on and installing from apt means using an ancient, possibly incompatible package.</p>
<p>So if I need to bootstrap <code>ansible_runner</code> on the remote machine and pip is a no go then what is the alternative?</p>
<p>I guess I could setup a virtualenv for the ansible user and give the full path to that interpreter but then every module using <code>ansible_runner</code> would either need to setup this venv or be dependent on it already existing.</p>

<ansible><python>
How to handle python modules with Ansible on Ubuntu 24.04?
RiverHeart
# How to handle python modules with Ansible on Ubuntu 24.04?

(Asked by: username_0 on 2024-11-27)

I started playing with Ansible and, since I know some Python, decided to dig in and learn how to create modules. For this module, I need to setup an Apt repository and rather than reinvent the wheel I wanted call an Ansible role from Python. To do that I needed to install ansible_runner.
On the remote machine (Ubuntu 24.04) pip complains that about installing system packages. This is apparently a new thing.
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.

    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

Problem is, I can't find a way of making Ansible execute in a virtualenv. I can force the install using pip install ansible_runner --break-system-packages but that's frowned on and installing from apt means using an ancient, possibly incompatible package.
So if I need to bootstrap ansible_runner on the remote machine and pip is a no go then what is the alternative?
I guess I could setup a virtualenv for the ansible user and give the full path to that interpreter but then every module using ansible_runner would either need to setup this venv or be dependent on it already existing.

**Tags:** ansible, python


---

## 1 answers

### Answer by username_1 on 2024-11-28. Score: 1 | Accepted answer

Running Ansible with the Virtual Environment
When running Ansible, explicitly reference the virtual environment Python binary to ensure all dependencies use the correct environment.
Option 1: Use the Virtual Environment Python
Specify the path to the virtual environment's python binary:
- name: Run command using virtualenv Python
  ansible.builtin.command:
    cmd: /home//ansible_venv/bin/python -m ansible_runner run my_playbook.yml

Option 2: Use Environment Variables
Set the PATH to include the virtual environment binaries temporarily:
- name: Set environment for ansible_runner
  ansible.builtin.environment:
    PATH: "/home//ansible_venv/bin:{{ ansible_env.PATH }}"

> **Comments:**
> **username_0**: Yeah, doesn't seem like there's a way around it other than to boostrap the virtual env with cloud-init or another playbook. Might be better to set the interpreter under defaults to `interpreter_python = "/home/<user>/ansible_venv/bin/python3"` to run the whole playbook/module in the venv.  

---

