2016-09-22 12:43:57 +00:00
---
title: About the parse_config_file Resource
---
2016-10-20 10:57:06 +00:00
# parse\_config\_file
2016-09-22 12:43:57 +00:00
Use the `parse_config_file` InSpec audit resource to test arbitrary configuration files. It works in the same way as `parse_config`. Instead of using a command output, this resource works with files.
2016-09-27 19:03:23 +00:00
## Syntax
2016-09-22 12:43:57 +00:00
A `parse_config_file` InSpec audit resource block declares the location of the configuration file to be tested, and then which settings in that file are to be tested.
describe parse_config_file('/path/to/file', { data_config_option: value } ) do
its('setting') { should eq 1 }
end
or:
options = {
2017-04-26 21:18:14 +00:00
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
2016-09-22 12:43:57 +00:00
multiple_values: true
}
describe parse_config_file('path/to/file', options) do
its('setting') { should eq 1 }
end
where each test
* Must declare the location of the configuration file to be tested
* Must declare one (or more) settings to be tested
* May run a command to `stdout`, and then run the test against that output
* May use options to define how configuration data is to be parsed
2016-09-27 19:03:23 +00:00
## Options
2016-09-22 12:43:57 +00:00
This resource supports the following options for parsing configuration data. Use them in an `options` block stated outside of (and immediately before) the actual test:
options = {
2017-04-26 21:18:14 +00:00
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
2016-09-22 12:43:57 +00:00
multiple_values: true
}
describe parse_config_file('path/to/file', options) do
its('setting') { should eq 1 }
end
2016-09-27 19:03:23 +00:00
## Matchers
2016-09-22 12:43:57 +00:00
This InSpec audit resource has the following matchers:
2017-04-26 21:18:14 +00:00
### assignment_regex
2016-09-22 12:43:57 +00:00
2017-04-26 21:18:14 +00:00
Use `assignment_regex` to test a key value using a regular expression:
2016-09-22 12:43:57 +00:00
'key = value'
may be tested using the following regular expression, which determines assignment from key to value:
2017-04-26 21:18:14 +00:00
assignment_regex: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/
2016-09-22 12:43:57 +00:00
2016-09-27 19:03:23 +00:00
### be
2016-09-22 12:43:57 +00:00
<%= partial "/shared/matcher_be" %>
2016-09-27 19:03:23 +00:00
### cmp
2016-09-22 12:43:57 +00:00
<%= partial "/shared/matcher_cmp" %>
2016-09-27 19:03:23 +00:00
### comment_char
2016-09-22 12:43:57 +00:00
Use `comment_char` to test for comments in a configuration file:
comment_char: '#'
2016-09-27 19:03:23 +00:00
### eq
2016-09-22 12:43:57 +00:00
<%= partial "/shared/matcher_eq" %>
2016-09-27 19:03:23 +00:00
### include
2016-09-22 12:43:57 +00:00
<%= partial "/shared/matcher_include" %>
2017-04-26 21:18:14 +00:00
### key_values
2016-09-22 12:43:57 +00:00
2017-04-26 21:18:14 +00:00
Use `key_values` to test how many values a key contains:
2016-09-22 12:43:57 +00:00
key = a b c
contains three values. To test that value to ensure it only contains one, use:
2017-04-26 21:18:14 +00:00
key_values: 1
2016-09-22 12:43:57 +00:00
2016-09-27 19:03:23 +00:00
### match
2016-09-22 12:43:57 +00:00
<%= partial "/shared/matcher_match" %>
2016-09-27 19:03:23 +00:00
### multiple_values
2016-09-22 12:43:57 +00:00
Use `multiple_values` if the source file uses the same key multiple times. All values will be aggregated in an array:
# # file structure:
# key = a
# key = b
# key2 = c
params['key'] = ['a', 'b']
params['key2'] = ['c']
To use plain key value mapping, use `multiple_values: false`:
# # file structure:
# key = a
# key = b
# key2 = c
params['key'] = 'b'
params['key2'] = 'c'
2016-09-27 19:03:23 +00:00
### standalone_comments
2016-09-22 12:43:57 +00:00
Use `standalone_comments` to parse comments as a line, otherwise inline comments are allowed:
'key = value # comment'
params['key'] = 'value # comment'
Use `standalone_comments: false`, to parse the following:
'key = value # comment'
params['key'] = 'value'
2016-09-27 19:03:23 +00:00
## Examples
2016-09-22 12:43:57 +00:00
The following examples show how to use this InSpec audit resource.
2016-09-27 19:03:23 +00:00
### Test a configuration setting
2016-09-22 12:43:57 +00:00
describe parse_config_file('/path/to/file.conf') do
its('PARAM_X') { should eq 'Y' }
end
2016-09-27 19:03:23 +00:00
### Use options, and then test a configuration setting
2016-09-22 12:43:57 +00:00
describe parse_config_file('/path/to/file.conf', { multiple_values: true }) do
its('PARAM_X') { should include 'Y' }
end
2016-09-28 23:28:31 +00:00
## Test a file with an ini-like structure (such as a yum.conf)
describe parse_config_file('/path/to/yum.conf') do
its('main') { should include('gpgcheck' => '1') }
end