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' ansible_service_windows = credentials(' ansible-service-windows') } options { ansiColor('xterm') } stages { stage('Generate Inventory File') { steps { // Generate the dynamic inventory file // Usage: $0 -i IP_LIST -u JENKINS_USER -g JENKINS_GROUP -w WINDOWS_USER -p ANSIBLE_PASSWORD [-a SERVER_SUBNET_GROUP] [-s] [-v] [-e]" 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 -v -s -a \$jenkins_subnet_group -g \$jenkins_group -u \$jenkins_user \ -w ${env.ansible_service_windows_USR} -p ${env.ansible_service_windows_PSW} -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-windows/.inv/inventory-\$hash.yml" cd /var/jenkins_home/ansible-windows echo ansible-playbook -i \$inventory_file /var/jenkins_home/ansible-windows/playbooks/test.yaml \ --ssh-common-args='-o StrictHostKeyChecking=no' " """ } } } post { always { // Remove dynamic Inventory file sh """ hash=\$(echo -n "${params.host_ip}" | md5sum | cut -c 1-8) inventory_file="/var/jenkins_home/ansible-windows/.inv/inventory-\$hash.yml" rm \$inventory_file """ } } }