1
votes

I'm currently testing a new chef cookbook for Zabbix monitoring, and had my first successful 'chef' using this cookbook. I was, however, forced to make a change to one of the attributes, because it will change from server to server. I decided to use a data bag, and I'm having difficulty retrieving and assigning the attribute.

Here are the contents of the data bag:

    [user@workstation chef-repo]# knife data bag show zabbix_psk psk_syslog
    WARNING: Unencrypted data bag detected, ignoring any provided secret options.
    id:  psk_syslog
    psk: PSK 060

I did not assign the attribute in the attributes file, but rather in the recipe: default.rb

keyvalue = data_bag('zabbix_psk')
node.normal['zabbix_agentd']['psk_number'] = keyvalue['psk_syslog']['psk']

And then I assign it in a template:

TLSPSKIdentity=<%= node['zabbix_agentd']['psk_number'] %>

When I attempt to Chef, I receive the following error and dump:

TypeError
---------
no implicit conversion of String into Integer

Cookbook Trace:
---------------
  /var/chef/cache/cookbooks/zabbix_agentd/recipes/default.rb:28:in `[]'
  /var/chef/cache/cookbooks/zabbix_agentd/recipes/default.rb:28:in `from_file'

Relevant File Content:
----------------------
/var/chef/cache/cookbooks/zabbix_agentd/recipes/default.rb:

 21:  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 22:  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 23:  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 24:  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 25:  # THE SOFTWARE.
 26:
 27:  keyvalue = data_bag('zabbix_psk')
 28>> node.normal['zabbix_agentd']['psk_number'] = keyvalue['psk_syslog']['psk']
 29:
 30:  case node['platform']
 31:  when 'centos', 'redhat'
 32:    include_recipe 'zabbix_agentd::yum_repo'
 33:  end
 34:
 35:  package 'zabbix-agent'
 36:
 37:  directory node['zabbix_agentd']['log_dir'] do

System Info:
------------
chef_version=13.6.0
platform=centos
platform_version=7.4.1708
ruby=ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
program_name=chef-client worker: ppid=11127;start=14:18:48;
executable=/opt/chef/bin/chef-client


Running handlers:
[2017-12-14T14:18:56-06:00] ERROR: Running exception handlers
Running handlers complete
[2017-12-14T14:18:56-06:00] ERROR: Exception handlers complete
Chef Client failed. 0 resources updated in 07 seconds
[2017-12-14T14:18:56-06:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[2017-12-14T14:18:56-06:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2017-12-14T14:18:56-06:00] ERROR: no implicit conversion of String into Integer
[2017-12-14T14:18:56-06:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

I'm having difficulty understanding what's wrong with the node.normal assignment in default.rb. I'm new to both Chef and Ruby, so I really appreciate the help. I should also note that I could not use the method get_databag_item; aparently that method did not exist?

1

1 Answers

1
votes

data_bag('zabbix_psk') returns an array of the names of the items in the bag, not the data in all the items. You want data_bag_item('zabbix_psk', 'psk_syslog') instead, and then adjust your usage of the value returned.