Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any proper way to use ansible-bender on base images without a Python runtime? #260

Open
brlin-tw opened this issue Oct 3, 2021 · 4 comments

Comments

@brlin-tw
Copy link

brlin-tw commented Oct 3, 2021

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?

@brlin-tw brlin-tw changed the title Any proper way to install Python runtime to base image before BuildahBuilder.find_python_interpreter() Any proper way to install Python runtime to base image before BuildahBuilder.find_python_interpreter()? Oct 3, 2021
@brlin-tw brlin-tw changed the title Any proper way to install Python runtime to base image before BuildahBuilder.find_python_interpreter()? Any proper way to use ansible-bender on base images without a Python runtime? Oct 3, 2021
@brlin-tw
Copy link
Author

brlin-tw commented Oct 3, 2021

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.

@brlin-tw
Copy link
Author

brlin-tw commented Oct 3, 2021

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

@TomasTomecek
Copy link
Collaborator

there is an epic which discuss this precise issue #49

the thing is that ansible actually requires python interpreter to be available in the target container

@michaelact
Copy link

michaelact commented Oct 23, 2021

Thank you @Lin-Buo-Ren .
Currently, I use your solution.
Maybe you can make a gist to put that little code in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants