Skip to content

Commit

Permalink
Merge pull request #1 from TimGrt/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
TimGrt committed Jul 5, 2024
2 parents 8d0bac3 + 95893f0 commit f1be02a
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 99 deletions.
32 changes: 5 additions & 27 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,50 +1,28 @@
---
name: Docker Build and Publish

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:

test:
name: Test
runs-on: ubuntu-latest

steps:
- name: Checkout repository.
uses: actions/checkout@v2

- name: Test building image.
run: docker build -t docker-ansible .

- name: Run the built image.
run: docker run --name test-container -d --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro docker-ansible

- name: Verify Ansible is accessible in the built image.
run: docker exec --tty test-container env TERM=xterm ansible --version

publish:
name: Publish
publish_to_dockerhub:
name: Publish to DockerHub
runs-on: ubuntu-latest
needs: test

steps:
- name: Checkout repository.
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Login to DockerHub.
uses: docker/login-action@v1
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push image.
uses: docker/build-push-action@v2
uses: docker/build-push-action@v6
with:
context: ./
file: Dockerfile
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/
4 changes: 4 additions & 0 deletions .markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# MD013/line-length - Line length
MD013:
# Number of characters, basically allowing very long lines
line_length: 1000
27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-merge-conflict
name: Check for merge conflict markers
- id: trailing-whitespace
name: Remove trailing whitespaces
args: [--markdown-linebreak-ext=md]
- id: no-commit-to-branch
name: Ensure committing to allowed branch
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.41.0
hooks:
- id: markdownlint
name: Lint Markdown files
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.28.6
hooks:
- id: check-github-workflows
name: Check Github workflows
- repo: https://github.com/hadolint/hadolint
rev: v2.12.0
hooks:
- id: hadolint-docker
name: Lint Dockerfile
entry: ghcr.io/hadolint/hadolint:latest-alpine hadolint
24 changes: 11 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
FROM registry.access.redhat.com/ubi8/ubi-init
# hadolint global ignore=DL3007,DL3033
FROM registry.access.redhat.com/ubi8/ubi-init:latest
LABEL maintainer="Tim Gruetzmacher"
ENV container=docker

RUN dnf install -y python38 python38-pip sudo

# Upgrade pip to latest version.
RUN pip3 install --no-cache-dir --upgrade pip

# Install Ansible via pip.
RUN pip3 install --no-cache-dir ansible cryptography

# Install Ansible inventory file.
RUN mkdir -p /etc/ansible \
&& printf "[local]\nlocalhost ansible_connection=local\n" > /etc/ansible/hosts
# Install requirements.
RUN yum -y install rpm dnf-plugins-core \
&& yum -y update \
&& yum -y install \
initscripts \
sudo \
which \
hostname \
&& yum clean all

# Create `ansible` user with sudo permissions
ENV ANSIBLE_USER=ansible
Expand Down
121 changes: 62 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,77 +1,80 @@
# RHEL 8 Ansible Test Image

RHEL 8 Docker container based on UBI 8 for Ansible playbook and role testing.
RHEL 8 Docker container for Ansible playbook and role testing.
This container is used to test Ansible roles and playbooks (e.g. with molecule) running locally inside the container.
A non-priviledged user `ansible` is created with password-less sudo configured.
A user `ansible` is created with password-less sudo configured.

[![Docker Build and Publish](https://github.com/TimGrt/docker-rhel8-ansible/actions/workflows/ci.yml/badge.svg)](https://github.com/TimGrt/docker-rhel8-ansible/actions/workflows/ci.yml) ![Docker Pulls](https://img.shields.io/docker/pulls/timgrt/rhel8-ansible) ![CodeFactor Grade](https://img.shields.io/codefactor/grade/github/timgrt/docker-rhel8-ansible/main)

## Tags

The following tags are available:

- `latest`: Latest stable version of Ansible on Python 3.8
## How to Build

To build the image on your own locally, do the following:
If you need to build the image on your own locally, do the following:

1. [Install Docker](https://docs.docker.com/engine/installation/).
2. Clone the repository and `cd` into this directory.
3. Run `docker build -t rhel8-ansible .`
1. Install container runtime, I use [Podman](https://podman.io/docs/installation) but you may also use [Docker](https://docs.docker.com/engine/installation/).
2. Clone the repository and `cd` into this directory.
3. Run `podman build -t rhel8-ansible .`

## How to Use Standalone

1. [Install Docker](https://docs.docker.com/engine/installation/).
2. Pull this image from Docker Hub or use the image you built earlier, e.g. called `rhel8-ansible:latest` for the next step.
```bash
docker pull timgrt/rhel8-ansible:latest
```
3. Run a container from the image. To test my Ansible roles, I add in a volume mounted from the current working directory with ``--volume=`pwd`:/etc/ansible/roles/role_under_test:ro``.
```bash
docker run --detach --privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro timgrt/rhel8-ansible:latest
```
4. Use Ansible inside the container.
```bash
docker exec --tty [container_id] env TERM=xterm ansible --version
```
```bash
docker exec --tty [container_id] env TERM=xterm ansible-playbook /path/to/ansible/playbook.yml
```
1. Install container runtime, I use [Podman](https://podman.io/docs/installation). You may also use [Docker](https://docs.docker.com/engine/installation/), but you'll have to adjust some configurations.
2. Pull this image from Docker Hub or use the image you built earlier, e.g. called `rhel8-ansible:latest` for the next step.

```bash
podman pull timgrt/rhel8-ansible:latest
```

3. Run a container from the image.

```console
podman run --detach --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro --name instance timgrt/rhel8-ansible:latest
```

4. Use the container in your inventory with the *podman* connection plugin.

```ini
[test]
instance ansible_connection=podman ansible_user=ansible
```

Using the `ansible` user inside the container to be able to test with become.

You'll need to have the Ansible collection `containers.podman` installed.

```console
ansible-galaxy collection install containers.podman
```

## How to Use with Molecule

1. [Install Docker](https://docs.docker.com/engine/installation/).
2. [Install Molecule](https://molecule.readthedocs.io/en/latest/installation.html).
3. Add Image in molecule.yml.

For example:
```yaml
---
driver:
name: docker
platforms:
- name: rhel8
image: timgrt/rhel8-ansible:latest
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
command: "/usr/sbin/init"
pre_build_image: true
provisioner:
name: ansible
config_options:
defaults:
interpreter_python: auto_silent
callback_whitelist: profile_tasks, timer, yaml
stdout_callback: yaml
ssh_connection:
pipelining: false
inventory:
host_vars:
rhel8:
ansible_user: ansible
```
1. [Install Docker](https://docs.docker.com/engine/installation/).
2. [Install Molecule](https://ansible.readthedocs.io/projects/molecule/installation/).
3. Add Image in `molecule.yml`.

```yaml
---
driver:
name: podman
platforms:
- name: rhel8
image: docker.io/timgrt/rhel8-ansible:latest
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
command: "/usr/sbin/init"
pre_build_image: true
provisioner:
name: ansible
config_options:
defaults:
interpreter_python: auto_silent
callback_result_format: yaml
ssh_connection:
pipelining: false
inventory:
host_vars:
rhel8:
ansible_user: ansible
```

## Author

Created 2021 by Tim Grützmacher
Created 2022 by Tim Grützmacher, inspired by [Jeff Geerling](https://www.jeffgeerling.com/)

0 comments on commit f1be02a

Please sign in to comment.