4

I am using Ansible for provision our servers, I installed the Jenkins 2.0 but it is becomeing with a startup configuration when I open the web UI. How can I do it with Ansible or shell or jenkins-cli. CentOS 7, Ansible 2.0.1.0. So,

  1. Installing Jenkins 2.0 from http://pkg.jenkins-ci.org/redhat-rc/jenkins-2.0-1.1.noarch.rpm rpm.
  2. Install java with yum.
  3. Service start jenkins.
  4. Open 192.168.46.10:8080, which is opening the Jenkins.
  5. In Web UI adding the initial admin password.
  6. In web UI select and install plugins.
  7. In web UI create a new admin user.

The 5,6,7 points are all the startup config of the new Jenkins. I haven't idea how we can install it autmatically.

Edit 1:

The 1,2,3 point is already done, just I didn't share because it is not necessary, because I only need an advice how can I configure the Jenkins. But now I add it to my question.

---
- name: Jenkins - install | Install java
  yum: name=java state=installed

- name: Jenkins - install | Install Jenkins 2.0
  yum: pkg=http://pkg.jenkins-ci.org/redhat-rc/jenkins-2.0-1.1.noarch.rpm state=installed

- name: Jenkins - install | Start and enable Jenkins 2.0
  service: name=jenkins state=started enabled=yes
CSchulz
  • 10,882
  • 11
  • 60
  • 114
PumpkinSeed
  • 2,945
  • 9
  • 36
  • 62
  • And what did you do so far? We are not going to do it for you. – Tymoteusz Paul Apr 13 '16 at 17:25
  • @TymoteuszPaul Nobody ask you to do it for me, because the 1,2,3 points are already done. I just don't know how can I manage the Jenkins configuration from cli. If you tell it to me it's enough for me, because I can use the shell module. I only used Jenkins from the Web UI which is not good when I want to install it automaticaly for a remote host. – PumpkinSeed Apr 13 '16 at 22:32
  • @TymoteuszPaul I edited the question, now you can comment helpfully. – PumpkinSeed Apr 14 '16 at 08:40
  • When you skip the wizard what do you do to install the basic plugins? Have you found a way to setup the tools (Jdk, Git, Maven) and credentials? – Mark Gargan Jun 01 '17 at 09:54
  • 1
    @MarkGargan I just did it manually, I copied them to the folder I think, but I don't remember. – PumpkinSeed Jun 01 '17 at 10:30

4 Answers4

8

You can create an initialization script (in groovy) to add an admin account. Script should be present in $JENKINS_HOME/init.groovy.d/*.groovy. See Jenkins CI Wiki for more details.

Here's an example.

security.groovy.j2 file:

#!groovy
import java.util.logging.Level
import java.util.logging.Logger
import hudson.security.*
import jenkins.model.*

def instance = Jenkins.getInstance()
def logger = Logger.getLogger(Jenkins.class.getName())

logger.log(Level.INFO, "Ensuring that local user '{{ jenkins.admin_username }}' is created.")

if (!instance.isUseSecurity()) {
    logger.log(Level.INFO, "Creating local admin user '{{ jenkins.admin_username }}'.")

    def strategy = new FullControlOnceLoggedInAuthorizationStrategy()
    strategy.setAllowAnonymousRead(false)

    def hudsonRealm = new HudsonPrivateSecurityRealm(false)
    hudsonRealm.createAccount("{{ jenkins.admin_username }}", "{{ jenkins.admin_password }}")

    instance.setSecurityRealm(hudsonRealm)
    instance.setAuthorizationStrategy(strategy)
    instance.save()
}

How to use in Ansible playbook:

- name: Create initialization scripts directory
  file: path={{ jenkins.home }}/init.groovy.d
        state=directory
        owner=jenkins
        group=jenkins
        mode=0775

- name: Add initialization script to setup basic security
  template: src=security.groovy.j2
            dest={{ jenkins.home }}/init.groovy.d/security.groovy

I was inspired by this GitHub reposiotry.

Anton Pelykh
  • 2,274
  • 1
  • 18
  • 21
  • The GitHub link returns 404 now. The closest I could find is https://github.com/geerlingguy/ansible-role-jenkins/blob/master/templates/basic-security.groovy.j2 – myahl Aug 10 '21 at 21:36
4

I found a solution, this is turn off the setup wizard, after it I was able to change config files.

- name: Jenkins - configure | Turn off Jenkins setup wizard
  lineinfile: dest=/etc/sysconfig/jenkins regexp='^JENKINS_JAVA_OPTIONS=' line='JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Djenkins.install.runSetupWizard=false"'
  notify: restart jenkins
PumpkinSeed
  • 2,945
  • 9
  • 36
  • 62
  • I'm seeing a similar problem, but this option `-Djenkins.install.runSetupWizard=false` doesn't completely solve it. It does skip the setup wizard, but when I try to open the Jenkins UI or interact with it from `jenkins-cli.jar` it still asks for a username and password. Did you have this problem? – laffoyb Apr 21 '16 at 11:04
  • We have existed users and configs which we are copying to the Jenkins config folder, and it is solve the problem. @laffoyb – PumpkinSeed Apr 21 '16 at 14:34
  • maybe this will help you @laffoyb https://wiki.jenkins-ci.org/display/JENKINS/Disable+security – BDuelz Jun 16 '16 at 20:53
2

The above solution didn't work for me but give me hint and this is the solution that worked for me on Ubuntu:

- name: Configure JVM Arguments
  lineinfile:
    dest: /etc/default/jenkins
    regexp: '^JAVA_ARGS='
    line: 'JAVA_ARGS="-Djava.awt.headless=true -Djenkins.install.runSetupWizard=false"'
  notify:
    - Restart Jenkins
Arbab Nazar
  • 22,378
  • 10
  • 76
  • 82
  • 5
    Just wanted to add that `/etc/default/jenkins` is for Debian/Ubuntu variants whereas `/etc/sysconfig/jenkins` is for CentOS/RedHat variants. – Amit Oct 11 '16 at 17:01
1

On Ubuntu 16.04 with Jenkins installed using apt-get, this works:

- name: "Turn off Jenkins setup wizard"
  lineinfile:
      dest: /etc/init.d/jenkins
      regexp: '^JAVA_ARGS='
      line: 'JAVA_ARGS="-Djava.awt.headless=true -Djenkins.install.runSetupWizard=false"'
      insertbefore: '^DAEMON_ARGS='
  notify: restart jenkins

- name: restart jenkins
  service: name=jenkins state=restarted

You will still have to setup the security though!

Bruno Bossola
  • 1,128
  • 1
  • 13
  • 24