从 Jenkins Active Choices 插件的 Ansible 库存中动态提取主机名

问题描述 投票:0回答:1

我正在致力于将 Ansible 与 Jenkins 集成,并且需要从 Ansible 库存文件中动态提取主机名,以使用 Active Choices 插件填充 Jenkins 作业中的选择。

我有一个脚本,在 Jenkins 中作为单独的作业执行时可以正常运行。但是,当我尝试在插件中运行它时,它不起作用。我不确定如何调试这个问题或理解为什么它在插件上下文中失败。有人可以提供有关 Jenkins 插件中调试脚本的见解或建议为什么它可能无法按预期执行吗?

这是我尝试过的:

properties([
    parameters([
        activeChoice(
            choiceType: 'PT_SINGLE_SELECT', 
            filterLength: 1, 
            filterable: false, 
            name: 'cloud', 
            randomName: 'choice-parameter-291132897896439385', 
            script: groovyScript(
                fallbackScript: [
                    classpath: [], 
                    oldScript: '', 
                    sandbox: false, 
                    script: ''
                ], 
            script: [
               classpath: [], 
               oldScript: '', 
               sandbox: false, 
               script: 
                  '''def getHostList(String envir, String envGroup) {
    node("slave-python") {
        
        def hosts = []
        checkout scmGit(branches: 
           [[name: \'*/master\']], 
           extensions: [
               cloneOption(
                   depth: 1, 
                   noTags: false, 
                   reference: \'\', 
                   shallow: true
                )
            ], 
            userRemoteConfigs: [
                [
                    credentialsId: \'jhgfghghf\', 
                    url: \'[email protected]:ansible/env.git\'
                ]
            ]
        )
        
        withCredentials([file(credentialsId: \'vault_file\', variable: \'ansibleVaultKeyFile\')]) {
            sh \'ls -lha\'
            
            String output = sh returnStdout: true, script: "ansible-inventory -i inventories/inventory.yml --list --vault-password-file ${ansibleVaultKeyFile}| jq -r \\\'.[] | select(.hosts != null) | .hosts | .[]\\\' | sort -u |grep \\\'${envGroup}\\\'"
            
            hosts = output.trim().split(\'\\n\').collect { "\'${it}\'" }.join(\', \')
        }
        println("[$hosts]")
    
        return hosts.tokenize()
        
    }
}
return getHostList(\'env-test\', \'kuber\')''']))])])

我的目标是:

  1. 从 Ansible 清单中动态提取主机名。
  2. 使用这些主机名填充 Jenkins 作业中的 Active Choices 参数。

问题:

  1. 如何修改脚本以正确从 Ansible 清单中提取主机名?
  2. 是否有更好的方法或插件将 Ansible 库存主机名集成到 Jenkins Active Choices 中?
jenkins ansible jenkins-plugins jenkins-groovy ansible-inventory
1个回答
0
投票

问题: 如何修改脚本以正确从 Ansible 清单中提取主机名? 是否有更好的方法或插件将 Ansible 库存主机名集成到 Jenkins Active Choices 中?

我不会称其为更好的方法,但我使用了一种不同的方法 - 我发现它更简单、更可靠、更安全,因为它有助于解决 Jenkins 无法处理的主要问题之一构建启动之前的参数。您可能想要应用所有建议或仅应用其中的一部分,具体取决于您的情况。

首先,由于

ansible-inventory
可以返回有效的 JSON 或 YAML,因此将 stdout 重定向到文件并使用
readJson
readYaml
加载它,然后直接从 JSON 对象提取数据可能会更简单、更安全,跳过甚至可能未安装的
jq
。请注意,我并不是说任何有关解析清单文件的事情,因为可以使用多个清单。

接下来,如果我正确理解了您命令中

grep
的用法,您可以通过应用
--limit
来避免这种情况,这也受
ansible-inventory
支持。不过,请务必小心 - 如果它是自由文本参数,则可能会进行注入并窃取保管库密码。这就是为什么我尽可能使用环境变量而不是 CLI 参数来配置所有内容的原因。

最后,这是我从对 Jenkins Jira 问题的无尽评论中学到的我最喜欢的 Jenkins 技巧,我动态定义参数及其值,并在使用

currentBuild.result = 'NOT_BUILT'
error
后立即停止构建消息和指向
Build with parameters
页面的链接。当构建号等于 1 或某些专用布尔构建参数为
true
时,我将其作为启用的单独阶段运行。

这允许:

  • 动态填充参数,而不使用外部(可能有缺陷或易受攻击的)插件;
  • 在不批准 Jenkins 管理面板中的自定义 Groovy 脚本的情况下执行相同的操作 - 很可能,Jenkins 可能会要求 Active Choice 脚本这样做,因为据我所知,它们不在 Groovy 沙箱中运行,这实际上可能是您的问题的原因问题;
  • 克服 Jenkins 最烦人的问题之一,特别是在使用多分支管道时,因为每个新分支都从那里的构建 #1 开始。
© www.soinside.com 2019 - 2024. All rights reserved.