How to install package in build stage and use the installed package during Deploy stage using CICD pipeline

Subject: Gitlab CICD
Hi,
I would like to install missing package at the time of build stage in docker container and use the installed package in other stages like deploy, package or production
Appreciate your help
Below is the code sample

From:.gitlab-ci.yml
image: alpine
before_script:

  • apk add ansible

buildJob1:
stage: build
script:
- echo “build stage”

packageJob2:
stage: package
script:
- ansible --version
- echo “Hello package1”