Dependency issue with azure.azcollection, Ansible installed via Homebrew

I’m using Ansible on macOS, installed via homebrew. I’m trying to use azure.azcollection, it’s telling me …

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'msrestazure'
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (ansible[azure] (azure >= 2.0.0)) on WORKSTATION's Python /usr/local/Cellar/ansible/9.2.0/libexec/bin/python. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}

:warning: I am not a python expert. I’ve been programming since 1981 but haven’t needed to use python until ansible, so it would not surprise me to find that I’m missing some details that others might find obvious.

With that said, what I’m getting from the error message is …

  • The azure.azcollection collection has dependencies which apparently were not installed by homebrew, even though azure.azcollection itself was installed as part of brew install ansible.
  • Installing python dependencies should be done in a “virtual environment”, to avoid making unintended changes to python files needed by the OS.

So I created and activated a virtual environment …

cd $WORK
pip3 -m venv venv
source venv/bin/activate

… then tried to install the dependencies within the venv/ directory.

cd $( brew --prefix ansible )/libexec/lib/python3.12/site-packages/ansible_collections/azure/azcollection/
pip3 install requirements-azure.txt

It downloaded and installed a bunch of packages, however it didn’t install them under the $WORK/venv/ directory, it installed them under /usr/local/Cellar/ansible/9.2.0/libexec/lib/python3.12/site-packages/ansible_collections/azure/azcollection/.

Am I mis-understanding how virtual environments work? Should it have installed the packages under $WORK/venv/, or did it do “the right thing” and I don’t understand what it was supposed to do?

Moving on … I then tried to run the playbook again, with the virtual environment still “active”.

cd $WORK
ansible-playbook xxx.yml

Same exact error message.

So my other question is … if the modules were installed in the correct location above, does ansible-playbook not pay attention to the VIRTUAL_ENV environment variable (which is still set), and it’s looking in some other location to find dependencies?

(Full disclosure: apparently I hadn’t run brew update in about two weeks, so this could also be an issue with outdated packages. brew upgrade is running in another window as I type this, once that finishes I’m going to try all this again and see if it helps.)