Skip to content

Instantly share code, notes, and snippets.

@brunocarvalhodearaujo
Forked from mrhockeymonkey/Jenkinsfile
Created July 21, 2021 16:39
Show Gist options
  • Save brunocarvalhodearaujo/b3fc1fb7b69632fe675e0e1ec484f455 to your computer and use it in GitHub Desktop.
Save brunocarvalhodearaujo/b3fc1fb7b69632fe675e0e1ec484f455 to your computer and use it in GitHub Desktop.
Jenkins pipeline running a remote ansible playbook
#!groovy
pipeline {
agent any
//These params will be displayed for user input when running a build, They are also accepted by the API
parameters {
string(name: 'BUILD_HOSTNAME', description: 'The name of the server to build (from Mdb)')
string(name: 'ILO_IP', description: 'The IP address for the server ilo')
booleanParam(name: 'skipOneView', description: 'Skip the OneView stage?', defaultValue: false)
booleanParam(name: 'skipBuild', description: 'Skip the Build stage?', defaultValue: false)
}
stages {
stage('Init') {
steps {
sh 'git status'
echo "Building $BUILD_HOSTNAME (Ilo: $ILO_IP)"
echo "skipOneView: ${params.skipOneView}"
echo "skipBuild: ${params.skipBuild}"
//The sshagent block will run any ssh aware commands as the user specified
sshagent(['ssh_user']) {
//To run a remote ssh command you will need to setup ssh keys (https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server)
sh "ssh -o StrictHostKeyChecking=no ssh_user@server.net -C \"cd ~/Project; git pull\""
}
}
}
stage('OneView') {
//The when block control is this stage should be run base don the params given
when {
expression {
params.skipOneView == false
}
}
steps {
//The withCredentials block places a username and password securely into the environment
withCredentials([usernamePassword(credentialsId: 'vault_user', passwordVariable: 'VAULTPASS', usernameVariable: 'VAULTUSER')]) {
sshagent(['ssh_user']) {
//Here we invoke ansible remotely using ssh. We use the password retreived from withCredentials to feed to ansible and unlock the vault.
//To do this we first set the password as an environment variable and then used passmgr.py to echo this out
sh "ssh -o StrictHostKeyChecking=no ssh_user@server.net -C \"export VAULTPASS=$VAULTPASS; cd ~/Project; ansible-playbook oneview.yml --vault-password-file passmgr.sh -i inventory.py -l $BUILD_HOSTNAME --extra-vars 'ilo_addr=$ILO_IP'\""
}
}
}
}
stage('Build') {
when {
expression {
params.skipBuild == false
}
}
steps {
withCredentials([usernamePassword(credentialsId: 'vault_user', passwordVariable: 'VAULTPASS', usernameVariable: 'VAULTUSER')]) {
sshagent(['ssh_user']) {
sh "ssh -o StrictHostKeyChecking=no ssh_user@server.net -C \"export VAULTPASS=$VAULTPASS; cd ~/Ansibuild; ansible-playbook build.yml --vault-password-file passmgr.sh -i inventory.py -l $BUILD_HOSTNAME --extra-vars 'ilo_addr=$ILO_IP skip_pxe=yes custom_disk_layout=UEFI_2Disk_General'\""
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment