Build custom ansible container image

In this section we are going to build a custom ansible container image using a Dockerfile and store the image in our container registry so that the CI/CD pipeline can use it for future runs as we expand our pipeline.

Select Code then Repository. Select the .gitlab-ci.yml file. Select the blue Edit selection list and then choose Edit single file.

In the stages section, add a new stage called container so that it looks like the following:

stages:
  - registry
  - container

Next, copy and paste the following block at the end of the file to define the staging phase:

build_container:
  stage: container
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'
      when: always
  tags:
    - ansible_f5
  image: ${CI_REGISTRY}/${CI_PROJECT_PATH}/podman:latest
  variables:
    IMAGE_TAG: ${CI_REGISTRY}/${CI_PROJECT_PATH}/ansible:latest
  script:
  - env
  - podman login --tls-verify=false -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  - podman build --tls-verify=false -t $IMAGE_TAG --build-arg REGISTRY="$CI_REGISTRY" --build-arg PROJECT="$CI_PROJECT_PATH" --build-arg PROJECT_ID="$CI_PROJECT_ID" --build-arg TOKEN="$ACCESS_TOKEN" --build-arg PUB_KEY="$SSH_PUB_KEY" --build-arg PRIV_KEY="$SSH_PRIV_KEY" .
  - podman push --tls-verify=false $IMAGE_TAG

At the top right corner, select Commit changes to update the file. You will be prompted for a commit message. Leave the default value and select the Commit changes button. This will trigger the CI/CD pipeline again.