Jenkins - Passing array/list to Parameterized Remote Build

后端 未结 2 818
故里飘歌
故里飘歌 2021-01-27 11:15

I am using Jenkins to remotely run an Ansible playbook via the Publish Over SSH command.

This command:

curl -k -v -X POST https://jenkins.m         


        
相关标签:
2条回答
  • 2021-01-27 11:44

    This is a bit of a guess, based on a similar problem I have seen with a choice parameter. Any documentation I have found seems to be wrong about how to handle these. It shouldn't be a list. Try passing as a string with newlines separating the items.

    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\ntwo\nthree"}]}'
    

    Let me know if this works. I'm interested to find out.


    Edit: (based on comments)

    Would this work:

    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":"'{\"thisIsAList\": [\"one\",\"two\",\"three\"]}'"}]}'
    

    The nested quotes get a bit ugly. If you are using pipeline or can massage the data in a shell script first, it would probably be cleaner.

    0 讨论(0)
  • 2021-01-27 11:53

    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.

    The command:

    ansible-playbook /home/<myUsr>/test/practice.yml --extra-vars "thisIsAList=$thisIsAList"

    Should have declared thisIsAList to be a dictionary object. I.e.:

    ansible-playbook /home/<myUsr>/test/practice.yml --extra-vars "{thisIsAList=$thisIsAList}"

    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.

    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.

    0 讨论(0)
提交回复
热议问题