将 DSL 用于 Jenkins 作业时 Ansible AWX 步骤失败

Ansible AWX step fails when using DSL for Jenkins job

我正在 运行在 Ubuntu 16.04 上安装 Jenkins 2.140 服务器以及使用 Ansible 2.6.2 的 Ansible AWX 1.0.7.2 服务器。

我正在 Jenkins 中创建一个作业,它 运行 是我的 Ansible AWX 服务器上的一个模板。我还有其他几个 Jenkins 工作,这些工作 运行 模板都可以工作,所以我知道我为此使用的一般配置没问题。

然而,当我使用使用 JobDSL 的种子作业创建 Jenkins 作业时,该作业在 Ansible AWX 步骤失败,输出如下:

11:50:42 [EnvInject] - Loading node environment variables.
11:50:42 Building remotely on windows-slave (excel Windows orqaheadless windows) in workspace C:\JenkinsSlave\workspace\create-ec2-instance-2
11:50:42 ERROR: Build step failed with exception
11:50:42 java.lang.NullPointerException
11:50:42    at org.jenkinsci.plugins.ansible_tower.AnsibleTower.perform(AnsibleTower.java:129)
11:50:42    at hudson.tasks.BuildStepMonitor.perform(BuildStepMonitor.java:20)
11:50:42    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
11:50:42    at hudson.model.Build$BuildExecution.build(Build.java:206)
11:50:42    at hudson.model.Build$BuildExecution.doRun(Build.java:163)
11:50:42    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504)
11:50:42    at hudson.model.Run.execute(Run.java:1815)
11:50:42    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
11:50:42    at hudson.model.ResourceController.execute(ResourceController.java:97)
11:50:42    at hudson.model.Executor.run(Executor.java:429)
11:50:42 Build step 'Ansible Tower' marked build as failure
11:50:42 [BFA] Scanning build for known causes...
11:50:42 [BFA] No failure causes found
11:50:42 [BFA] Done. 0s
11:50:42 Started calculate disk usage of build
11:50:42 Finished Calculation of disk usage of build in 0 seconds
11:50:42 Started calculate disk usage of workspace
11:50:42 Finished Calculation of disk usage of workspace in 0 seconds
11:50:42 Finished: FAILURE

那个输出并没有真正给我任何可以使用的东西,尤其是因为我不是 Java 专家。

我手动配置了 Jenkins 作业,一切正常。这是工作作业的 config.xml(仅 AWX 部分)。请注意,所有这些额外变量都在作业的前面作为参数传递:

<builders>
<org.jenkinsci.plugins.ansible__tower.AnsibleTower plugin="ansible-tower@0.9.0">
<towerServer>AWX Server</towerServer>
<jobTemplate>create-ec2-instance</jobTemplate>
<extraVars>
key_name: ${key_name} ec2_termination_protection: ${ec2_termination_protection} vpc_subnet_id: ${vpc_subnet_id} security_groups: ${security_groups} instance_type: ${instance_type} instance_profile_name: ${instance_profile_name} assign_public_ip: ${assign_public_ip} region: ${region} image: ${image} instance_tags: ${instance_tags} ec2_wait_for_create: ${ec2_wait_for_create} ec2_wait_for_create_timeout: ${ec2_wait_for_create_timeout} exact_count: ${exact_count} delete_volume_on_termination: ${delete_volume_on_termination} data_disk_size: ${data_disk_size} private_domain: ${private_domain} route53_private_record_ttl: ${route53_private_record_ttl} dns_record: ${dns_record} elastic_ip: ${elastic_ip}
</extraVars>
<jobTags/>
<skipJobTags/>
<jobType>run</jobType>
<limit/>
<inventory/>
<credential/>
<verbose>true</verbose>
<importTowerLogs>true</importTowerLogs>
<removeColor>false</removeColor>
<templateType>job</templateType>
<importWorkflowChildLogs>false</importWorkflowChildLogs>
</org.jenkinsci.plugins.ansible__tower.AnsibleTower>
</builders>

以及来自失败的 JobDSL 生成的作业的 config.xml,这对我来说看起来是一样的:

