pipeline { agent any // Define parameters parameters { //booleanParam(name: 'deploy_iso', defaultValue: true, description: 'Deploy extra ISO boot options - will increase pipeline runtime.') string(name: 'host_ip', defaultValue: '172.20.20.41', description: 'PXE Server IP Address') string(name: 'iface', defaultValue: 'eth1', description: 'Listen interface for PXE') string(name: 'server_ip', defaultValue: '172.29.1.10', description: 'IP address for DHCP server, might not match the target IP such as in the default case where I have two interfaces') string(name: 'router_ip', defaultValue: '172.29.1.10', description: 'IP Address for router') string(name: 'dhcp_subnet', defaultValue: '172.29.1.0', description: 'Default Subnet for DHCP server') string(name: 'dhcp_netmask', defaultValue: '255.255.255.0', description: 'Default netmask for DHCP server') string(name: 'dhcp_start', defaultValue: '172.29.1.50', description: 'DHCP start IP') string(name: 'dhcp_end', defaultValue: '172.29.1.250', description: 'DHCP end IP') booleanParam(name: 'configure_routing', defaultValue: true, description: 'Check this option to configure the PXE server to be the gateway for the dhcp network') string(name: 'inet_iface', defaultValue: 'eth0', description: 'Interface for internet if enabling routing') booleanParam(name: 'refresh_only', defaultValue: true, description: 'Only run the PXE Role to refresh configs') } environment { ANSIBLE_FORCE_COLOR = '1' SATURN_BEHEMOTH = credentials('SATURN_BEHEMOTH') APPS_LIST = 'pxe-server' LINUX_LDAP_PWD = credentials('LINUX_LDAP') AUTHORIZED_KEY = credentials('AUTH_SSH_KEY') matt_public_key = credentials('matt_public_key') matt_private_key = credentials('matt_private_key') cosmos_password = credentials('cosmos_password') cosmos_root_password = credentials('cosmos_root_password') } 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/pxe-server.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 { //Run the pxe-server ansible playbook // /workspace/ansible/playbooks/pxe-server.yaml 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/pxe-server.yaml" extra_vars=\$(echo " \ new_hostname=debian-pxe.home.cosmos \ saturn_behemoth=${SATURN_BEHEMOTH} \ authorized_key=${AUTHORIZED_KEY} \ rename_host=true apps_list=${APPS_LIST} \ matt_public_key='${env.matt_public_key}' \ cosmos_password='${env.cosmos_password}' \ cosmos_root_password='${env.cosmos_root_password}' listen_interface=${params.iface} \ internet_interface=${params.inet_iface} \ dhcp_subnet=${params.dhcp_subnet} \ dhcp_netmask=${params.dhcp_netmask} \ dhcp_start=${params.dhcp_start} \ dhcp_end=${params.dhcp_end} \ server_ip=${params.server_ip} \ router_ip=${params.router_ip} \ configure_routing=${params.configure_routing} \ refresh_only=${params.refresh_only} \ " | 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 matt_private_key='${env.matt_private_key}' " """ } } } 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 """ } } }