geerlingguy / geerlingguy/ansible-for-devops

Chapter 15 Ansible and Docker: No module named 'requests'

Open
#576 0 comments 4 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
9.9k
Forks
3.8k
PR merge metrics
No merged PRs in 30d

Description

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:

```txt
[localhost]
ansible_python_interpreter="/usr/bin/python3"
```

or in the playbook itself:

```yml
---
- 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:

```json
// 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}"
}

```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with Chapter 15, “Ansible and Docker,” and review how the playbook's Python interpreter and required packages are described. Done means the chapter explains the interpreter mismatch and documents the inventory or playbook configuration shown in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python
Domain
devops, documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.