pipeline { agent any // Define parameters parameters { string(name: 'host_ip', description: 'Target System Address') // string(name: 'new_hostname', description: 'Update Hostname') // reference for later // choice(name: 'DEPLOY_ENV', choices: ['dev', 'staging', 'prod'], description: 'Environment to deploy to') // booleanParam(name: 'rename_host', defaultValue: true, description: 'When checked hostname will be renamed') // booleanParam(name: 'config_matt', defaultValue: true, description: 'config matt profile') } environment { ANSIBLE_FORCE_COLOR = '1' SATURN_BEHEMOTH = credentials('SATURN_BEHEMOTH') pxe_proxy_password = credentials('pxe_proxy_password') PXE_API_KEY = credentials('PXE_API_KEY') LINUX_LDAP_PWD = credentials('LINUX_LDAP') AUTHORIZED_KEY = credentials('AUTH_SSH_KEY') TERRA_BEHEMOTH_SMB = credentials('TERRA_BEHEMOTH_SMB') MATT_PASSWORD = credentials('MATT_PASSWORD') 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') vm_party_username_password = credentials('cosmos_root_password') is_admin = '0' } 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 Jenkins | 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 -g \$jenkins_group -u \$jenkins_user -i ${params.host_ip} """ } } stage('Ansible Playbook') { steps { 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/test.yaml \ --ssh-common-args='-o StrictHostKeyChecking=no'\ --extra-vars "saturn_behemoth=${SATURN_BEHEMOTH} linux_ldap_pwd=${LINUX_LDAP_PWD} \ pxe_proxy_password=${pxe_proxy_password} PXE_API_KEY=${PXE_API_KEY} \ AUTHORIZED_KEY=${AUTHORIZED_KEY} TERRA_BEHEMOTH_SMB=${TERRA_BEHEMOTH_SMB} \ CIFS_USERNAME=${env.TERRA_BEHEMOTH_SMB_USR} CIFS_PASSWORD=${env.TERRA_BEHEMOTH_SMB_PSW} \ MATT_PASSWORD=${env.MATT_PASSWORD} host_ip=${params.host_ip} \ matt_public_key='${env.matt_public_key}' matt_private_key='${env.matt_private_key}' \ cosmos_password='${env.cosmos_password}' cosmos_root_password='${env.cosmos_root_password}' \ vm_party_username_password=${env.vm_party_username_password} }" """ } } } 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 """ } } }