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 """ cd /var/jenkins_home/ansible chmod +x /var/jenkins_home/ansible/inventory/inventory.sh /var/jenkins_home/ansible/inventory/inventory.sh ${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 """ } } }