Tuesday, July 26, 2016

Using Ansible For Cloudbees Jenkins Tile Configuration

As with most of these posts, I ran into an issue with something and I am going to explain what I did to solve it.  The issue this time was trying to reapply configuration to a Jenkins Slave after a redeploy/upgrade of the Cloudbees Jenkins Tile for Pivotal Cloud Foundry (PCF).  When upgrading any PCF Tile, any information not stored on persistent disk will be deleted after the upgrade completes.  The biggest challenge with this PCF Tile is any supporting software/tools (NodeJS, NPM, etc) on the Jenkins Slaves for building applications will need to be installed every time Jenkins is updated.

There are a couple of ways to solve this issue.  One way is to use Docker Jenkins Slaves for your build environment. Here is a link for the Cloudbees Docker Hub page.  They have pre-built Docker images for you to use so you don't have to configure the build environments manually.  The use of Docker is something I want to investigate further and will post another blog entry for that topic.  Another way is to implement Ansible and the use of playbooks to quickly configure the build environment back to before the upgrade.

In a previous post, I talked about how to setup Ansible and creating your first playbook.  Below I will go through a sample playbook to put back various tools after an upgrade.

Sample Jenkins Slave Reconfigure Playbook

1:  ---  
2:  - hosts: jenkins-build-env     #server or group of server detailed in your Ansible host file  
3:    remote_user: vcap            #Cloudbees Jenkins Tile Linux User  
4:    become: yes                  #Elevate your privileges for running the below commands  
5:    tasks:                       #Start of the commands that will run  
6:     - name: copy over latest cf installer  
7:       copy: src=/file_location/cf dest=/var/vcap/packages/cloudfoundry-cli/bin   
8:     - name: copy over debian sources.list  
9:       copy: src=/file_location/sources.list dest=/etc/apt  
10:    - name: copy over npmrc config file  
11:      copy: src=/file_location/.npmrc dest=/home/vcap/.npmrc  
12:              owner=vcap group=vcap mode=0600  
13:    - name: update debian repos  
14:      apt: update_cache=yes  
15:    - name: install debian package  
16:      apt: name=package-name state=present force=yes  
17:    - name: install npm package  
18:      npm: name=package-name state=present global=yes registry=http://custom-registry.com executable=/var/vcap/data/jenkins_slave/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/JenkinsNodeVersionName/node_extracted_folder/bin/npm  

In lines 1 - 5, these lines are the basic start to any playbook.  Inside of the tasks block (lines 6 - 18), the playbook is performing the following actions.  Just to note, all of these actions inside of the playbook can be done with the base install of Ansible and does not require any special plugins.

  • Upgrading the CF CLI inside of Jenkins Slave to a later version
  • Modifying the Debian repository list
  • Copying over the npm configuration file for the vcap user and setting permissions
  • Performing an apt-get update
  • Installing a debian package using the new repository list
  • Installing a npm package from a custom npm repository and using a specific npm installation

For the file copies, I suggest that you create a place on the server where Ansible is installed to hold a small file repository.  This way you can copy over files to your Ansible targets easily.

Summary
Using this sample, you can start building automated scripts to reconfigure any PCF Tile customizations that you make.  It will save you time in the long run and make sure your results are consistent across your environment.

~RRRII

4 comments: