无法通过Ubuntu 16.04 LTS上的Ansible with Jenkins_作业创建Jenkins作业

无法通过Ubuntu 16.04 LTS上的Ansible with Jenkins_作业创建Jenkins作业,jenkins,ansible,Jenkins,Ansible,我试图在Ansible中使用jenkins_作业模块,它不断抛出错误无法创建作业,不支持带有编码声明的Unicode字符串。请使用无声明的字节输入或XML片段。 我使用main.yml调用的文件tasks/add_job.yml中的以下任务触发此错误: - name: Install CloudBees Folder for {{ item }} jenkins_job: config: "{{ lookup('template', 'config.xml.j2') }}"

我试图在Ansible中使用jenkins_作业模块,它不断抛出错误
无法创建作业,不支持带有编码声明的Unicode字符串。请使用无声明的字节输入或XML片段。

我使用main.yml调用的文件tasks/add_job.yml中的以下任务触发此错误:

- name: Install CloudBees Folder for {{ item }}
  jenkins_job:
    config: "{{ lookup('template', 'config.xml.j2') }}"
    name: {{ item }}
    password: "{{ jenkins_admin_password }}"
    url: "http://{{ jenkins_hostname }}:{{ jenkins_http_port }}"
    user: "{{ jenkins_admin_username }}"
tasks/add_jobs.yml多次调用此任务,如下所示:

- name: Include job array via vars.
  include_vars:
    file: jobs.yml

- name: Install jobs.
  include: add_job.yml
  with_items: "{{ jenkins_jobs }}"
var文件如下所示:

jenkins_jobs:
  - Job1
  - Job2
