Lets create an SSH key pair to automatically push the configuration through the CI/CD pipeline without prompting for a username and password
Login as the gitlab-runner and follow the steps below to create an ssh key
cloud_user@ed26757f4b1c:~$ su gitlab-runner
Password:
gitlab-runner@ed26757f4b1c:/home/cloud_user$ cd ~
gitlab-runner@ed26757f4b1c:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/gitlab-runner/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/gitlab-runner/.ssh/id_rsa.pub
Your public key has been saved in /home/gitlab-runner/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:AO6s6kw4FID/JhCa3I+KwmGze0tZpc7Mx3XL3sa8EmE gitlab-runner@ed26757f4b1c.mylabserver.com
The key's randomart image is:
+---[RSA 3072]----+
|o . |
|+ . . |
|o=. . .. |
|+.o+ o. E |
| o .=o S.... |
|o+.oX.. . o.. |
|*.*= = o o+ |
|=*.. . ...+ |
|++o.. .oo. |
+----[SHA256]-----+
Run this command
cat /home/gitlab-runner/.ssh/id_rsa.pub
Copy the contents of your public key to the clipboard
Go to the Gitlab server
Edit your profile under your user settings to add the public key
Select SSH Keys and add new key
Paste the key in the key text box and click add key
On Server1 cd to the .ssh directory for the gitlab-runner user
Create and save a new file named “config” in the .ssh folder
Type i and copy and paste the following:
Host server2 (1)
Hostname server2 (1)
User git
Port 2222
Preferredauthentications publickey
IdentityFile ~/.ssh/id_rsa
Type esc and then shift + zz to save and exit the file
|
Port 2222 for external connectivity translates to 22 for internal in Docker.
|
1 |
Change the host and hostname values to reflect your Gitlab-CE Server. Use the FQDN. |
Restart sshd
sudo systemctl restart sshd
Run the following command to test connectivity
1 |
Change the hostname values to reflect your Gitlab-CE Server. Use the FQDN. |
cloud_user@ed26757f4b1c:~$ ssh -T git@ccoe-netdev-02.presidio-demo.com
The authenticity of host '[ccoe-netdev-02.presidio-demo.com]:2222 ([10.129.225.179]:2222)' can't be established.
ECDSA key fingerprint is SHA256:JhIlzg5flNje7/tMtzv6e8S/axpapbp38sh61unVBQs.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[ccoe-netdev-02.presidio-demo.com]:2222,[10.129.225.179]:2222' (ECDSA) to the list of known hosts.
Welcome to GitLab, @knorton!
|
If you are recieving an ssh timeout, make sure port 2222 is open to Server2. If you are using a cloud platform you may need to add the Server2 internal IP address to the Server1 host file.
|
Jump back on the Server 1 running the Gitlab-Runner
Login to your new backup repository
Let’s backup the config and upload the configurations to Gitlab
Run the following commands while logged in as the gitlab-runner:
cd ~
cd network-automation/backup/
git init
On the Gitlab-Ce server capture the git remote add origin command from your backup git repository on the GitLab Server
git remote add origin git@server2:knorton/backup.git (1)
1 |
Grab the git remote add origin command from your backup git repository on the GitLab Server |
git config --global user.email "knorton@presidio.com" (1)
git config --global user.name "Ken Norton" (2)
1 |
Change the user email with your email address |
2 |
Change the user name with your name |
git commit -m "Initial commit of backup"
git push --set-upstream origin master
Follow the git commands in the image below
cloud_user@ed26757f4b1c:~$ su gitlab-runner
Password:
gitlab-runner@ed26757f4b1c:/home/cloud_user$ cd ~
gitlab-runner@ed26757f4b1c:/home$ cd network-automation/backup/
gitlab-runner@ed26757f4b1c:~/network-automation/backup$ git init
Initialized empty Git repository in /home/gitlab-runner/network-automation/backup/.git/
gitlab-runner@ed26757f4b1c:~/network-automation/backup$ git remote add origin git@server2:knorton/backup.git (1)
gitlab-runner@ed26757f4b1c:~/network-automation/backup$ git add .
gitlab-runner@ed26757f4b1c:~/network-automation/backup$ git config --global user.email "knorton@presidio.com"
gitlab-runner@ed26757f4b1c:~/network-automation/backup$ git config --global user.name "Ken Norton"
gitlab-runner@ed26757f4b1c:~/network-automation/backup$ git commit -m "Initial commit of backup"
[master (root-commit) cf49541] Initial commit of backup
5 files changed, 324 insertions(+)
create mode 100644 2023-12-17/show_run_clab-Arista-2s-3l-leaf1.txt
create mode 100644 2023-12-17/show_run_clab-Arista-2s-3l-leaf2.txt
create mode 100644 2023-12-17/show_run_clab-Arista-2s-3l-leaf3.txt
create mode 100644 2023-12-17/show_run_clab-Arista-2s-3l-spine1.txt
create mode 100644 2023-12-17/show_run_clab-Arista-2s-3l-spine2.txt
gitlab-runner@ed26757f4b1c:~/network-automation/backup$ git push --set-upstream origin master
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 2 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 2.12 KiB | 1.06 MiB/s, done.
Total 8 (delta 4), reused 0 (delta 0)
To ed26757f4b2c.mylabserver.com:knorton/backup.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
1 |
Grab the git remote add origin command from your backup git repository on the GitLab Server |