• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

KJBweb

Technical Notes & How To's

  • Home
  • How-to
  • Quick Fixes
  • Examples
  • Hardware
You are here: Home / Examples / Looping through Attributes within Chef Recipes

Looping through Attributes within Chef Recipes

November 4, 2016Leave a Comment

In a quest to make it easier for myself and others to modify my Chef recipes, I thought it was about time I started to use attributes much more seriously than I had been doing previously.

Soon enough, I needed to loop through a hash of attributes in order to make my recipe’s code as clean and efficient as possible, and if you are relatively new to Chef then you might find yourself running into the same syntactical nightmare I did.

Here’s how I did it.

The following is an extract from my attributes/default.rb file:

default[:software] = {
  :repos => {
    :remove => [
      'CentOS-Base',
      'CentOS-Debuginfo',
      'CentOS-fasttrack',
      'CentOS-Media',
      'CentOS-Vault'
  ],

The aim here is to use the yum cookbook to remove each of the listed repositories from the base image.

The code below addresses the part of the hash we wish to work with, takes the value contained within and runs through each iteration, passing i which contains the name of the repository:

# Remove base image repo's
node[:software][:repos][:remove].each do |i|
  yum_repository i do
    action :delete
  end
end

Great! We have supplied a single piece of information stored in a relatively simple hash and provided it to the yum_repository statement within the recipe.

Though, we now want to configure yum to use some new repositories, and this requires multiple bits of information to be supplied for each repository we wish to add…

Take a look at the next part of my attributes/default.rb file:

:add => {
  :epel => {
    'name' => 'epel',
    'description' => 'Extra Packages for Enterprise Linux',
    'baseurl' => 'http://hostname/repo/epel/6/$basearch/',
    'gpgkey' => 'http://hostname/repo/epel/RPM-GPG-KEY-EPEL-6',
    'gpgcheck' => true
  },
  :centos => {
    'name' => 'centos',
    'description' => 'CentOS Base',
    'baseurl' => 'http://hostname/repo/centos/$releasever/os/$basearch/',
    'gpgkey' => nil,
    'gpgcheck' => false
  }
}

Here we can see each repository has a set of defined key’s and associated values, we need to supply these to the yum_repository statement a little more gracefully this time, rather than just looping through blindly.

# Add custom repo's
node[:software][:repos][:add].each do |k, v|
  yum_repository v['name'] do
    description v['description']
    baseurl v['baseurl']
    gpgkey v['gpgkey']
    gpgcheck v['gpgcheck']
    action :create
  end
end

To a ruby guru, this may be pretty obvious, though it took a while to figure out both the key and value need to passed into each iteration of the loop, and then each value needs to be requested by it’s key using the value[‘key’] method you see above.

If you know of a better way to do this then feel free to comment below as I’d be interested to see it.

Filed Under: Examples Tagged With: chef, ruby

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • How To Get SMETS1/2 Smart Meter Data into HomeAssistant the Easy Way
  • How to Install 7zip on Ubuntu 18.04 LTS (Bionic Beaver)
  • How to Install the NewRelic MySQL Plugin
  • Repair “Repository Error: database disk image is malformed” YUM Issue
  • How to Check NTP Connectivity and Function on the Command Line

Recent Comments

  • himanshu on How to Test Memcached Connectivity
  • moun on How to Perform a GET Request with CURL via the Command Line
  • Karl on How to Install rar/unrar on Ubuntu 18.04 LTS (Bionic Beaver)
  • John on How to Install rar/unrar on Ubuntu 18.04 LTS (Bionic Beaver)
  • How to Install rar/unrar on Ubuntu 16.04 LTS (Xenial) - KJBweb on How to Install rar/unrar on Ubuntu 18.04 LTS (Bionic Beaver)

Archives

  • November 2022
  • February 2019
  • August 2018
  • November 2016

Categories

  • Examples
  • Hardware
  • How-to
  • Quick Fixes

Copyright © 2023 · WordPress · Log in