JuniorPenguin
JuniorPenguin

Reputation: 145

Using gitlab, how do I trigger CI to use a different YAML file for ansible?

I have a hypothetical .gitlab-ci.yml file:

stages:
- apply
run ansible playbook:
  stage: apply
  image: docker.example.com/my-ansible:2.8
  scripts:
  - ansible-playbook -i ./some-inventory -v playbook.yml

Here's what I'm trying to accomplish - when I upload new-inventory file and playbook file, I want the script to run ansible with those new file names. Is there a way to make the inventory and playbook yaml files into a variable so it picks up new files in the inventory?

So it might look like this:

stages:
- apply
run ansible playbook:
  stage: apply
  image: docker.example.com/my-ansible:2.8
  scripts:
  - ansible-playbook -i ./latest-uploaded-inventory -v latest-uploaded-playbook.yml

Upvotes: 0

Views: 711

Answers (1)

Shmuel
Shmuel

Reputation: 1678

If i'm understood what you wrote correctly
You are trying to run every playbook in the repo that changed in the latest commit.

So this should do it.

Make sure you installed git in the docker image.

This command returns list of all files changed in the latest commit.
git diff --name-only HEAD^..HEAD

image: alpine

before_script:
  - apk add git # If your image not contains git.

build1:
  stage: build
  script:
    - playbooks=""
    # add all changed files in latest commit to array.
    - for playbook in $(git diff --name-only HEAD^..HEAD); do playbooks="${playbooks} $playbook"; done; 
    # echo each file name, in your case change to ansible command.
    - for playbook in $playbooks; do echo $playbook; done; 

Here is a link to gitlab build job console output with result of the above .gitlab-ci.yml

Good luck!

Upvotes: 1

Related Questions