undefined method 'filter'
Closed this issue · 9 comments
Cookbook version
2.2.0
Chef-client version
14.10.9
Platform Details
RedHat 7
Scenario:
Run basic filter_lines
Steps to Reproduce:
depends 'line' # put in metadata.rb
RECIPE:
content = <<~EOF
export var=EXAMPLE
EOF
match_pattern = /System wide functions and aliases/
filter_lines 'insert var' do
path '/etc/bashrc'
filter after: [match_pattern, content]
end
Expected Result:
Success
Actual Result:
NoMethodError: undefined method 'filter' for Custom resource line_filter_files from cookbook line
Could you try that with the filters instead of filter property? There's a typo in the documentation examples.
Thanks that was it. I added a not_if { ::File.read("/etc/bashrc").include?(content) } and it does what I want.
If you need to add that not_if something is wrong. Could you tell me what happened without the not_if?
Thanks
It simply matches the line and adds my content again. I assumed that was expected behavior. Is there another way to construct the recipe to prevent that?
content='export var=EXAMPLE' . The EOL characters are unexpected (dumb on my part).
or
content = << -EOF
line1
line2
EOF.split("\n")
Filters expect strings without EOL, or an array of strings without EOL characters.
Your example doesn't work due to the 'EOF' must be on it's own line. This works:
content = <<~EOF
export foo=bar
export bar=baz
EOF
content = content.split("\n")
I prefer the squiggle HEREDOC available since Ruby 2.3 (https://infinum.co/the-capsized-eight/multiline-strings-ruby-2-3-0-the-squiggly-heredoc) as it allows me to indent but strips the leading whitespace.
A fix for your original input is in the works.
The documentation errors and allowing lines to be specified in a string have been fixed by #141. Splitting the lines into an array before handing them to the sample filters is no longer required.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.