1

I have this Jenkins pipeline where I need to run ansiblePlaybook() command with inventory file. Inventory file does contain date (current date) part (my-instance-ips-(mm-dd-yyyy)). Here, I am facing issue in creating currentDate variable and pass into pipeline script

Jenkins File:

pipeline {
    agent any   
     stages {
         stage ( 'Executing shell script' ) {
             steps {
                 script {
                     sh """
                     currentDate = "\$(date +'%m-%d-%Y')"
                     inventory_file = "my-instance-ips-{$currentDate}.yml"
                     
                     ansiblePlaybook (
                     playbook: 'task.yml',
                     inventory: $inventory_file,
                     installation: 'ansible-2.6.5','
                     -e "DATE = $currentDate")
                     """
                     }
                 }
             }
           }
}

Error Message:

groovy.lang.MissingPropertyException: No such property: currentDate for class: groovy.lang.Bindin

Could someone help me out to create current date in pipeline script and the same should pass over to ansible playbook command?

Alfred Hitchkock
  • 357
  • 2
  • 14

1 Answers1

3

Looks like you are invoking ansible plugin. If that is what you are trying to achive, then your ansible playbook call should not be inside sh step.
You need to get the command's output first then invoke ansible plugin.

import java.time.format.DateTimeFormatter
pipeline {
    agent any   
    stages {
         stage ( 'Executing shell script' ) {
             steps {
                 script {
/*                     currentDate = sh (
                         script: "date +'%m-%d-%Y'"
                         returnStatus: true
                     ).trim()
*/
                     cDate = java.time.LocalDate.now()
                     currentDate = cDate.format(DateTimeFormatter.ofPattern("MM-dd-yyyy"))
                     inventory_file = "my-instance-ips-${currentDate}.yml"
                     println inventory_file

                     ansiblePlaybook ([
                         playbook: 'task.yml',
                   //      credentialsId: 'xxxx',
                         disableHostKeyChecking: true,
                         inventory: "${inventory_file}",
                         extraVars: [
                             DATE: "${currentDate}"
                         ]
                     ])
                   }
                 }
             }
           }
}
Vijesh
  • 923
  • 2
  • 12
  • 22
  • I am getting ```unexpected token: returnStdout``` error for this code – Alfred Hitchkock May 04 '21 at 05:13
  • Which Jenkins version you are using? It seems you need to update plugins and dependencies as mentioned here: https://stackoverflow.com/questions/42727104/jenkins-pipeline-sh-returnstdout-not-working – Vijesh May 04 '21 at 07:05
  • I've added another workaround to get the date. I've also corrected the extraVars section where I was using '=' instead of ':' – Vijesh May 04 '21 at 07:47
  • I tried with this, but getting ```java.lang.ClassCastException: class org.jenkinsci.plugins.ansible.workflow.AnsiblePlaybookStep.setExtraVars() expects interface java.util.Map but received class java.util.ArrayList``` this error.. – Alfred Hitchkock May 04 '21 at 08:31
  • I don't think we need the extra vars anymore.. if we delete it, the code will be good!! thank you so much – Alfred Hitchkock May 04 '21 at 08:33
  • ```expects interface java.util.Map but received class java.util.ArrayList``` error was due to the '=' inside extraVars map. Please see the updated code. – Vijesh May 04 '21 at 08:54