Search code examples
rubyregexchef-infraaws-opsworks

OpsWorks Ruby returning nil for valid regex test


In OpsWorks, I'm trying to test the number suffix on a hostname of a given node, and to extract that number if it isn't 1. If the number isn't 1, I have this regex to match the number:

/([\d]+)$­/

Which is run against a node naming scheme that follows this pattern:

  • node1
  • node2
  • node3
  • node(n...)

I've verified this works using Rubular: http://rubular.com/r/Ei0kqjaxQn

However, when I run this against an instance with OpsWorks, this match returns nil, no matter what number the hostname has at the end. The OpsWorks agent version is the latest at time of writing (4023), using Chef 12.13.37.

This is the code in the cookbook trying use the matched number:

short_app_name.to_s + node['hostname'][/([\d]+)$­/, 1].to_s + '.' + app['domains'].first

The run fails with type error no implicit conversion of nil into String. However, regex searches against that property work earlier in the recipe, when checking the node's number suffix. Is there a different method I should be using to extract the node's suffix?


Edit: app['domains'].first is populated. This line still fails with the same type error if it is swapped out with domain.com.


Solution

  • When I copy your regex and paste it into my terminal to test, there's a soft hyphen character after the dollar sign at the end of the regex, removing this makes things work:

    The website isn't showing it even when I copy it from my terminal, but a screenshot shows the issue:

    enter image description here

    That second line ('irb(main):002:0') is what I copy/pasted from your cookbook code, the character is "\xc2\xad"