cleanup
All checks were successful
/ build-docker-image (push) Successful in 10s

This commit is contained in:
2025-11-16 22:44:06 +00:00
parent d701fdb2a4
commit 07058cc39b
6 changed files with 14 additions and 221 deletions

View File

@@ -1,5 +1,10 @@
on:
push
push:
branches:
- main
schedule:
- cron: '@weekly' # once per week
jobs:
build-docker-image:

View File

@@ -1,89 +1,11 @@
<!-- omit in toc -->
# Ansible
Welcome to ansible kb git repo.
<!-- omit in toc -->
## Table of Contents
- [Docker](#docker)
- [Environment variables](#environment-variables)
- [Gitea actions](#gitea-actions)
- [Build local](#build-local)
## Docker
check [Docker Image](https://git.limbosolutions.com/kb/-/packages/container/ansible/latest).
```
docker pull git.limbosolutions.com/kb/ansible:latest
docker run --rm \
-e ANSIBLE_INVENTORY="${ANSIBLE_PLAYBOOK_INVENTORY}" \
-e ANSIBLE_PRIVATE_KEY="${ANSIBLE_PLAYBOOK_PRIVATE_KEY}" \
-e ANSIBLE_REMOTE_USER="${ANSIBLE_PLAYBOOK_REMOTE_USER}" \
-e ANSIBLE_HOST_KEY_CHECKING=false \
-v ${VOLUME_PATH}:/project \
git.limbosolutions.com/kb/ansible:latest
```
### Environment variables
All [ansible official](https://docs.ansible.com/ansible/latest/reference_appendices/config.html) environment variables.
- ANSIBLE_HOST_KEY_CHECKING (ansible official)
- ANSIBLE_BECOME_PASS (ansible official)
- ANSIBLE_REMOTE_USER
- ANSIBLE_INVENTORY
- ANSIBLE_PROJECT_DIR
- ANSIBLE_HOST_KEY_CHECKING
### Gitea actions
Example executing ansible playbook using gitea actions.
``` yaml
name: deploy host
on:
push:
paths:
- "ansible/**"
- ".gitea/workflows/**host**.yml"
jobs:
Deploy:
runs-on: "homesrv-deploy"
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: setup ssh
run: |
echo "${{ secrets.HOST_ANSIBLE_PRIVATE_KEY }}" > ${GITHUB_WORKSPACE}/ssh-key &&
chmod 600 ${GITHUB_WORKSPACE}/ssh-key
- name: Run Ansible Playbook
run: |
docker run --rm \
-e ANSIBLE_PRIVATE_KEY_FILE="${GITHUB_WORKSPACE}/ssh-key" \
-e ANSIBLE_REMOTE_USER="${{ secrets.HOST_ANSIBLE_REMOTE_USER }}" \
-e ANSIBLE_INVENTORY="${{ secrets.HOST_ANSIBLE_INVENTORY }}" \
-e ANSIBLE_PROJECT_DIR="${GITHUB_WORKSPACE}/ansible" \
-e ANSIBLE_HOST_KEY_CHECKING=false \
-e ANSIBLE_BECOME_PASS=${{ secrets.HOST_ANSIBLE_BECOME_PASS }} \
--volumes-from ${{ env.JOB_CONTAINER_NAME }} \
git.limbosolutions.com/kb/ansible \
```
### Build local
``` bash
docker build docker-f docker/Dockerfile -t ${IMAGE_NAME}
docker pull git.limbosolutions.com/kb/ansible:latest
```
[check script](./docker/docker-run-ansible-local.sh) for more information on building and running local.

View File

@@ -1,18 +1,10 @@
FROM ubuntu:latest
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y
RUN apt-get install -y gcc python3-dev
RUN apt-get install -y openssh-client
RUN apt-get install python3-pip -y
RUN apt-get install python3-virtualenv -y
RUN pip3 install ansible --break-system-packages
RUN pip3 install ansible-runner --break-system-packages
FROM alpine:latest
RUN mkdir /project
RUN mkdir /scripts
RUN mkdir /data
RUN echo "build started.."
COPY scripts /scripts
COPY playbook-sample-project /project
# Install Ansible
RUN apk add --no-cache ansible
# Verify installation
RUN ansible --version
ENTRYPOINT ["python3", "/scripts/run.py"]

View File

@@ -1,17 +0,0 @@
#/bin/bash
source .local/docker/.env
docker build docker \
-f docker/Dockerfile \
-t ${IMAGE_NAME}
docker run --rm \
-e ANSIBLE_INVENTORY="${ANSIBLE_PLAYBOOK_INVENTORY}" \
-e ANSIBLE_PRIVATE_KEY="${ANSIBLE_PLAYBOOK_PRIVATE_KEY}" \
-e ANSIBLE_REMOTE_USER="${ANSIBLE_PLAYBOOK_REMOTE_USER}" \
-e ANSIBLE_HOST_KEY_CHECKING=false \
-v ${ANSIBLE_PLAYBOOK_PROJECT_VOLUME_DIR}:/project $IMAGE_NAME

View File

@@ -1,9 +0,0 @@
- name: Hello from ansible
hosts: all
tasks:
- name: Ping my hosts
ansible.builtin.ping:
- name: Print message
ansible.builtin.debug:
msg: Hello world

View File

@@ -1,100 +0,0 @@
import os
import sys
from ansible_runner import Runner, RunnerConfig
import subprocess
"""
https://docs.ansible.com/ansible/latest/cli/ansible-playbook.html
usage: ansible-playbook [-h] [--version] [-v] [--private-key PRIVATE_KEY_FILE]
[-u REMOTE_USER] [-c CONNECTION] [-T TIMEOUT]
[--ssh-common-args SSH_COMMON_ARGS]
[--sftp-extra-args SFTP_EXTRA_ARGS]
[--scp-extra-args SCP_EXTRA_ARGS]
[--ssh-extra-args SSH_EXTRA_ARGS]
[-k | --connection-password-file CONNECTION_PASSWORD_FILE]
[--force-handlers] [--flush-cache] [-b]
[--become-method BECOME_METHOD]
[--become-user BECOME_USER]
[-K | --become-password-file BECOME_PASSWORD_FILE]
[-t TAGS] [--skip-tags SKIP_TAGS] [-C] [-D]
[-i INVENTORY] [--list-hosts] [-l SUBSET]
[-e EXTRA_VARS] [--vault-id VAULT_IDS]
[-J | --vault-password-file VAULT_PASSWORD_FILES]
[-f FORKS] [-M MODULE_PATH] [--syntax-check]
[--list-tasks] [--list-tags] [--step]
[--start-at-task START_AT_TASK]
playbook [playbook ...]
"""
def process_private_Keyfile(rc):
if(os.environ.get('ANSIBLE_PRIVATE_KEY') is not None):
with open("/root/ansible_private_key", 'w') as file:
file.write(os.environ.get('ANSIBLE_PRIVATE_KEY'))
file.flush()
subprocess.run(['chmod', '600', '/root/ansible_private_key'])
rc.cmdline_args += "--private-key /root/ansible_private_key"
elif (os.environ.get('ANSIBLE_PRIVATE_KEY_FILE') is not None):
rc.cmdline_args += "--private-key " + os.environ.get('ANSIBLE_PRIVATE_KEY_FILE')
def build_cmdLine_args(rc):
if(rc.cmdline_args is None):
rc.cmdline_args=""
process_private_Keyfile(rc);
if(os.environ.get('ANSIBLE_REMOTE_USER') is not None):
print("---------------------------------------")
print("remote user:")
print(os.environ.get('ANSIBLE_REMOTE_USER'))
print("---------------------------------------")
rc.cmdline_args += " -u " + os.environ.get('ANSIBLE_REMOTE_USER')
if(os.environ.get('ANSIBLE_VERBOSE') is not None):
print("---------------------------------------")
print("remote user:")
print(os.environ.get('ANSIBLE_VERBOSE'))
print("---------------------------------------")
rc.cmdline_args += " -vvv"
def execute_playbook(projectdir):
rc = RunnerConfig(
private_data_dir="/data",
project_dir=projectdir
)
rc.playbook=os.environ.get('ANSIBLE_PLAYBOOK', "site.yml")
rc.inventory=os.environ.get('ANSIBLE_INVENTORY', "127.0.0.1,")
build_cmdLine_args(rc)
if(rc.inventory=="127.0.0.1,"):
rc.cmdline_args += "--limit 127.0.0.1 --connection local"
rc.prepare()
print("---------------------------------------")
print("command:")
print(rc.generate_ansible_command())
print("---------------------------------------")
r = Runner(config=rc)
r.run()
def list_workspace(projectdir):
print("---------------------------------------")
print("project files:" + " " + projectdir)
items=os.listdir(projectdir)
for image in items:
print(image)
print("---------------------------------------")
def main():
projectdir = os.environ.get('ANSIBLE_PROJECT_DIR', "/project")
list_workspace(projectdir)
execute_playbook(projectdir)
main()