Tag node and exit recipe


#1

Hello!

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.

Thanks


#2

Do you mean Chef node tags?

https://docs.chef.io/recipes.html#use-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') }


#3

I’m using some like this

  if not tagged?("sbs-api")
    tag('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")
  end

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?

Thanks!


#4

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.


#5

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?


#6

@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!