使用 ansible 创建 jenkins 工作

问题描述:

我正在开发一个使用 ansible 在 centos7 上部署 jenkins CI 服务器的项目我在使用 ansible 从 xml 模板创建 jenkins 作业时遇到问题.

I'm working on a project to deploy a jenkins CI server on centos7 using ansible And I'm having problems creating jenkins jobs from an xml template using ansible.

到目前为止一切正常,但现在我希望能够创建作业,并使用 ansible 从 xml 文件中为它们提供一些基本配置.我的解决方案是来自 jenkins-cli 的以下命令:

Everything works fine so far, but now i want to be able to create jobs, and give them some basic configuration from an xml file using ansible. My solution was the following command from jenkins-cli:

sudo java -jar jenkins-cli.jar -s http://localhost:8080 create-job Job_test1 < Job_test1.xml

在 centos7 框中手动输入时,这非常有效,但是当我将其放入 ansible 并运行时:

this works perfectly when entered manually in the centos7 box, but when i put it into ansible and run it:

- name: create jenkins jobs with xml files
  sudo: yes
  command: "java -jar {{ jenkins.cli_dest }} -s http://localhost:8080 create-job {{ item.name }} < {{ jenkins_dest }}/{{ item.xml_name }}"
  with_items: jenkins_jobs

它给出了以下错误消息:

it gives the following error message:

stderr: Too many arguments: <
java -jar jenkins-cli.jar create-job NAME
Creates a new job by reading stdin as a configuration XML file.

有谁知道解决这个问题的方法吗?据我所知,我做得很好(因为命令在没有被 ansible 输入时有效)

Does anyone know a solution to this? As far as I can see I'm doing it properly(since the command works when not entered by ansible)

命令模块没有' 不支持输入和输出重定向,因为它不将命令字符串传递给 shell.这是它的文档所说的:

The command module doesn't support input and output redirection since it doesn't pass the command string to a shell. This is what its documentation says:

它不会通过shell处理,所以像$HOME这样的变量和像<"、>"、|"和&"这样的操作将不起作用(如果您需要这些功能,请使用 shell 模块).

It will not be processed through the shell, so variables like $HOME and operations like "<", ">", "|", and "&" will not work (use the shell module if you need these features).

所以:

- name: create jenkins jobs with xml files
  sudo: yes
  shell: "java -jar {{ jenkins.cli_dest }} -s http://localhost:8080 create-job {{ item.name }} < {{ jenkins_dest }}/{{ item.xml_name }}"
  with_items: jenkins_jobs