Gitlab-ci - multiple images, one before_script per stage and install dependency

Hello!
I have some questions related to gitlab-ci

  1. How can I use more than one image? Maybe one image per stage or depend on job name?
####  How use one image for JobA1 and JobA2  

JobA1:
    stage: Test
    script:
        - ansible-playbook testa1.yml

JobA2:
    stage: Test
    script:
        - ansible-playbook testa2.yml

####  How use other image to JobB1 and JobB2  

JobB1:
    stage: Test
    script:
        - ansible-playbook testa1.yml

JobB2:
    stage: Test
    script:
        - ansible-playbook testa2.yml
  1. How can I use before_script depends on stage?
  2. How can I install dependency to image? I tried do it like that

before_script:
- apt update
- apt install software-properties-common
- apt-add-repository --yes --update ppa:ansible/ansible
- apt install ansible

I see in the log that the commands from before_script will execute, but when installing ansible the installation must be confirmed - how can I do it? Alternatively, is there a better way to install ansible to the image?

0 upgraded, 73 newly installed, 0 to remove and 12 not upgraded.
Need to get 24.8 MB of archives.
After this operation, 112 MB of additional disk space will be used.
Do you want to continue? [Y/n] Abort.

Best Regards,
BElluu

Hello,

Did you find a solution for all your questions ?

You can specify the image keyword in every job:

job1:
  image: image1

job2:
  image: image2

You cannot, but you can use anchors to do not repeat the code: CI/CD YAML syntax reference | GitLab

This is up to the type of image, of course. For systems based on apt, you can do apt-get install -y ansible