Chapter 15 Ansible and Docker: No module named 'requests'
fex01 opened this issue · 0 comments
Even after installing the docker Python library - and checking the requests module - I got the error message ModuleNotFoundError: No module named 'requests'.
After getting a bit more familiar with the -vvv log output, it became obvious that the playbook was using the wrong Python interpreter.
Solution one: installing the docker Python library as root to make the library available for the playbook.
Solution two: explicitly stating the python interpreter - either in the inventory file:
[localhost]
ansible_python_interpreter="/usr/bin/python3"or in the playbook itself:
---
- hosts: localhost
connection: local
# playbook fails to find installed Python packages if interpreter is not set explicitly
vars:
ansible_python_interpreter: /usr/bin/python3
tasks:
...Just leaving this here in case that someone else is running into similar troubles.
This issue might potentially be caused by me using a VS Code DevContainer as Ansible host 🤔
In case that someone else wants to have the same experience - here is the devcontainer.json file:
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/ubuntu
{
"name": "Ansible",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/base:jammy",
// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
// https://github.com/devcontainers/features/tree/main/src/git
"ghcr.io/devcontainers/features/git:1": {
"ppa": true,
"version": "latest" // https://github.com/git/git/tags
},
// https://github.com/devcontainers-contrib/features/tree/main/src/ansible
"ghcr.io/devcontainers-contrib/features/ansible:2": {
"version": "latest"
},
// https://github.com/devcontainers/features/tree/main/src/docker-in-docker
"ghcr.io/devcontainers/features/docker-in-docker:2": {
// fails for debian:bookworm, successful for debian:bullseye (arch=arm64)
// fails for versions newer than 23.0.7 (arch=arm64)
"version": "23.0.7", // https://docs.docker.com/engine/release-notes/24.0/
"moby": true,
"dockerDashComposeVersion": "v2",
"azureDnsAutoDetection": false,
"dockerDefaultAddressPool": "base=192.168.0.0/16,size=24",
"installDockerBuildx": true
},
// https://github.com/devcontainers-contrib/features/tree/main/src/apt-get-packages
"ghcr.io/devcontainers-contrib/features/apt-get-packages:1": {
"packages": "vim,gnupg,software-properties-common,curl,jq,iputils-ping",
"ppas": "",
"clean_ppas": false,
"preserve_apt_list": true,
"force_ppas_on_non_ubuntu": false
}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "uname -a",
// Configure tool-specific properties.
"customizations": {
"vscode": {
"settings": {
"editor.minimap.enabled": false,
"files.autoSave": "afterDelay",
"git.enableSmartCommit": true,
},
"extensions": [
"microsoft.vscode-black-formatter",
"microsoft.vscode-docker",
"Microsoft.vscode-python",
"DonJayamanne.vscode-python-manager",
"streetsidesoftware.vscode-spell-checker",
"donjayamanne.githistory",
"GitHub.copilot"
]
}
},
// Configure mounts from host to container.
"mounts": [
// host SSH key so you can connect to Git repos
"source=${localEnv:HOME}/.ssh,target=/home/vscode/.ssh,type=bind"
],
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
"postStartCommand": "git config --global --add safe.directory ${containerWorkspaceFolder}"
}