2 minute read

r10k code manager can be used to push puppet code from a remote repository such as github. This way, the puppet code is maintained in a version control system, minimizing the impact of a failing or malicious code. Code can be reverted easily to the latest stable version. Also multiple users can collaborate on one project of code.

In this process, users push code to the central repository from their machines, this code can then be deployed on the Puppet server using r10k.

Installation

To install r10k you must have the ruby package manager gem installed.

gem install r10k

If you have an old version of Ruby installed it’s possible that the above command will result in an error. This is because it was using the default ‘gem’ PATH for Ruby version less than 2.3 which is not supported by r10k. To install r10k, use the gem which comes with the Puppet install, usually at /opt/puppetlabs/puppet/bin/gem. So the command will look like: /opt/puppetlabs/puppet/bin/gem install r10k. This will instal r10k under /opt. Enable r10k PATH ln -s /opt/puppetlabs/puppet/bin/r10k /bin/r10k

Configuration

Repository Setup

On github, the name of the default repository is master. This name doesn’t work well with r10 deployment on Puppet server due to the confusion between Puppet Master and Master branch. For the deployment to work with Puppet, change the branch name to production. This way all the code in the production branch will be deployed to the production environment on running Puppet.

r10k Setup

To use r10k with Puppet, create a configuration file for r10k at : /etc/puppetlabs/r10k/r10k.yaml

---
cachedir: '/var/cache/r10k'
sources:
  labrepo:
    remote: 'https://USERNAME:PASSWORD@github.com/somegithubrepo/control-repo.git'
    basedir: '/etc/puppetlabs/code/environments'
  • cachedir - rk10k cache location.
  • sources - Sources where code is pulled from.
    • remote - The remote repository location. If the repository is private, you can give the username and password in the URL as above. If you are using a public repository get the HTTPS repo URL and enter that here.
    • basedir - The directory where the code is deployed. r10k matches the branch name to an environment’s name inside this directory.

Usage

To deploy code from the repository to the Puppet server, run:

r10k deploy environment -pv

Puppetfile

When you are writing and deploying your code using r10k. You don’t need to create/import the directory structure of any forge modules that you use in your code. A simple solution is to create a puppetfile and write the module names being used inside the file. With this kind of setup, forge modules are installed when the code is deployed using r10k. One downside to this is that the Puppetfile will not resolve dependencies for any modules inside it. You must exclusively include all the dependencies inside the module and also any other branched dependencies.

Create the puppetfile in the the root of your environment, example production. Example puppetfile:

mod 'souldo-remotedesktop', '1.0.2'
mod 'souldo-wmi', '0.1.0'
mod 'puppetlabs-powershell', '2.3.0'
mod 'puppetlabs-stdlib', '6.1.0'
mod 'puppetlabs-registry', '2.1.0'
mod 'puppet-windows_firewall', '2.0.2'
mod 'puppetlabs-chocolatey', '5.0.0'

Check dependencies on The Forge

Each module on the forge has it’s dependencies listed under the Dependencies tab.

It also shows the line you must add to your Puppetfile to install the module.

Categories:

Updated: