coglinev3.ansible_bender
Ansible Role: ansible_bender
Tired of using Dockerfiles to build containers?
This role sets up ansible-bender, a tool that helps you create containers using Ansible playbooks, turning them into container images.
With ansible-bender, you can build and set up containers the same way you do for virtual machines or physical systems. You can use the power of Ansible and reuse your existing Ansible content for your container setups. You can use templates, copy files, add encrypted data, handle errors, add conditions, and more—all with Ansible's orchestration features during the image building process.
The supported Linux distributions for this role are:
- Alpine Linux 3.14,
- Alpine Linux 3.15,
- Alpine Linux 3.16,
- Alpine Linux 3.17,
- Alpine Linux 3.18,
- Alpine Linux 3.19,
- Enterprise Linux 8,
- Enterprise Linux 9,
- Debian 11 (Bullseye),
- Debian 12 (Bookworm),
- Linux Mint 20 (Ulyana),
- Ubuntu 20.04 LTS (Focal Fossa),
- Ubuntu 22.04 LTS (Jammy Jellyfish).
NOTE
The rootless mode for Podman needs the newuidmap program installed. Enterprise Linux 7 (RHEL 7 / CentOS 7) supports this only from version 7.7.
Requirements
Ansible-bender needs a few programs installed on your host system:
- Buildah
- Podman
- Python 3.6 or later (Python 3.5 or earlier are not supported)
- Ansible (Ansible must be made with Python 3)
All these requirements can be installed with this role.
Role Variables
Here's a list of variables you can customize, along with their default values (see defaults/main.yml):
# Required programs for ansible-bender (buildah, podman, python3.6 or higher)
ab_dependencies:
- buildah
- podman
- python3
- python3-pip
- python3-setuptools
- python3-software-properties
- python3-virtualenv
- procps
- runc
- slirp4netns
# Package state for dependencies: ( present ) | latest
ab_dependencies_package_state: present
# Python packages installed with pip3.
ab_python_packges:
- wheel
- ansible
- ansible-bender
# Package state for Python packages: ( present ) | latest
ab_python_packge_state: present
# List of container registries, separated by commas
ab_container_search_registry: "'docker.io', 'registry.fedoraproject.org', 'quay.io', 'registry.access.redhat.com', 'registry.centos.org'"
# List of users that can use rootless mode:
ab_users: []
Dependencies
None.
Example Playbook
---
# file: roles/ansible-bender/tests/test.yml
- hosts: all
vars:
ab_users:
- your_username
roles:
- { role: coglinev3.ansible_bender }
Example of using ansible-bender
Create an image
To test ansible-bender, you need an Ansible playbook first. Create a playbook template with:
ansible-bender init
Next, edit the playbook.yml
file. Modify the ansible_bender dictionary variable and add tasks. Here’s a simple example that creates an nginx container based on Alpine Linux.
---
- name: Containerized version of nginx
hosts: all
vars:
# Configuration for ansible-bender
ansible_bender:
# ansible-bender needs an image with preinstalled Python 3
base_image: python:3-alpine
target_image:
# Command to run by default when starting the container
cmd: "nginx -g \"daemon off;\""
name: bender-nginx
ports: ['80', '443']
working_dir: /var/www/localhost/htdocs
labels:
build-by: "{{ ansible_user }}"
working_container:
volumes:
# Mount this git repo to the working container at /src
- "{{ playbook_dir }}:/src:Z"
tasks:
- name: Install dependencies needed for the bender-nginx project
apk:
name: nginx
state: present
- name: Ensure directory /run/nginx exists
file:
path: /run/nginx
state: directory
mode: '0750'
owner: nginx
group: nginx
Now, you can build this example image with:
ansible-bender build ./playbook.yml
Run the container
Once the image is successfully built, you can start a new container with podman.
podman run -d -p 8080:80 bender-nginx
Lastly, use curl to check if the nginx container is working.
curl http://127.0.0.1:8080/
If you see "404 Not Found," nginx is running correctly—it just means that the requested page is not available.
Version
Release: 1.5.0
License
BSD
Author Information
Copyright © 2024 Cogline.v3.
Installs ansible-bender, a tool which bends containers using Ansible playbooks and turns them into container images.
ansible-galaxy install coglinev3.ansible_bender