Accessing an ansible role from inside a Docker-In-Docker jenkins build agent in Kubernetes

7/16/2021

I have my controller jenkins node running in kubernetes via kubernetes plugin and I provision a pod with 2 build containers to handle all of my stage builds, the actual operations are done using ansible playbooks. We are using a combination of Jenkinsfiles and groovy scripted pipeline templates to do the builds. Currently, I can only load a playbook into the agent workspace via this line:

writeFile file: "./unit_integration_tests.yml",text:libraryResource("ansible/playbooks/build_playbooks/unit_integration_tests.yml")

And how I call this ansible playbook to run it:

stage("Run Unit/Integration Tests") {
                    container('dev-dotnet-core-builder') {
                        withEnv(["SERVICE_NAME=${SERVICE_NAME}"]) {
                            ansiblePlaybook colorized: false, installation: 'ansible', playbook: 'unit_integration_tests.yml'
                        }
                    }
                }

For more context on the agent pod, here is the pod template I am using:

def call(body) {
    // evaluate the body block, and collect configuration into the object
    def pipelineParams= [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = pipelineParams
    body()

    podTemplate(label: 'jenkins-dind-microservices-builder',
        containers: [containerTemplate(name: 'infra-devops', image: 'docker.mycompany.com/infra/infra-devops-env:pipelineV1', command: 'cat', ttyEnabled: true, privileged: true),
        containerTemplate(name: 'dev-dotnet-core-builder', image: 'docker.mycompany.com/dev/dev-dotnet-core-builder:0.0.22', command: 'cat', ttyEnabled: true, privileged: true)],
        imagePullSecrets: ["docker"],
        volumes: [hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')]
    )

I have to do a writeFile on each playbook I plan on using throughout the build and all the stages. I would like to do some refactoring where I move some playbook logic into an ansible role. The problem is with ansible roles, they are spread out in a bunch of folders. I cannot copy a directory from the shared lib via this method (writeFile). Is there any way that I can make a folder from my shared lib resources folder available to this agent build pod? perhaps maybe I need some groovy helper to do this? Thank you in advance for any help on this matter.

-- Noah Dlugoszewski
ansible
docker
docker-in-docker
jenkins
kubernetes

0 Answers