简体   繁体   English

如何将 shell 变量传递给 ansible 剧本命令

[英]How to pass shell variable to ansible playbook command

I have this Jenkins pipeline where I need to run ansiblePlaybook() command with inventory file.我有这个 Jenkins 管道,我需要在其中运行带有库存文件的 ansiblePlaybook() 命令。 Inventory file does contain date (current date) part (my-instance-ips-(mm-dd-yyyy)).库存文件确实包含日期(当前日期)部分(my-instance-ips-(mm-dd-yyyy))。 Here, I am facing issue in creating currentDate variable and pass into pipeline script在这里,我在创建 currentDate 变量并传递到管道脚本时遇到问题

Jenkins File: Jenkins 文件:

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?有人可以帮我在管道脚本中创建当前日期,并且同样应该传递给 ansible 剧本命令吗?

Looks like you are invoking ansible plugin.看起来您正在调用 ansible 插件。 If that is what you are trying to achive, then your ansible playbook call should not be inside sh step.如果那是您想要实现的目标,那么您的 ansible 剧本调用不应该在sh步骤中。
You need to get the command's output first then invoke ansible plugin.您需要先获取命令的 output 然后调用 ansible 插件。

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}"
                         ]
                     ])
                   }
                 }
             }
           }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM