- Start Jenkins server, connect using SSH inside Jenkins Master.
$ sudo apt update -y
$ sudo apt install ansible -y
-
Visit Jenkins Dashboard, Install new plugin "Ansible Plugin" Use "Install without Restart"
-
From Jenkins dashboard, goto "Global Tool Configuration" > Setup path for Ansible "/usr/bin"
-
Create an additional virtual machine in same VPC / VNet as jenkins master. Lets call it "Sample1"
-
Use SSH connection with Jenkins Master, to create a new SSH Keypair and Use "private-key" in Jenkins credentials and public-key tobe copied to "Sample1"
## Become JENKINS user
$ sudo -i
$ su - jenkins
$ mkdir temp
$ ssh-keygen
Path for Public Key: $PWD/temp/id_rsa.pub
Pass Phrase: <ENTER>
$ cd temp
$ ls
## Lets copy PUBLIC Key to Sample1
## Private IP of Sample1 : 10.0.1.5
$ ssh-copy-id -i $PWD/temp/id_rsa.pub [email protected]
Accept the server thumbprint: YES
## Change File permissions of SSH Keys
$ chmod 600 id_rsa
$ chmod 600 id_rsa.pub
## Test the connectivity with 10.0.1.5
$ ssh -i $PWD/temp/id_rsa [email protected]
## You should be now connected
$ exit
## Display contents of PRIVATE KEY on Screen
$ cat id_rsa
## Just COPY entire private key
-
Open Jenkin dashboard > Manage Credentials > Global Credential Manager "Jenkins" > Add Credentials
Choose option "SSH Username with private key"
ID: webhostid Description: SSH Credentials for Web Host Username: mahendra Private Key: Enter Directly PASTE the key copied from SSH Terminal
Save the credentials
-
Login in GitHub and FORK this repository 'https://github.com/mahendra-shinde/jenkins-ansible-javaapp'
-
From inside the remote server, get private ip address (Use AWS Console or Azure Portal)
$ hostname -i
-
Get inside the FORKED repository (create in step#7) and edit file 'deployment/evniornment.ini'
Replace these line:
[webhosts] 10.0.1.5 ansible_user=mahendra
With these [Please replace 172.17.9.2 with Private IP of Your VM, and 'azureuser' with Username of VM ]:
[webhosts] 172.17.9.2 ansible_user=azureuser
-
After making changes, just use "Commit " button.
-
Modify the Jenkinsfile inside your repository and Update line #11. You need to replace repository URL. Then use "Commit" button.
-
Login in Jenkins Dashboard, and create a new "Pipeline Project"
Description: Ansible integration demo Pipeline: Pipeline from SCM SCM: Git Repository URL: YOUR-REPO-URL
-
Save the Job, and use "Build Now"
-
Verify the build console and once build is SUCCESS, use PUBLIC-IP of Target VM.
http://PUBLIC-IP:8080/