使用类似的"套房"在测试厨房中进行代码重用?

时间:2014-04-24 20:39:18

标签: yaml chef

除了attributes

之外,如何重新使用我的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': ''

对于我的TESTING实例,我想继承上述所有属性,maven除外,我想覆盖(不同版本):

    - 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': ''

2 个答案:

答案 0 :(得分:2)

您需要在平台下编辑.kitchen.yml以获取您想要通用的任何属性。如果您想更改它们,那么您只需将它们放在具有不同值的套件部分下。

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'

然后,您可以通过调用node['maven']['version']来访问配方中的属性,以检索当前值,该值将根据套件部分中的覆盖而改变。

答案 1 :(得分:1)

值得注意的是,您可以在kitchen.yml中使用Erb语法

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

过度使用这种情况很快就会导致无法读取的配置,但在适度的情况下它可能很有用。

相关问题