0

I'm new to Chef and Test-kitchen and I'm trying to use random JSON file as attribute or environment (preferably attribute), but unfortunately I can't access the JSON values from the recipes.

I'm using the following directory structure:

uat
├── attributes
│   ├── dev.json
│   ├── .kitchen
│   │   └── logs
│   │       └── kitchen.log
│   └── prod.json
├── Berksfile
├── Berksfile.lock
├── chefignore
├── environments
│   ├── dev.json
│   └── prod.json
├── Gemfile
├── Gemfile.lock
├── .kitchen
│   ├── default-windows.yml
│   └── logs
│       ├── default-windows.log
│       └── kitchen.log
├── .kitchen.yml
├── metadata.rb
└── recipes
    ├── default.rb
    ├── prep.rb
    └── service_install.rb

This is the .kitchen.yml:

---
driver:
  name: machine
  username: sample_user
  password: sample_pass
  hostname: 192.168.1.102
  port: 5985

provisioner:
  name: chef_zero
  json_attributes: true
  environments_path: 'environments/dev'

platforms:
  - name: windows

suites:
  - name: default
    run_list:
      - recipe[uat::default]

This is the dev.json:

{
    "groupID": "Project-name",
    "directoryName": "sample_folder",
    "environmentType": "UAT",
}

This is the recipe prep.rb :

directory "C:/Users/test/#{node['directoryName']}" do
  recursive true
  action :create
end

If I create something.rb in attributes folder and with content: default['directoryName'] = 'sample_folder', it works like a charm, but I need to use a JSON file which to store parameters company wide.

Could you please help me find what I'm doing wrong.

user3283749
  • 21
  • 1
  • 5

1 Answers1

0

So a couple of issues. First, the environments_path points at a folder, not the specific file, so that should just be environments/. Second, it has to be an actual environment object, see https://docs.chef.io/environments.html#json for a description of the schema. Third, you would need to actually apply the environment to the test node:

provisioner:
  # Other stuff ...
  client_rb:
    chef_environment: dev
coderanger
  • 52,400
  • 4
  • 52
  • 75