在Ubuntu 16.04 LTS上无法通过Ansible与jenkins_job创建Jenkins作业

时间:2017-06-21 13:41:37

标签: jenkins ansible

我正在尝试使用Ansible中的 jenkins_job 模块,它不断抛出错误Unable to create job, Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.

我使用以下任务触发此错误,该任务位于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 }}"

任务/ 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

我的配置文件位于模板目录中,看起来像这样(我尝试了很多不同的XML文件,最后尝试使用来自ansible GitHub项目的this config

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

我针对通过以下方式配置的本地Vagrant运行此目标:

Vagrant.configure(2) do |config|
  config.vm.box = 'ubuntu/xenial64'
  config.vm.network(:private_network, ip: '192.168.99.101')
  config.vm.network(:forwarded_port, guest: 8080, host: 8080)
  config.vm.provider :virtualbox do |provider|
    provider.customize ['modifyvm', :id, '--name', 'ansible-jenkins']
    provider.customize ['modifyvm', :id, '--cpus', '2']
    provider.customize ['modifyvm', :id, '--memory', '2048']
    provider.customize ['modifyvm', :id, '--nictype1', 'virtio']
  end
end

在收到此错误之前,我有很多问题,甚至让jenkins_job运行,因为它一直抱怨缺少依赖项,所以这是我在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

这是-vvvv输出

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"
}

我尝试了许多xml声明的排列:包括它,删除它,使用utf-16等,似乎没有任何工作。任何指针都会感激不尽。我很欣赏有很多其他方法可以通过Ansible(cUrl,cli等)创建作业,我正在移植我的项目以使用job-dsl插件,但如果我能使用'out'来完成这项工作,它会非常简洁开箱即用的'Ansible模块

好吧......所以这是我在StackOverflow上的第一个问题...如果我有什么不对,请道歉。

1 个答案:

答案 0 :(得分:0)

事实证明,我的主机广告资源中的ansible_python_interpreter设置为/usr/bin/python3。当我将其设置为/usr/bin/python时,问题就消失了。耶....

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

我不想创建符号链接或做virtenv东西...所以我只是在之后将ansible_python_interpreter设置为/usr/bin/python 我安装了python2-dev apt包但是之前我运行了jenkins_script模块。然后我在运行脚本后再将其重新设置。

# 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_script模块源代码的位置,我会做一个拉取请求来修复底层问题但是我无法弄清楚它在哪里......