2016-08-26 47 views
2

我正在運行一個安裝jenkins的腳本,需要運行cli.jar來運行多個腳本。它需要jnlp端口,我希望通過可靠的shell命令進行設置。但它似乎失敗了。如何通過命令行在jenkins中設置jnlp端口?

- name: Set jnlp port 
    shell: 'curl -X POST -d ".useSecurity=on&slaveAgentPort.type=fixed&value=49187&core%3Aapply=true&json=%7B%22useSecurity%22%3A+%7B%22slaveAgentPort%22%3A+%7B%22type%22%3A+%22fixed%22%2C+%22value%22%3A+%2249187%22%7D%7D%2C+%22core%3Aapply%22%3A+%22true%22%7D" --header "Content-Type:application/x-www-form-urlencoded" http://localhost:8080//configureSecurity/configure' 
    remote_user: jenkins 
    become: yes 
    become_method: sudo 

與-vvv選項運行提供了:

ERROR! Syntax Error while loading YAML. 


The error appears to have been in '/vagrant/ansible/roles/jenkins1.648/tasks/plugins.yml': line 37, column 370, but may 
be elsewhere in the file depending on the exact syntax problem. 

The offending line appears to be: 

- name: Check update center and push it to the update URL 
    shell: 'curl -X POST -d .useSecurity=on&slaveAgentPort.type=fixed&value=49187&core%3Aapply=true&json=%7B%22useSecurity%22%3A+%7B%22slaveAgentPort%22%3A+%7B%22type%22%3A+%22fixed%22%2C+%22value%22%3A+%2249187%22%7D%7D%2C+%22core%3Aapply%22%3A+%22true%22%7D" --header "Content-Type:application/x-www-form-urlencoded" http://localhost:8080//configureSecurity/configure' -vvv 
                                                                                               ^here 
This one looks easy to fix. It seems that there is a value started 
with a quote, and the YAML parser is expecting to see the line ended 
with the same kind of quote. For instance: 

    when: "ok" in result.stdout 

Could be written as: 

    when: '"ok" in result.stdout' 

Or equivalently: 

    when: "'ok' in result.stdout" 
We could be wrong, but this one looks like it might be an issue with 
unbalanced quotes. If starting a value with a quote, make sure the 
line ends with the same set of quotes. For instance this arbitrary 
example: 

    foo: "bad" "wolf" 

Could be written as: 

    foo: '"bad" "wolf"' 

Ansible failed to complete successfully. Any error output should be 
visible above. Please fix these errors and try again. 
+0

@techraf:是的,當我只是嘗試從命令行運行它的時候呢 –

+0

@techraf我在curl命令結束後添加-vvv。你能詳細說明你如何期待它是 –

+0

好吧,我只想在jenkins中設置TCP/jnlp端口,並且相同的curl命令可以直接正常工作,但不能與正確的工作正常工作。我正試圖找到一個解決方案,爲什麼它失敗了。 –

回答

2

如果你好嗎通過ansible調用詹金斯-CLI命令常規,這個工作對我來說:

def instance=jenkins.model.Jenkins.instance 
instance.setSlaveAgentPort(9999) 
instance.save() 

Ansible任務可能看起來像:

- name: Set jnlp port 
    shell: 'java -jar /tmp/jenkins-cli.jar -s http://localhost:8080 groovy /tmp/fix-jnlp-port.groovy' 
    remote_user: jenkins 
    become: yes 
    become_method: sudo 

很明顯,你需要下載jenkins-cli.jar文件......並且事先安裝java。

這假定JNLP端口將由Jenkins發送(通過某個標題),並且可以在本地訪問。

或者,您可以使用curl將該常規代碼傳遞給腳本控制檯(我還沒有嘗試過)。此鏈接股份例子: https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Script+Console

curl --data-urlencode "script=$(<./somescript.groovy)" http://jenkins/scriptText 

而只是爲了消除對捲曲的依賴,隨意嘗試用GET_URL和URI ansible模塊。

+0

這看起來很有趣。將嘗試。謝謝! –

+0

這將繼續並永久設置一個端口,或者它將暫時使用該端口作爲腳本 –

+0

instance.save()將使它在jenkins進程重啓時保持不變。如果你省略了,jnlp端口將被設置,直到jenkins進程關閉。換句話說,這不是每個常規腳本執行。 –