我的配置文件位于templates目录中,如下所示(我尝试了许多不同的XML文件,最后尝试使用ansible GitHub项目:

<?xml version='1.0' encoding='UTF-8'?>
<project>
  <actions/>
  <description></description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <jenkins.model.BuildDiscarderProperty>
      <strategy class="hudson.tasks.LogRotator">
        <daysToKeep>1</daysToKeep>
        <numToKeep>20</numToKeep>
        <artifactDaysToKeep>-1</artifactDaysToKeep>
        <artifactNumToKeep>-1</artifactNumToKeep>
      </strategy>
    </jenkins.model.BuildDiscarderProperty>
    <org.jenkinsci.plugins.gitbucket.GitBucketProjectProperty plugin="gitbucket@0.8">
      <linkEnabled>false</linkEnabled>
    </org.jenkinsci.plugins.gitbucket.GitBucketProjectProperty>
  </properties>
  <scm class="hudson.scm.NullSCM"/>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders/>
  <publishers/>
  <buildWrappers/>
</project>
在出现这个错误之前,我在运行jenkins_作业时遇到了很多问题,因为它一直在抱怨缺少依赖项,所以下面是我在Ubuntu 16.04.2 LTS上安装的包列表

jenkins_job_dependencies:
  - build-essential
  - python-pip
  - python3-pip
  - libffi-dev
  - libssl-dev
  - libxml2-dev
  - libxslt1-dev
  - python-dev
  - python3-dev
  - python-lxml
  - python3-lxml
  - python-jenkins
  - python3-jenkins
  - python3-venv
  - git
这是
-vvv
输出

fatal: [local-vagrant]: FAILED! => {
    "changed": false,
    "failed": true,
    "invocation": {
        "module_args": {
            "config": "<?xml version='1.0' encoding='UTF-8'?>\n<project>\n  <actions/>\n  <description></description>\n  <keepDependencies>false</keepDependencies>\n  <properties>\n    <jenkins.model.BuildDiscarderProperty>\n      <strategy class=\"hudson.tasks.LogRotator\">\n        <daysToKeep>1</daysToKeep>\n        <numToKeep>20</numToKeep>\n        <artifactDaysToKeep>-1</artifactDaysToKeep>\n        <artifactNumToKeep>-1</artifactNumToKeep>\n      </strategy>\n    </jenkins.model.BuildDiscarderProperty>\n    <org.jenkinsci.plugins.gitbucket.GitBucketProjectProperty plugin=\"gitbucket@0.8\">\n      <linkEnabled>false</linkEnabled>\n    </org.jenkinsci.plugins.gitbucket.GitBucketProjectProperty>\n  </properties>\n  <scm class=\"hudson.scm.NullSCM\"/>\n  <canRoam>true</canRoam>\n  <disabled>false</disabled>\n  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>\n  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>\n  <triggers/>\n  <concurrentBuild>false</concurrentBuild>\n  <builders/>\n  <publishers/>\n  <buildWrappers/>\n</project>\n",
            "enabled": null,
            "name": "Job1",
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "state": "present",
            "token": null,
            "url": "http://localhost:8080",
            "user": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
        }
    },
    "msg": "Unable to create job, Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration. for http://localhost:8080"
}
fatal:[本地流浪汉]:失败!=>{
“更改”:错误,
“失败”:对,
“调用”:{
“模块参数”:{
“配置”:“\n\n\n\n false\n\n\n\n 1\n 20\n-1\n-1\n\n\n\n false\n\n\n true\n false\n false\n false\n false\n\n false\n\n\n\n\n\n”,
“已启用”:空,
“名称”:“作业1”,
“密码”:“没有日志参数中指定的值”,
“国家”:“目前”,
“令牌”:空,
“url”:”http://localhost:8080",
“用户”:“没有日志参数中指定的值”
}
},
“msg”:“无法创建作业,不支持带有编码声明的Unicode字符串。请使用字节输入或不带声明的XML片段。有关http://localhost:8080"
}
我尝试了许多xml声明的排列方式:包括它、删除它、使用utf-16等等,但似乎都不起作用。如果有任何提示,我将不胜感激。我很感激有许多其他方法可以通过Ansible(cUrl、cli等)创建作业我正在将我的项目移植到job dsl插件中,但如果我能使用一个“开箱即用”的Ansible模块来实现这一点,那就太好了


好的……这是我关于StackOverflow的第一个问题……如果我有任何错误,我表示歉意。

事实证明是ansible_python_解释器在我的主机清单中设置为
/usr/bin/python3
。当我将其设置为
/usr/bin/python
时,问题消失了。耶

…但这只是故事的一半。当我销毁并重新创建我的虚拟机时,它在第一步就失败了,因为Ubuntu 16.04LTS没有
/usr/bin/python
它只有
/usr/bin/python3

我不想创建符号链接或执行virtenv之类的操作…所以我只是在安装python2 dev apt包之后,但在运行jenkins_脚本模块之前,将ansible_python_解释器设置为
/usr/bin/python
。然后在运行脚本之后,我再次将其设置回原处

# Truncated playbook for brevity...
- name: Install dependency
  package: "name=python-dev state=present"

- name: Set the python interpreter to the symlink (which points at version 2.7)
  set_fact:
    ansible_python_interpreter: '/usr/bin/python'

- name: Do script thing.
  jenkins_script:
    script: "{{ script_var }}"
    user: "{{ jenkins_username }}"
    password: "{{ jenkins_password }}"
  changed_when: true
  no_log: True

- name: Set the python interpreter back to version 3
  set_fact:
    ansible_python_interpreter: '/usr/bin/python3'
如果我能找到jenkins_脚本模块的源代码在哪里,我会做一个pull请求来修复潜在的问题,但遗憾的是,我无法找出它在哪里

# Truncated playbook for brevity...
- name: Install dependency
  package: "name=python-dev state=present"

- name: Set the python interpreter to the symlink (which points at version 2.7)
  set_fact:
    ansible_python_interpreter: '/usr/bin/python'

- name: Do script thing.
  jenkins_script:
    script: "{{ script_var }}"
    user: "{{ jenkins_username }}"
    password: "{{ jenkins_password }}"
  changed_when: true
  no_log: True

- name: Set the python interpreter back to version 3
  set_fact:
    ansible_python_interpreter: '/usr/bin/python3'