Jenkins API intermittent authentication errors

ansibleJenkins

I'm using Ansible's jenkins_job task to set up some Jenkins jobs. I'm authenticating with a username+password which is authenticated as a Linux user on the Jenkins machine.

  - name: Install my job
    jenkins_job:
      name: "my-job"
      user: "{{ jenkins_admin_user }}"
      password: "{{ jenkins_admin_pass}}"
      config: "{{ lookup('template', 'templates/jobs/my-job.xml') }}" 

When I run Ansible some jobs get installed but part-way through the run, after installing some of my jobs (a different number each time), I get this error:

Unable to validate if job exists, Error in request. Possibly authentication failed [401]: Invalid password/token for user: my-jenkins-user

It's clear that my credentials work – jobs show up in Jenkins. So why am I getting this error?

Best Answer

This is a problem with the Jenkins API, not with Ansible. The solution is to use an API key instead of a password.

Log into Jenkins and click your username in the top right, then click "Configure" to see your API token. To use the key, just enter it instead of a password when authenticating with the Jenkins API.