<builders>
<org.jenkinsci.plugins.ansible__tower.AnsibleTower>
<towerServer>AWX Server</towerServer>
<jobTemplate>create-ec2-instance</jobTemplate>
<jobType>run</jobType>
<templateType>job</templateType>
<extraVars>
key_name: ${key_name} ec2_termination_protection: ${ec2_termination_protection} vpc_subnet_id: ${vpc_subnet_id} security_groups: ${security_groups} instance_type: ${instance_type} instance_profile_name: ${instance_profile_name} assign_public_ip: ${assign_public_ip} region: ${region} image: ${image} instance_tags: ${instance_tags} ec2_wait_for_create: ${ec2_wait_for_create} ec2_wait_for_create_timeout: ${ec2_wait_for_create_timeout} exact_count: ${exact_count} delete_volume_on_termination: ${delete_volume_on_termination} data_disk_size: ${data_disk_size} private_domain: ${private_domain} route53_private_record_ttl: ${route53_private_record_ttl} dns_record: ${dns_record} elastic_ip: ${elastic_ip}
</extraVars>
<verbose>true</verbose>
<importTowerLogs>true</importTowerLogs>
</org.jenkinsci.plugins.ansible__tower.AnsibleTower>
</builders>

因此,对于 JobDSL 生成的作业,您总是会遇到一些预期的差异,例如缺少空字段,但我们遵循此过程的所有(成功的)其他作业都是这种情况。

JobDSL 脚本在这里:

    configure { project ->
        project / 'builders ' << 'org.jenkinsci.plugins.ansible__tower.AnsibleTower' {
            towerServer 'AWX Server'
            jobTemplate ('create-ec2-instance')
            templateType 'job'
            jobType 'run'
            extraVars('''key_name: ${key_name} 
ec2_termination_protection: ${ec2_termination_protection} 
vpc_subnet_id: ${vpc_subnet_id} 
security_groups: ${security_groups} 
instance_type: ${instance_type} 
instance_profile_name: ${instance_profile_name} 
assign_public_ip: ${assign_public_ip} 
region: ${region} image: ${image} 
instance_tags: ${instance_tags} 
ec2_wait_for_create: ${ec2_wait_for_create} 
ec2_wait_for_create_timeout: ${ec2_wait_for_create_timeout} 
exact_count: ${exact_count} 
delete_volume_on_termination: ${delete_volume_on_termination} 
data_disk_size: ${data_disk_size} 
private_domain: ${private_domain} 
route53_private_record_ttl: ${route53_private_record_ttl} 
dns_record: ${dns_record} 
elastic_ip: ${elastic_ip}''')
            verbose 'true'
            importTowerLogs 'true'
        }
    }

这生成的作业 在我看来 在 UI(以及 XML)中完全相同,但我一直在理解当我运行失败时。显然我错过了一些东西,但如果我看到了什么,我将无法终生。

尽管其他 AWX 作业构建时没有这个,但我添加了缺失的(空)字段,作业开始成功。

因此,将我的 JobDSL 脚本更改为:

    configure { project ->
        project / 'builders ' << 'org.jenkinsci.plugins.ansible__tower.AnsibleTower' {
            towerServer 'AWX Server'
            jobTemplate ('create-ec2-instance')
            extraVars('''key_name: ${key_name} 
ec2_termination_protection: ${ec2_termination_protection} 
vpc_subnet_id: ${vpc_subnet_id} 
security_groups: ${security_groups} 
instance_type: ${instance_type} 
instance_profile_name: ${instance_profile_name} 
assign_public_ip: ${assign_public_ip} 
region: ${region} 
image: ${image} 
instance_tags: ${instance_tags} 
ec2_wait_for_create: ${ec2_wait_for_create} 
ec2_wait_for_create_timeout: ${ec2_wait_for_create_timeout} 
exact_count: ${exact_count} 
delete_volume_on_termination: ${delete_volume_on_termination} 
data_disk_size: ${data_disk_size} 
private_domain: ${private_domain} 
route53_private_record_ttl: ${route53_private_record_ttl} 
dns_record: ${dns_record} 
elastic_ip: ${elastic_ip}''')
            jobTags ''
            skipJobTags ''
            jobType 'run'
            limit ''
            inventory ''
            credential ''
            verbose 'true'
            importTowerLogs 'true'
            removeColor ''
            templateType 'job'
            importWorkflowChildLogs ''

现在按预期工作。