Files
jenkinsfiles/Jenkinsfile.cd-to-iso

75 lines
2.8 KiB
Plaintext

pipeline {
agent any
// Define parameters
parameters {
string(name: 'host_ip', description: 'Target System Address')
booleanParam(name: 'eject_drive', defaultValue: false, description: 'Check this option to eject disk at appropriate times')
booleanParam(name: 'create_iso', defaultValue: false, description: 'Check this option to perform the extraction, leave unchecked for a test run')
string(name: 'disk_drive', defaultValue: '/dev/sr0', description: 'Local DVD Drive device path')
string(name: 'destination_path', defaultValue: '/mnt', description: 'Path to store ISO files')
}
environment {
ANSIBLE_FORCE_COLOR = '1'
}
options {
ansiColor('xterm')
}
stages {
stage('Generate Inventory File') {
steps {
// Generate the dynamic inventory file
sh """
jenkins_group=\$(echo ${env.BUILD_USER_GROUPS} | sed 's/,/\\n/g' | grep -v \$SERVER_SUBNET_GROUP | grep Jenkins | head -n 1)
jenkins_subnet_group=\$(echo ${env.BUILD_USER_GROUPS} | sed 's/,/\\n/g' | grep -e authenticated -e \$SERVER_SUBNET_GROUP | sort -rf | head -n 1)
jenkins_user=\$(echo ${env.BUILD_USER})
cd /var/jenkins_home/ansible
chmod +x /var/jenkins_home/ansible/inventory/inventory.sh
/var/jenkins_home/ansible/inventory/inventory.sh -s -a \$jenkins_subnet_group -g \$jenkins_group -u \$jenkins_user -i ${params.host_ip}
"""
}
}
stage('Ansible Playbook') {
steps {
//Run the cosmos-base ansible playbook
// /workspace/ansible/playbooks/cosmos-base.yaml
sh """
echo ${params.host_ip}
hash=\$(echo -n ${params.host_ip} | md5sum | cut -c 1-8)
inventory_file="/var/jenkins_home/ansible/.inv/inventory-\$hash.yml"
cd /var/jenkins_home/ansible
ansible-playbook -i \$inventory_file /var/jenkins_home/ansible/playbooks/cd-to-iso.yaml \
--ssh-common-args='-o StrictHostKeyChecking=no' \
--extra-vars "disk_drive=${params.disk_drive} eject_drive=${params.eject_drive} \
destination_path=${params.destination_path} create_iso=${params.create_iso}"
"""
}
}
}
post {
always {
// Remove dynamic Inventory file
sh """
hash=\$(echo -n "${params.host_ip}" | md5sum | cut -c 1-8)
inventory_file="/var/jenkins_home/ansible/.inv/inventory-\$hash.yml"
rm \$inventory_file
"""
}
}
}