Configure Jenkins 2.0 with Ansible

﹥>﹥吖頭↗ 提交于 2019-12-04 11:06:06

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.

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

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

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!