use Ansible to connect over JumpServer within Gitlab CI

Viewed 1439

I want to configure private VMs which are reachable over bastion host. So I am using ubuntu 20.04 image, then I install python and all related libraries and after that I create virtual environment and install Ansible.

stages:
  - configure

configure:
  image: ubuntu:20.04
  stage: configure
  before_script:
    - apt-get -y update
    - addgroup deploy
    - mkdir /opt/.env
    - chgrp -R deploy /opt/.env    
    - chmod -R 770 /opt/.env
    - apt install -y build-essential libssl-dev libffi-dev python3-dev
    - apt install -y python3-pip
    - apt install -y python3-venv
    - apt install git -y
    - python3.8 -m venv /opt/.env/basic --system-site-packages
    - source /opt/.env/basic/bin/activate
    - pip install wheel
    - pip install ansible
  script:
    - source /opt/.env/basic/bin/activate
    - echo $my_ssh_key >> .ssh/my_ssh_key.pem
    - chmod 400 .ssh/my_ssh_key.pem
    - mv .ssh /root/.ssh,
    - mv .ansible.cfg /root/.ansible.cfg
    - echo $(ansible --version)
    - ansible-playbook ansible/playbooks/start.yml


my repo structure is as following:

.ssh
   |_ ansible.cfg

ansible
   |_ playbooks

.ansible.cfg

.gitlab-ci.yml

the content of .ansible.cfg:

[ssh_connection]
ssh_args = -F /root/.ssh/ansible.cfg -o ControlMaster=auto -o ControlPersist=60m
control_path = /root/.ssh/ansible-%%r@%%h:%%p

the content of .ssh/.ansible.cfg:

Host BASTION
    HostName x.x.xx.x
    User ec2-user
    IdentityFile /root/.ssh/my_ssh_key.pem
    ControlMaster auto
    ControlPath /root/.ssh/ansible-%r@%h:%p
    ControlPersist 5m
    StrictHostKeyChecking=no
    UserKnownHostsFile=/dev/null


Host 10.*
    User ec2-user
    IdentityFile /root/.ssh/my_ssh_key.pem
    stricthostkeychecking=no
    ProxyJump BASTION

However, I recive the following error when the ansible script excuted:

 fatal: [10.1.8.58]: UNREACHABLE! => {
     "changed": false,
     "msg": "Failed to connect to the host via ssh: kex_exchange_identification: Connection closed by remote host",
     "unreachable": true
 }

adding user to the ansible run command did not change anything (ansible-playbook ansible/playbooks/start.yml -u ec2-user)

Have anyone faced a similar issue? I am out of idea right now, I'll appreciate any ideas.

1 Answers
Related