# Tag node and exit recipe

**URL:** <https://discourse.chef.io/t/tag-node-and-exit-recipe/12661>\
**Category:** Chef Infra (archive)\
**Created:** [February 28, 2018, 3:16pm UTC](https://discourse.chef.io/t/tag-node-and-exit-recipe/12661 "2018-02-28T15:16:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tiago\_Cruz](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/tiago_cruz/32/49_2.png) [@Tiago\_Cruz](https://discourse.chef.io/u/Tiago_Cruz)\
**Post date:** [February 28, 2018, 3:16pm UTC](https://discourse.chef.io/t/tag-node-and-exit-recipe/12661/1 "2018-02-28T15:16:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jdunn](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/jdunn/32/1800_2.png) [@jdunn](https://discourse.chef.io/u/jdunn)\
**Post date:** [March 8, 2018, 4:45pm UTC](https://discourse.chef.io/t/tag-node-and-exit-recipe/12661/2 "2018-03-08T16:45:28Z")

</div>

Do you mean Chef node tags?

> **[About Recipes](https://docs.chef.io/recipes/)**
>
> A recipe is the most fundamental configuration element within the organization. A recipe:
> Is authored using Ruby, which is a programming language designed to read and behave in a predictable manner Is mostly a collection of resources, defined using...

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') }`

---

<div class="post-metadata">

**Author:** ![Tiago\_Cruz](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/tiago_cruz/32/49_2.png) [@Tiago\_Cruz](https://discourse.chef.io/u/Tiago_Cruz)\
**Post date:** [March 8, 2018, 5:15pm UTC](https://discourse.chef.io/t/tag-node-and-exit-recipe/12661/3 "2018-03-08T17:15:17Z")

</div>

I’m using some like this

```auto
  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!

---

<div class="post-metadata">

**Author:** ![jdunn](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/jdunn/32/1800_2.png) [@jdunn](https://discourse.chef.io/u/jdunn)\
**Post date:** [March 8, 2018, 6:03pm UTC](https://discourse.chef.io/t/tag-node-and-exit-recipe/12661/4 "2018-03-08T18:03:24Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![kallistec](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/kallistec/32/23_2.png) [@kallistec](https://discourse.chef.io/u/kallistec)\
**Post date:** [March 8, 2018, 8:03pm UTC](https://discourse.chef.io/t/tag-node-and-exit-recipe/12661/5 "2018-03-08T20:03:42Z")

</div>

> [@Tiago\_Cruz](#):
>
> ```auto
> 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
> 
> ```

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?

---

<div class="post-metadata">

**Author:** ![Tiago\_Cruz](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/tiago_cruz/32/49_2.png) [@Tiago\_Cruz](https://discourse.chef.io/u/Tiago_Cruz)\
**Post date:** [March 8, 2018, 9:46pm UTC](https://discourse.chef.io/t/tag-node-and-exit-recipe/12661/6 "2018-03-08T21:46:52Z")

</div>

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