diff --git a/.github/workflows/.DS_Store b/.github/workflows/.DS_Store index 1a77fd3..5008ddf 100644 Binary files a/.github/workflows/.DS_Store and b/.github/workflows/.DS_Store differ diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..12a0d36 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,28 @@ +--- + +name: ci +on: + push: + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: 'Print docker version information' + run: | + docker --version + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: 'Login to Docker Hub' + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: 'Build and push' + uses: docker/build-push-action@v5 + with: + push: true + tags: thehedhly/ansible:latest + context: . + file: Dockerfile diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml deleted file mode 100644 index 1542bf0..0000000 --- a/.github/workflows/docker.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: Docker Image CI -on: - push: - branches-ignore: - - "master" -jobs: - build: - runs-on: ${{ matrix.os }} - strategy: - matrix: - include: - - os: ubuntu-20.04 #ubuntu-latest - - os: ubuntu-18.04 - steps: - # Checkout - - uses: actions/checkout@v2 - with: - # Full git history is needed to get a proper list of changed files within `super-linter` - fetch-depth: 0 - # Verbose - - name: Print full context - env: - GITHUB_CONTEXT: ${{ toJson(github) }} - run: echo "$GITHUB_CONTEXT" - - name: Print branch and repository - run: echo "Branch = ${GITHUB_REF##*/} | Repository = ${GITHUB_REPOSITORY#*/}" - # Linting - - name: Lint Dockerfile - uses: github/super-linter@v4 - env: - #VALIDATE_ALL_CODEBASE: false - #VALIDATE_DOCKERFILE: true - VALIDATE_MARKDOWN: false - DEFAULT_BRANCH: ${{ github.ref }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - # Build - - name: Build the Docker image - run: docker build . --file Dockerfile --tag ${GITHUB_REPOSITORY#*/}:"${GITHUB_REF##*/}" - # Inspect - - name: Inspect image - run: docker image inspect ${GITHUB_REPOSITORY#*/}:"${GITHUB_REF##*/}" \ No newline at end of file diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml new file mode 100644 index 0000000..1575948 --- /dev/null +++ b/.github/workflows/linter.yml @@ -0,0 +1,20 @@ +--- + +name: Lint Code Base +on: + push: + branches-ignore: [main] + pull_request: + branches: [main] + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: 'Yamllint' + uses: karancode/yamllint-github-action@master + with: + yamllint_file_or_dir: . + yamllint_strict: true + yamllint_comment: false diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml deleted file mode 100644 index 0201cfd..0000000 --- a/.github/workflows/release.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: Release CI -on: - push: - branches: - - master -jobs: - Create-release: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2.2.0 - # Verbose - - name: Print full context - env: - GITHUB_CONTEXT: ${{ toJson(github) }} - run: echo "$GITHUB_CONTEXT" - - name: Print release name - env: - SUPPORTED_IMAGE: ${{ github.event.head_commit.message }} - run: echo "$SUPPORTED_IMAGE" - # # Docker Hub Login - - name: Login to DockerHub - uses: docker/login-action@v1 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - # Docker Hub Push - - name: Build and push - id: docker_build - uses: docker/build-push-action@v2 - with: - context: . - push: true - tags: ${{ github.repository }}:${{ github.event.head_commit.message }} - # Verbose - - name: Print image digest - run: echo ${{ steps.docker_build.outputs.digest }} - # Create Release - - uses: ncipollo/release-action@v1 - with: - tag: ${{ github.event.head_commit.message }} - token: ${{ secrets.G_TOKEN }} \ No newline at end of file diff --git a/.hadolint.yaml b/.hadolint.yaml new file mode 100644 index 0000000..f1701b3 --- /dev/null +++ b/.hadolint.yaml @@ -0,0 +1,5 @@ +--- + +ignored: + - DL3008 + - DL3006 #see https://github.com/hadolint/hadolint/issues/339 diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..7277fba --- /dev/null +++ b/.yamllint @@ -0,0 +1,9 @@ +--- + +extends: default + +rules: + line-length: disable + truthy: + allowed-values: ['true', 'false'] + check-keys: false diff --git a/Dockerfile b/Dockerfile index c4abf25..a2b2ab7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,31 +1,69 @@ -FROM ubuntu:20.04 -LABEL author="Hamza Hedhly" -LABEL version="Ubuntu 20.04 (Focal Fossa)" -LABEL documentation="https://github.com/senjoux" -# Supported Ansible releases: -# - 4.0 (default) -# - 3.0 -# - 2.10 -# see releases https://docs.ansible.com/ansible/devel/roadmap/ansible_roadmap_index.html -ENV ANSIBLE_VERSION=4.0.0 -# if none provided an "ansible" user will be created -ENV ANSIBLE_USER=ansible -# Python3-pip version -ENV PYTHON3_PIP_VERSION=20.0.2-5ubuntu1.5 - -RUN apt-get update --no-install-recommends -y \ - # prepare user - && adduser $ANSIBLE_USER \ - && echo "$ANSIBLE_USER ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers \ - # install Ansible - && apt-get install --no-install-recommends python3-pip=$PYTHON3_PIP_VERSION -y \ - && rm -rf /var/lib/apt/lists/*\ - && python3.8 -m pip install --no-cache-dir ansible==$ANSIBLE_VERSION \ - # other - && echo "export PS1='[\u@\h:\w] $ '" >> /home/$ANSIBLE_USER/.bashrc \ - # self - && ansible --version \ - && python3.8 -m pip list - -ENTRYPOINT ["/bin/bash","-c"] -CMD ["su $ANSIBLE_USER"] \ No newline at end of file +# Before overriding BASE_IMAGE and PYCMD, please consult: +# - https://docs.ansible.com/ansible/latest/reference_appendices/release_and_maintenance.html#ansible-core-support-matrix +# and also maybe (in case want to install ansible community): +# - https://docs.ansible.com/ansible/latest/reference_appendices/release_and_maintenance.html#ansible-community-changelogs +# +ARG BASE_IMAGE="python:3.11.7-slim" +ARG PYCMD="/usr/local/bin/python3.11" +ARG SYS_ZONEINFO="Europe/Berlin" +ARG ANSIBLE_HOME="/usr/share/ansible" +ARG ANSIBLE_GALAXY_CLI_COLLECTION_OPTS="-v" +ARG ANSIBLE_GALAXY_CLI_ROLE_OPTS="" +ARG ANSIBLE_INSTALL_REFS="ansible-core==2.16.0" +# ARG ANSIBLE_INSTALL_REFS="ansible-core" +# ARG ANSIBLE_INSTALL_REFS="ansible==9" +# ARG ANSIBLE_INSTALL_REFS="ansible" +ARG ANSIBLE_USER="thehedhly" + +# Base build stage +FROM $BASE_IMAGE as base +USER root +ARG BASE_IMAGE +ARG PYCMD +ARG ANSIBLE_INSTALL_REFS + +RUN "unlink /etc/localtime \ + && ln -s /usr/share/zoneinfo/$SYS_ZONEINFO /etc/localtime \ + && $PYCMD -m ensurepip \ + && $PYCMD -m pip install --no-cache-dir $ANSIBLE_INSTALL_REFS" +USER guest + +# Galaxy build stage +FROM base as galaxy +ARG ANSIBLE_HOME +ARG ANSIBLE_GALAXY_CLI_COLLECTION_OPTS +ARG ANSIBLE_GALAXY_CLI_ROLE_OPTS +WORKDIR / +COPY requirements.yml . +RUN ansible-galaxy role install $ANSIBLE_GALAXY_CLI_ROLE_OPTS -r requirements.yml --roles-path "$ANSIBLE_HOME/roles"\ + && ANSIBLE_GALAXY_DISABLE_GPG_VERIFY=1 ansible-galaxy collection install $ANSIBLE_GALAXY_CLI_COLLECTION_OPTS -r requirements.yml --collections-path "$ANSIBLE_HOME/collections" + +# Final build stage +FROM base as final +LABEL org.opencontainers.image.created="date and time on which the image was built (string, date-time as defined by RFC 3339)" +LABEL org.opencontainers.image.authors="https://github.com/thehedhly" +LABEL org.opencontainers.image.url="dockerhub url" +LABEL org.opencontainers.image.source="github repository" +LABEL org.opencontainers.image.version="version of the packaged software" +ARG ANSIBLE_HOME +ARG ANSIBLE_USER +ENV ANSIBLE_CONFIG "/home/$ANSIBLE_USER/.ansible.cfg" +# ENV ANSIBLE_HOME = $XANSIBLE_HOME + +COPY --from=galaxy $ANSIBLE_HOME $ANSIBLE_HOME + +RUN useradd -m $ANSIBLE_USER\ + && echo "$ANSIBLE_USER ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers\ + && apt-get update\ + && apt-get install --no-install-recommends -y openssh-client\ + && apt-get install --no-install-recommends -y iputils-ping\ + && apt-get -qq clean\ + && rm -rf /var/lib/apt/lists/* + +USER $ANSIBLE_USER +COPY --chown=$ANSIBLE_USER:$ANSIBLE_USER ansible.cfg $ANSIBLE_CONFIG + +WORKDIR "/home/$ANSIBLE_USER" + +# ENTRYPOINT ["/bin/bash","-c"] +# CMD ["su $ANSIBLE_USER"] diff --git a/LICENSE b/LICENSE index 6c6e033..7c6df84 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 elhedhly +Copyright (c) 2024 H.Hedhly https://github.com/thehedhly Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md deleted file mode 100644 index 7aa63e9..0000000 --- a/README.md +++ /dev/null @@ -1,44 +0,0 @@ -Ansible Logo - -# Docker-Ansible -[![CI][badge-gh-actions]][link-gh-actions] - -## Features -- Ansible control Docker image. -- Custom Ubuntu user creation - -## OS support -| OS | Version | -|------|---------| -| Ubuntu | 20.04 (Focal Fossa) | - -## Ansible releases support: -| Release | -|------| -| 4.0 | -| 3.0 | -| 2.10 | - -## Environment variables - -| Name | Version | default | -|------|---------|---------| -| `ANSIBLE_VERSION` | Ansible version. | 4.0.0 | -| `ANSIBLE_USER` | Custom Ubuntu user name. | ansible | -| `PYTHON3_PIP_VERSION` | Version of the "python3-pip" module to be used for Ansible installation. | 20.0.2-5ubuntu1.5 | - -## Usage -- Build image -``` -docker build -t ansible_control: . -``` -- Run & access container -``` -docker build -t ansible_control: . -``` - - -This project was created by [Hamza Hedhly](https://de.linkedin.com/in/hedhly). - -[badge-gh-actions]: https://github.com/senjoux/docker_ansible/actions/workflows/release.yml/badge.svg -[link-gh-actions]: https://github.com/senjoux/docker_ansible/actions/workflows/release.yml \ No newline at end of file diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..411616e --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,16 @@ +[defaults] +# playbook_dir = ./playbooks +roles_path = ~/.ansible/roles:./roles:/usr/share/ansible/roles +collections_path = ~/.ansible/collections:./collections:/usr/share/ansible/collections +bin_ansible_callbacks = True +callbacks_enabled = ansible.posix.profile_tasks +force_color = True +# Use the YAML callback plugin +stdout_callback = yaml + +nocows = 1 +# cow_selection = random +# cow_selection = small + +[galaxy] +collections_path_warning = False diff --git a/requirements.yml b/requirements.yml new file mode 100644 index 0000000..ef1f14f --- /dev/null +++ b/requirements.yml @@ -0,0 +1,15 @@ +--- + +roles: [] +# roles: + # - name: geerlingguy.go + # version: "1.1.0" + +# collections: [] +collections: + - name: ansible.posix + version: ">=1.5.0" + - name: community.general + version: ">=8.1.0" + - name: community.docker + version: ">=3.5.0"