Any proper way to use ansible-bender on base images without a Python runtime?
Hello, just trying to test ansible-bender on a official alpine base image on Docker Hub but encountered the following error:
$ ansible-bender build playbooks/build-container.yml
01:22:35.205 buildah_builder.py ERROR couldn't locate python interpreter, tried these paths: ('/usr/bin/python3', '/usr/local/bin/python3', '/usr/bin/python3.7', '/usr/bin/python37', '/usr/bin/python3.6', '/usr/bin/python36', '/usr/bin/python2', '/usr/local/bin/python2', '/usr/bin/python', '/usr/local/bin/python', '/usr/libexec/platform-python')
There was an error during execution: no python interpreter was found in the base image "docker.io/alpine:latest", you can specify the path via CLI option --python-interpreter
Then, I tried to install a Python3 runtime beforehand in my playbook but it didn't work:
---
- name: 使用 ansible-bender 建置容器
hosts: all
# Alpine doesn't have Python installation by default we collect these
# later after we have one
gather_facts: False
vars:
ansible_bender:
base_image: docker.io/alpine:latest
target_image:
name: latest-bash
cmd: ash
pre_tasks:
- name: Install Python for Ansible runtime
raw: apk add python3
tasks:
- name: Install GNU Bash
become: True
apk:
name: bash
Is there anyway to do so, without preparing a custom image?
I RTFM'd and found that a Python interpreter is a requirement of the base image. However what I can't understand is why it is required in the first place as Ansible playbooks don't necessarily require Python on the managed node to run (for instance, a playbook only calling the raw module to do the job, or using it to install a Python runtime before doing other tasks that requiring one).
If the only reason that requires ansible-bender to locate a Python interpreter is to tell Ansible the proper one to use one can always provide it via the ansible_extra_arg configuration key.
I found a workaround to make ansible-bender happy for the alpine image:
Build command:
ansible-bender build --python-interpreter /usr/bin/python3 playbooks/build-container.yml
Refined playbook:
---
- name: Build latest-bash container using ansible-bender
hosts: all
# Alpine doesn't have Python installation by default we collect these
# later after we have one
gather_facts: False
vars:
ansible_bender:
base_image: alpine:latest
target_image:
name: latest-bash
cmd: bash
pre_tasks:
- name: Install Python for Ansible runtime
raw: apk add python3
- name: Gather facts after Python runtime is available
setup:
post_tasks:
- name: Remove unneeded Python
raw: apk del python3
- name: Drop apk cache
raw: ash -c 'rm /var/cache/apk/*'
tasks:
- name: Install GNU Bash
apk:
name: bash
there is an epic which discuss this precise issue https://github.com/ansible-community/ansible-bender/issues/49
the thing is that ansible actually requires python interpreter to be available in the target container
Thank you @Lin-Buo-Ren . Currently, I use your solution. Maybe you can make a gist to put that little code in.