Tag node and exit recipe


There is some way to tag some node and exit the chef-run?
And do not exit, if the tag is already there?

'Cause we have some checks that need the tag, and is boring to tag by hand.


Do you mean Chef node tags?


It’s better to write a guard on the resources needing tags rather than to exit the whole Chef run, e.g.

only_if { tagged?('wat') }

I’m using some like this

  if not tagged?("sbs-api")
    raise("Tag 'sbs-api' not found. Please run on your machine: \n
    $ knife tag create #{node['fqdn']} sbs-api \n 
    $ knife vault refresh deploy_keys sbs_deployer \n 
     right now and run this recipe again")

But this ‘raise’ quit without save my ‘tag’… so, I need to add the tag by hand.
Do you have a better way to do this?


This is because raise will exit the Chef run with a failure and the node object isn’t saved as a result. Try node.save right before the raise.

1 Like

I’m confused by this code. Why would you create the tag for the user if you’re just gonna fail and tell them to tag it?

@jdunn, awesome… node.save did the trick, thanks!!

@kallistec, now I don’t need to ask the user to create the tag, only the vault refresh!

Thanks guys!