首页 > 解决方案 > 将 DSL 用于 Jenkins 作业时,Ansible AWX 步骤失败

问题描述

我在 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$1.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>

还有config.xml来自失败的 JobDSL 生成的工作,在我看来是一样的:

<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)中看起来是相同的,但是当我运行它时,我总是遇到这种错误。显然我错过了一些东西,但如果我看到了什么,我就无法终生。

标签: jenkins-job-dslansible-toweransible-awx

解决方案


尽管其他 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 ''

现在按预期工作。


推荐阅读