Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/ansibleplaybook-docker-execution/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GOLANG_VERSION=1.24
39 changes: 39 additions & 0 deletions examples/ansibleplaybook-docker-execution/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

DOCKER_COMPOSE_BINARY := $(shell docker compose version > /dev/null 2>&1 && echo "docker compose" || (which docker-compose > /dev/null 2>&1 && echo "docker-compose" || (echo "docker compose not found. Aborting." >&2; exit 1)))

PROJECT_NAME := go-ansible-$(shell basename ${PWD})

# dafault target
.DEFAULT_GOAL: help

help: ## Lists available targets
@echo
@echo "Makefile usage:"
@grep -E '^[a-zA-Z1-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[1;32m%-20s\033[0m %s\n", $$1, $$2}'
@echo

build: ## Build the Docker compose environment
@$(DOCKER_COMPOSE_BINARY) build

up: ## Create and start containers
@$(DOCKER_COMPOSE_BINARY) --project-name $(PROJECT_NAME) up --detach --build

down: ## Stop and remove containers, networks, and volumes
@$(DOCKER_COMPOSE_BINARY) --project-name $(PROJECT_NAME) down --volumes --remove-orphans --timeout 3

restart: down up ## Restart the containers

ps: ## List containers
@$(DOCKER_COMPOSE_BINARY) --project-name $(PROJECT_NAME) ps

logs: ## Show all logs
@$(DOCKER_COMPOSE_BINARY) --project-name $(PROJECT_NAME) logs

attach-ansible: ## Attach to the ansible container
@$(DOCKER_COMPOSE_BINARY) --project-name $(PROJECT_NAME) exec --workdir /code/examples/$$(basename $$(pwd)) ansible /bin/sh

project-name: ## Show the project name
@echo $(PROJECT_NAME)

run: ## Run the playbook
@$(DOCKER_COMPOSE_BINARY) --project-name $(PROJECT_NAME) run --build --rm --workdir /code/examples/$$(basename $$(pwd)) ansible go run $$(find . -type f -name '*.go')
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"context"
"fmt"
"os"

"github.com/apenella/go-ansible/v2/pkg/execute"
"github.com/apenella/go-ansible/v2/pkg/execute/result/transformer"
"github.com/apenella/go-ansible/v2/pkg/playbook"
"github.com/docker/docker/client"
)

func main() {

// ctx := context.Background()
apiClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
defer apiClient.Close()

executable := NewDockerExec(apiClient)

ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
Connection: "local",
Inventory: "127.0.0.1,",
}

playbookCmd := playbook.NewAnsiblePlaybookCmd(
playbook.WithPlaybooks("site.yml"),
playbook.WithPlaybookOptions(ansiblePlaybookOptions),
)

executor := execute.NewDefaultExecute(
execute.WithCmd(playbookCmd),
execute.WithExecutable(executable),
execute.WithTransformers(
transformer.Prepend("ansibleplaybook-docker-executor example"),
),
)

err = executor.Execute(context.TODO())
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
15 changes: 15 additions & 0 deletions examples/ansibleplaybook-docker-execution/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---

services:
ansible:
build:
context: docker/ansible
args:
- golang_version=${GOLANG_VERSION}
command: ["tail", "-f", "/dev/null"]
# command: ["ansible-playbook", "--help"]
volumes:
- ../..:/code
working_dir: /code
## Set the init flag to true lets the process 1 to reap all the zombie processes
init: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
ARG golang_version=1.23

FROM golang:${golang_version}-bookworm AS golang

FROM python:3.12-alpine3.19

RUN apk add --update --no-cache \
openssh-client \
git \
&& rm -rf /var/cache/apk/*

RUN pip3 install -U pip setuptools \
&& pip3 install --no-cache-dir \
setuptools-rust \
cryptography \
# Required library to execute ansible community.general.dig plugin
dnspython \
ansible \
&& ln /usr/local/bin/ansible-playbook /usr/bin/ansible-playbook

COPY --from=golang /usr/local/go /usr/local/go

# Configure Go
ENV GOROOT /usr/local/go
ENV PATH /usr/local/go/bin:/go/bin:$PATH

RUN mkdir -p ${GOPATH}/src ${GOPATH}/bin
Loading
Loading