问题描述
我正在使用Jenkins通过Publish Over SSH
命令远程运行Ansible剧本.
I am using Jenkins to remotely run an Ansible playbook via the Publish Over SSH
command.
此命令:
curl -k -v -X POST https://jenkins.myhost.com/job/Ansible_Deploy/build?token=<appToken> --user <myUser>:<userToken> --data-urlencode json='{"parameter":[{"name":"thisIsAList","value":["one","two","three"]}]}'
应触发生成后操作,以通过SSH远程执行以下命令:
should trigger a post-build action to remotely execute the following command over SSH:
ansible-playbook /home/<myUser>/test/practice.yml --extra-vars "thisIsAList=$thisIsAList"
thisIsAList
是作业通知"下的字符串参数,并且已对作业进行了参数化.我已经成功执行了类似的命令,但是由于值是一个列表,因此该命令失败了.我尝试了字符串参数"和多行字符串参数"都无济于事.
thisIsAList
is a string parameter under Job Notifications, and the job is parameterized. I have successfully executed similar commands, but this one fails, assumingly because the value is a list. I have tried both "String Parameter" as well as "Multi-line String Parameter" to no avail.
这是堆栈跟踪:
org.kohsuke.stapler.WrongTypeException: Got type array but no lister class found for type class java.lang.String
at org.kohsuke.stapler.RequestImpl$TypePair.convertJSON(RequestImpl.java:723)
at org.kohsuke.stapler.RequestImpl.bindJSON(RequestImpl.java:478)
at org.kohsuke.stapler.RequestImpl.instantiate(RequestImpl.java:777)
Caused: java.lang.IllegalArgumentException: Failed to convert the value parameter of the constructor public hudson.model.StringParameterValue(java.lang.String,java.lang.String)
at org.kohsuke.stapler.RequestImpl.instantiate(RequestImpl.java:779)
at org.kohsuke.stapler.RequestImpl.access$200(RequestImpl.java:83)
at org.kohsuke.stapler.RequestImpl$TypePair.convertJSON(RequestImpl.java:678)
Caused: java.lang.IllegalArgumentException: Failed to instantiate class hudson.model.StringParameterValue from {"name":"thisIsAList","value":["one","two","three"]}
at org.kohsuke.stapler.RequestImpl$TypePair.convertJSON(RequestImpl.java:680)
at org.kohsuke.stapler.RequestImpl.bindJSON(RequestImpl.java:478)
at org.kohsuke.stapler.RequestImpl.bindJSON(RequestImpl.java:474)
at hudson.model.StringParameterDefinition.createValue(StringParameterDefinition.java:88)
at hudson.model.ParametersDefinitionProperty._doBuild(ParametersDefinitionProperty.java:165)
注意:这可能是,但未得到有效的响应.
Note: This may be a duplicate of How to pass an array to a jenkins parameterized job via remote access api? but it hasn't gotten a valid response.
推荐答案
由于在Jenkins或Ansible文档中的任何地方都没有详细介绍此嵌套级别,因此,在解决问题后,我将对该主题进行一些说明.
Since this level of nesting isn't detailed anywhere in the Jenkins or Ansible documentation I'll shed some light on the topic now that I've solved my issue.
命令:
ansible-playbook /home/<myUsr>/test/practice.yml --extra-vars "thisIsAList=$thisIsAList"
应该已经将thisIsAList
声明为字典对象.即:
Should have declared thisIsAList
to be a dictionary object. I.e.:
ansible-playbook /home/<myUsr>/test/practice.yml --extra-vars "{thisIsAList=$thisIsAList}"
此外,cURL
命令中的数据的格式应如下所示:
Furthermore, the data in the cURL
command should've been formatted differently like so:
json='{"parameter":[{"name":"thisIsAList","value":"[one,two,three]"}]}'
注意:双引号位于整个列表中,而不是各个元素.
Note: the double-quotes are around the whole list, rather than the individual elements.
最后,对于其他嵌套项(例如列表中的dict),您必须像这样对双引号进行转义:
Finally, with further nested items (such as dict inside a list) you have to escape the double-quotes like so:
{"parameter":[{"name":"thisIsADictNestedInAList","value":"[{\"name\":\"numbers\",\"value\":[1s, 2s, 3s]}]"}]}
似乎,在此嵌套级别,不再需要对列表加双引号;可能是因为向上一级的引号已经导致其被正确解释.
It seems, that at this level of nesting, it is no longer required to double-quote the lists; probably because the quotes one level up already lead it to be interpreted correctly.
这篇关于Jenkins-将数组/列表传递给参数化的远程构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!