如何通过命令行设置Jenkins中的jnlp端口?

4

我正在运行一个安装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.

@techraf:是的,当我只是尝试从命令行运行它时,它确实有问题。 - user_dev
@techraf 我在curl命令结束后添加了-vvv。您能否详细说明您的期望是什么? - user_dev
好的,我只是想在Jenkins中设置TCP/jnlp端口,相同的curl命令直接运行正常,但在ansible中不行。我正在努力找到解决方案,为什么它会失败。 - user_dev
你的 -vvv 在单引号外面,应该放在里面。 - guido
1个回答

4
如果您同意通过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。
这假定Jenkins将通过某个标题发送JNLP端口,并且可以在本地访问。
或者,您可以使用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模块。


这看起来很有趣。我会试一试的。谢谢! - user_dev
它会永久设置一个端口还是只是暂时使用该端口来运行脚本? - user_dev
instance.save() 将使其在 Jenkins 进程重启后持久化。如果省略此操作,JNLP 端口仅在 Jenkins 进程关闭之前设置。换句话说,它不是针对每个 Groovy 脚本执行的。 - Parag Doke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接