Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to create jenkins credentials via ansible

I am putting together a developer machine using ansible. In that machine i am installing jenkins.

I have created the jobs for jenkins with ansible:

- shell: "java -jar {{ jenkins.cli_jar }} -s {{ jenkins.server }} create-job \
      {{ item.name }} < {{ jenkins.jobs_dir }}/{{ item.xml_file }}"
  with_items: "jenkins.jobs"

And installed the plugins, via cli etc.

But now i am missing the ssh credentials for the jobs; i just want a ssh credential with user "jenkins" and that uses "From the Jenkins master ~/.ssh".

This type of credentials are the ones i am talking about:

enter image description here

Maybe is a groovy script but i haven't find a lot of information about it. Thanks for the help.

like image 209
civilian Avatar asked Jan 26 '16 23:01

civilian


People also ask

How to run Ansible playbook with Jenkins pipeline job?

Choose the ‘ Invoke Ansible Playbook ‘ option as Build Step. And configure the same playbook which was created to print the username. Save the Job and click on Build Now. Verify the Console log. Step #4.2. Run Ansible Playbook with Jenkins Pipeline Job

Is there a REST API for the ansible credentials plugin?

Unfortunately there is no REST API for the Credentials Plugin, but the following snippet will do the trick with curl. Please note that this assumes Jenkins is running locally on its default port of 8080, and the username & password admin have permissions to create credentials. And here are the tasks to accomplish the same with Ansible:

What are the required fields for Jenkins credentials?

org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl: A secret text credential. Required fields: id, scope, secret com.cloudbees.jenkins.plugins.awscredentials.AWSCredentialsImpl: AWS credentials as used by the EC2 plugin.

Is there a REST API for the Jenkins credentials plugin?

Recently while building a Pipeline as a Service implementation, I faced the challenge of adding credentials into Jenkins via a script. Unfortunately there is no REST API for the Credentials Plugin, but the following snippet will do the trick with curl.


3 Answers

You can use the jenkins client from command line on the machine where the jenkins runs like:

java -jar jenkins-cli.jar -s http://localhost:8080/ groovy create-credential.groovy

with create-credential.groovy:

import jenkins.model.*
import com.cloudbees.plugins.credentials.CredentialsScope
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl

def addPassword = { username, new_password ->
    def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
        com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class,
        Jenkins.instance
    )

    def c = creds.findResult { it.username == username ? it : null }

    if ( c ) {
        println "found credential ${c.id} for username ${c.username}"
    } else {
        def credentials_store = Jenkins.instance.getExtensionList(
            'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
            )[0].getStore()

        def scope = CredentialsScope.GLOBAL

        def description = ""

        def result = credentials_store.addCredentials(
            com.cloudbees.plugins.credentials.domains.Domain.global(), 
            new UsernamePasswordCredentialsImpl(scope, null, description, username, new_password)
            )

        if (result) {
            println "credential added for ${username}" 
        } else {
            println "failed to add credential for ${username}"
        }
    }
}

addPassword('pinky', 'narf')

This will add the global credential for user 'pinky' with password 'narf'

like image 171
DevOoops Avatar answered Oct 01 '22 01:10

DevOoops


As of version 2.1.1 of the plugin (June 2016) this is possible through the CLI or the REST API:

https://github.com/jenkinsci/credentials-plugin/blob/master/docs/user.adoc#creating-a-credentials

from that page: $ cat > credential.xml <<EOF <com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl> <scope>GLOBAL</scope> <id>deploy-key</id> <username>wecoyote</username> <password>secret123</password> </com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl> EOF $ curl -X POST -H content-type:application/xml -d @credential.xml \ https://jenkins.example.com/job/example-folder/credentials/store/folder/\ domain/testing/createCredentials

like image 24
Korny Avatar answered Oct 01 '22 03:10

Korny


Jenkins credentials plugin doesn't allow credentials creation using API (https://issues.jenkins-ci.org/browse/JENKINS-28407).

A viable solution would be recording a credential creation using your prefered browser and JMeter proxy or Selenium IDE. and replaying it using JMeter CLI or saving the Selenium recorded test as a groovy script.

You may also take a look at https://github.com/jenkinsci/credentials-plugin/pull/33

like image 30
Zandao Avatar answered Oct 01 '22 02:10

Zandao