如何通过命令行在jenkins中设置jnlp端口?

时间:2016-08-26 11:45:47

标签: jenkins ansible ansible-playbook

我正在运行安装jenkins的ansible playbook,需要运行cli.jar来运行多个脚本。它需要jnlp端口,我想通过ansible shell命令设置它。但它似乎失败了。

 - 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.

1 个答案:

答案 0 :(得分:3)

如果您可以通过ansible调用jenkins-cli groovy命令,这对我有用:

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将该groovy代码传递给脚本控制台(我还没试过)。此链接分享示例: https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Script+Console

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

只是为了消除对curl的依赖,请随意尝试使用get_url和uri ansible模块。