使用DSL进行Jenkins作业时Ansible AWX步骤失败

时间:2018-11-23 12:28:28

标签: jenkins-job-dsl ansible-tower 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>

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)中在我看来是 相同,但是在运行它时却不断遇到这种失败。显然我缺少了一些东西,但是如果我看到了什么,我将一生都无法。

1 个答案:

答案 0 :(得分:0)

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

现在可以正常工作了。