3

How can I re-use all of my attributes section, except for maven

     suites:
       - name: DEV

     attributes: 
          'ant': &ant
            'version': '1.9.3'
            'home': '/my/path/ant'
          'maven':  
            'version': 3
            'm2_home': '/my/path/maven'
            '3': 
              'version': '3.2.1'
              'maven_rc': 
            'opts': ''

For my TESTING instance, I would like to inherit all of the above attributes, except for maven, which I'd like to override (different version):

    - name: TESTING

     attributes:  
          <<: *ant        # re-use ant as it's the same configuration
          'maven':        # different version for TESTING
            'version': 3
            'm2_home': '/my/path/maven'
            '3': 
              'version': '3.0.5'
              'maven_rc': 
            'opts': ''
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
  • 1
    You want to read this: http://stackoverflow.com/questions/8466223/reuse-a-block-of-code-in-yaml – sethvargo Apr 25 '14 at 03:05
  • Thanks, Seth. I read that article and updated my question. Could you please take a look? – Kevin Meredith Apr 25 '14 at 18:30
  • How about creating a `base` or `default` run_list, that you then inherit from in both `dev` and `qa`, with qa adding a recipe on top? Note that you would have to inherit only `run_list`, not the entire suite. – JeanMertz Apr 27 '14 at 13:31
  • @JeanMertz - could you please post an answer? (1) for credit (2) I'm not sure how to use inheritance in YAML – Kevin Meredith Apr 28 '14 at 13:13
  • @KevinMeredith turns out I was incorrect, it seems in YAML you *can't* inherit and then append any extra values in the same namespace, sorry. – JeanMertz May 06 '14 at 15:14

2 Answers2

2

You will want to edit your .kitchen.yml under the platform for any attributes you want to be universal. If you would like to change them then you just put them under the suites section with a different value.

platforms:
  - name: ubuntu-12.04
    driver_config:
      box: ubuntu-12.04
    run_list:
      - recipe[apt::default]
    attributes:
      ant: &ant
        version: '1.9.3'
        home: '/my/path/ant'
      maven:
        version: 3
        m2_home: '/my/path/maven'
        3:
          version: '3.2.1'
          maven_rc:
        opts: ''

suites:
  - name: default
    run_list:
      - recipe[cookbook::default]
    attributes:

  - name: testing
    run_list:
      - recipe[cookbook::default]
    attributes:
      maven:
        3:
          version: '3.0.5'

You can then access the attributes inside the recipes by calling node['maven']['version'] to retrieve the current value which will change depending if it is overwritten in the suites section.

jarsever
  • 690
  • 1
  • 7
  • 15
1

Worth noting that you can use Erb syntax in your kitchen.yml

<% my_attrs = {'foo' => 'bar'} %> suites: - name: one attributes: <%= my_attrs.to_yaml %> - name: two attributes: <%= my_attrs.merge('baz' => 'whatever').to_yaml %>

Overusing this can quickly lead to unreadable configs, but in moderation it can be useful.

coderanger
  • 52,400
  • 4
  • 52
  • 75