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

How to set jnlp port in jenkins through command line?

我正在 运行安装 jenkins 的 ansible 剧本,需要 运行 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.

如果您可以通过 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 发出(通过一些 header)并且可以在本地访问。

或者,您可以使用 curl 将 groovy 代码传递到脚本控制台(我没有尝试过)。 link 分享示例: https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Script+Console

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

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