3

I have some ansible roles and I would like to use molecule testing with them.

When I execute command molecule init scenario -r get_files_uid -d docker I get the following file structure

get_files_uid
├── molecule
│   └── default
│       ├── converge.yml
│       ├── molecule.yml
│       └── verify.yml
├── tasks
│   └── main.yml
└── vars
    └── main.yml

After that, I execute molecule test and I receive the following error:

PLAY [Converge] ****************************************************************

TASK [Gathering Facts] *********************************************************
fatal: [instance]: FAILED! => {"ansible_facts": {}, "changed": false, "failed_modules": {"ansible.legacy.setup": {"failed": true, "module_stderr": "/bin/sh: python: command not found\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 127}}, "msg": "The following modules failed to execute: ansible.legacy.setup\n"}

PLAY RECAP *********************************************************************
instance                   : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

My ansible.cfg looks like this:

[defaults]
roles_path = roles
ansible_python_interpreter = /usr/bin/python3

And I use MacOS with Ansible

ansible [core 2.13.3]
  config file = None
  configured module search path = ['/Users/scherevko/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/homebrew/Cellar/ansible/6.3.0/libexec/lib/python3.10/site-packages/ansible
  ansible collection location = /Users/scherevko/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/homebrew/bin/ansible
  python version = 3.10.6 (main, Aug 11 2022, 13:36:31) [Clang 13.1.6 (clang-1316.0.21.2.5)]
  jinja version = 3.1.2
  libyaml = True

molecule version:

molecule 4.0.1 using python 3.10 
    ansible:2.13.3
    delegated:4.0.1 from molecule
    docker:2.0.0 from molecule_docker requiring collections: community.docker>=3.0.0-a2
    podman:2.0.2 from molecule_podman requiring collections: containers.podman>=1.7.0 ansible.posix>=1.3.0

When I run molecule --debug test I see

ANSIBLE_PYTHON_INTERPRETER: python not found

How to fix that?

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
a1dude
  • 174
  • 2
  • 13

1 Answers1

0

The default scaffold for role molecule role initialization uses quay.io/centos/centos:stream8 as the test instance image (see molecule/default/molecule.yml)

This image does not have any /usr/bin/python3 file available:

$ docker run -it --rm quay.io/centos/centos:stream8 ls -l /usr/bin/python3
ls: cannot access '/usr/bin/python3': No such file or directory

If you let ansible discover the available python by itself, you'll see that the interpreter actually found is /usr/libexec/platform-python like in the following demo (no ansible.cfg in use):

$ docker run -d --rm --name instance quay.io/centos/centos:stream8 tail -f /dev/null
2136ad2e8b91f73d21550b2403a6b37f152a96c2373fcb5eb0491a323b0ed093

$ ansible instance -i instance, -e ansible_connection=docker -m setup | grep discovered
        "discovered_interpreter_python": "/usr/libexec/platform-python",

$ docker stop instance 
instance

Since your ansible.cfg only contains a default value for role path besides that wrong python interpreter path, I suggest you simply remove that file which will fix your problem. At the very least, remove the line defining ansible_python_interpreter to use default settings.

Note that you should also make sure that ANSIBLE_PYTHON_INTERPRETER is not set as a variable in your current shell (and remove that definition from whatever shell init file if it is the case).

Hardcoding the path of the python interpreter should anyway be your very last solution in very few edge cases.

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • hmmm, when i delete `ansible_python_interpreter` from ansible.cfg - nothing was change. After that i was decide to check variable `echo $ANSIBLE_PYTHON_INTERPRETER` and i receive `python not found`. When i execute `ANSIBLE_PYTHON_INTERPRETER=""` And all fixed! may be its my mistake, i don't know. Thank you for your minds, they helps me think – a1dude Sep 01 '22 at 10:56
  • The fact that `ANSIBLE_PYTHON_INTERPRETER` is set as an environment variable in your currnent environment was not precised in your question. The solution here is to remove that definition from your shell init file (e.g. .bashrc if you use bash). At the very least you should `unset ANSIBLE_PYTHON_INTERPRETER` in the current shell – Zeitounator Sep 01 '22 at 11:06
  • So i try to test another role in which I need to install some software and choose ubuntu docker container instead centos. After execute `molecule test` i receive `{"discovered_interpreter_python": "/usr/bin/python"}, "failed": true, "module_stderr": "/bin/sh: 1: /usr/bin/python: not found\n", "module_stdout": "", "msg": "The module failed to execute correctly, you probably need to set the interpreter."}` Why he can't find python3 in a new docker ubuntu? – a1dude Sep 01 '22 at 11:55
  • The official ubuntu docker image does not have any python installed. You need to `apt install python3` – Zeitounator Sep 01 '22 at 11:59