How can a recipe get a full list of the server tags?

6.2k Views Asked by At

How can a recipe get a full list of the server tags?

or

How can a recipe check if a certain tag is set/present?

BTW: tagged? didn't work for me

2

There are 2 best solutions below

2
On

You can get all the tags of a node in recipe by node.tags or node[:tags]. So to check, if the certain tag is present, you need to:

node[:tags].include? 'mytag'

To get the full list of all the tags on all nodes you have to use search.

search( :node, '*:*' ).collect(:tags).flatten.uniq

But tagged? 'mytag' should work actually. Show your code.

1
On

There are a couple of things to keep in mind with Chef tags:

  1. You cannot use them in attribute files. It would be awesome if you could, but it just doesn't work. This is what you will see if you try: ERROR: Undefined node attribute or method 'tagged?' on node'.
  2. This may be fixed in later versions of Chef, but there was a time when you would get errors when checking for tags if the node was not tagged at all.

Those two things aside, I use them a lot in recipes and they work very well.

if tagged?('legacy')
  do legacy things
end

or

return if tagged?('ignore')

Those types of uses should work, no problem, from your recipes.