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 """ set +x 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}) playbook_file="/var/jenkins_home/ansible/playbooks/cd-to-iso.yaml" cd /var/jenkins_home/ansible chmod +x /var/jenkins_home/ansible/inventory/inventory.sh set -x /var/jenkins_home/ansible/inventory/inventory.sh -p \$playbook_file -s -a \$jenkins_subnet_group -g \$jenkins_group -u \$jenkins_user -i ${params.host_ip} """ } } stage('Ansible Playbook') { steps { // Build ISO file with Ansible sh """ set +x 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" playbook_file="/var/jenkins_home/ansible/playbooks/cd-to-iso.yaml" extra_vars=\$(echo " \ disk_drive=${params.disk_drive} \ eject_drive=${params.eject_drive} \ destination_path=${params.destination_path} \ create_iso=${params.create_iso} \ " | tr -s " ") set -x cd /var/jenkins_home/ansible ansible-playbook -i \$inventory_file \$playbook_file \ --ssh-common-args='-o StrictHostKeyChecking=no' \ --extra-vars "\$extra_vars" """ } } } post { always { // Remove dynamic Inventory file sh """ set +x hash=\$(echo -n "${params.host_ip}" | md5sum | cut -c 1-8) inventory_file="/var/jenkins_home/ansible/.inv/inventory-\$hash.yml" set -x rm \$inventory_file """ } } }