# Node.save?

**URL:** <https://discourse.chef.io/t/node-save/2080>\
**Category:** Chef Infra (archive)\
**Created:** [September 9, 2011, 10:46am UTC](https://discourse.chef.io/t/node-save/2080 "2011-09-09T10:46:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dennybaa](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/dennybaa/32/286_2.png) [@dennybaa](https://discourse.chef.io/u/dennybaa)\
**Post date:** [September 9, 2011, 10:46am UTC](https://discourse.chef.io/t/node-save/2080/1 "2011-09-09T10:46:59Z")

</div>

Hey all,

How do you think why we meet the node.save command in many recipes? For  
example, I’ve seen it in the mysql recipe.  
As far as I understand any attributes that are set in recipes are  
automatically persisted.  
So what for is the ‘node.save’ command then?

Thanks for any ideas

Denis

---

<div class="post-metadata">

**Author:** ![Ranjib\_Dey](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Ranjib\_Dey](https://discourse.chef.io/u/Ranjib_Dey)\
**Post date:** [September 9, 2011, 11:16am UTC](https://discourse.chef.io/t/node-save/2080/2 "2011-09-09T11:16:15Z")

</div>

its saved at the end of the run automatically. If you are setting an  
attribute in the recipe and trying to reuse it in the same recipe afterwards  
u make an explicit node.save call.

On Fri, Sep 9, 2011 at 4:16 PM, Denis Barishev [denis.barishev@gmail.com](mailto:denis.barishev@gmail.com)wrote:

> Hey all,
> 
> How do you think why we meet the node.save command in many recipes? For  
> example, I've seen it in the mysql recipe.  
> As far as I understand any attributes that are set in recipes are  
> automatically persisted.  
> So what for is the 'node.save' command then?
> 
> Thanks for any ideas
> 
> Denis

---

<div class="post-metadata">

**Author:** ![dennybaa](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/dennybaa/32/286_2.png) [@dennybaa](https://discourse.chef.io/u/dennybaa)\
**Post date:** [September 9, 2011, 11:30am UTC](https://discourse.chef.io/t/node-save/2080/3 "2011-09-09T11:30:35Z")

</div>

Yeah. I suspected this. What if run fails will be the attribute saved  
then?  
Talking about reusing the attribute in the same recipe during the same  
run. I sure the attribute is available as soon as it set. There's no a  
particular need to do node.save.  
So I don't quite understand that what you mean by this, Ranjib.

Denis

On Fri 09 Sep 2011 03:16:15 PM MSD, Ranjib Dey wrote:

> its saved at the end of the run automatically. If you are setting an  
> attribute in the recipe and trying to reuse it in the same recipe  
> afterwards u make an explicit node.save call.
> 
> On Fri, Sep 9, 2011 at 4:16 PM, Denis Barishev  
> \<[denis.barishev@gmail.com](mailto:denis.barishev@gmail.com) [mailto:denis.barishev@gmail.com](mailto:denis.barishev@gmail.com)\> wrote:
> 
> ```
> Hey all,
> 
> How do you think why we meet the node.save command in many
> recipes? For example, I've seen it in the mysql recipe.
> As far as I understand any attributes that are set in recipes are
> automatically persisted.
> So what for is the 'node.save' command then?
> 
> Thanks for any ideas
> 
> Denis
> 
> ```

---

<div class="post-metadata">

**Author:** ![Brian\_Akins](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/brian_akins/32/298_2.png) [@Brian\_Akins](https://discourse.chef.io/u/Brian_Akins)\
**Post date:** [September 9, 2011, 11:31am UTC](https://discourse.chef.io/t/node-save/2080/4 "2011-09-09T11:31:04Z")

</div>

On Sep 9, 2011, at 7:16 AM, Ranjib Dey wrote:

> its saved at the end of the run automatically. If you are setting an attribute in the recipe and trying to reuse it in the same recipe afterwards u make an explicit node.save call.

No, you don't have to save to use later in the same run. node.save persists it on the chef-server. This is useful to do if you know other nodes may need this node's attributes "right away." One case in mysql master/slave configurations that was discussed on the list in the last few days.

Also, correct me if I am wrong, if a chef-client run fails, then node.save is not automatically called at the end.

--Brian

---

<div class="post-metadata">

**Author:** ![dennybaa](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/dennybaa/32/286_2.png) [@dennybaa](https://discourse.chef.io/u/dennybaa)\
**Post date:** [September 9, 2011, 11:33am UTC](https://discourse.chef.io/t/node-save/2080/5 "2011-09-09T11:33:45Z")

</div>

On 09/09/2011 03:31 PM, Brian Akins wrote:

> On Sep 9, 2011, at 7:16 AM, Ranjib Dey wrote:
> 
> > its saved at the end of the run automatically. If you are setting an attribute in the recipe and trying to reuse it in the same recipe afterwards u make an explicit node.save call.
> 
> No, you don't have to save to use later in the same run. node.save persists it on the chef-server. This is useful to do if you know other nodes may need this node's attributes "right away." One case in mysql master/slave configurations that was discussed on the list in the last few days.  
> Thanks for clarification, Brian!

> Also, correct me if I am wrong, if a chef-client run fails, then node.save is not automatically called at the end.

Yeah, this moment is still of interest, right.

Denis

---

<div class="post-metadata">

**Author:** ![Ranjib\_Dey](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Ranjib\_Dey](https://discourse.chef.io/u/Ranjib_Dey)\
**Post date:** [September 9, 2011, 11:55am UTC](https://discourse.chef.io/t/node-save/2080/6 "2011-09-09T11:55:40Z")

</div>

node.save is not invoked if the run fails, but i guess we can hack it inside  
a report handler and save it there.

On Fri, Sep 9, 2011 at 5:03 PM, Denis Barishev [denis.barishev@gmail.com](mailto:denis.barishev@gmail.com)wrote:

> On 09/09/2011 03:31 PM, Brian Akins wrote:
> 
> > On Sep 9, 2011, at 7:16 AM, Ranjib Dey wrote:
> > 
> > its saved at the end of the run automatically. If you are setting an
> > 
> > > attribute in the recipe and trying to reuse it in the same recipe afterwards  
> > > u make an explicit node.save call.
> > > 
> > > No, you don't have to save to use later in the same run. node.save  
> > > persists it on the chef-server. This is useful to do if you know other  
> > > nodes may need this node's attributes "right away." One case in mysql  
> > > master/slave configurations that was discussed on the list in the last few  
> > > days.
> 
> Thanks for clarification, Brian!
> 
> Also, correct me if I am wrong, if a chef-client run fails, then node.save
> 
> > is not automatically called at the end.
> 
> Yeah, this moment is still of interest, right.
> 
> Denis

---

<div class="post-metadata">

**Author:** ![dennybaa](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/dennybaa/32/286_2.png) [@dennybaa](https://discourse.chef.io/u/dennybaa)\
**Post date:** [September 9, 2011, 12:04pm UTC](https://discourse.chef.io/t/node-save/2080/7 "2011-09-09T12:04:35Z")

</div>

On 09/09/2011 03:55 PM, Ranjib Dey wrote:

> node.save is not invoked if the run fails, but i guess we can hack it  
> inside a report handler and save it there.

Okay good. But I think we'd better explicitly call node.save inside a  
recipe (as others do it) if we expect fails latter in recipe... It's not  
good a good practice to persist data of a node failed to run.

Denis

---

<div class="post-metadata">

**Author:** ![Klaas\_Jan\_Wierenga](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/klaas_jan_wierenga/32/798_2.png) [@Klaas\_Jan\_Wierenga](https://discourse.chef.io/u/Klaas_Jan_Wierenga)\
**Post date:** [September 9, 2011, 2:27pm UTC](https://discourse.chef.io/t/node-save/2080/8 "2011-09-09T14:27:23Z")

</div>

Hi,

node.save is used explicitly when information must be save even if the chef  
run fails.

A good example is when a random password (e.g. for MySQL) has been generated  
and applied to the database. If you don't call node.save and the run fails,  
then the next run will generate a new password which doesn't match the  
password in the database so access to the database will fail.

By calling node.save right after generating the password but before applying  
it to the database you ensure that even when the chef run fails lateron, the  
same password will be used in subsequent chef runs.

Regards,

Klaas Jan Wierenga

2011/9/9 Denis Barishev [denis.barishev@gmail.com](mailto:denis.barishev@gmail.com)

> On 09/09/2011 03:55 PM, Ranjib Dey wrote:
> 
> > node.save is not invoked if the run fails, but i guess we can hack it  
> > inside a report handler and save it there.
> 
> Okay good. But I think we'd better explicitly call node.save inside a  
> recipe (as others do it) if we expect fails latter in recipe... It's not  
> good a good practice to persist data of a node failed to run.
> 
> Denis

--  
Groeten,

Klaas Jan  
[k.j.wierenga@gmail.com](mailto:k.j.wierenga@gmail.com)

---

<div class="post-metadata">

**Author:** ![John\_E\_Vincent\_lusis](https://avatars.discourse-cdn.com/v4/letter/j/c89c15/32.png) [@John\_E\_Vincent\_lusis](https://discourse.chef.io/u/John_E_Vincent_lusis)\
**Post date:** [September 9, 2011, 2:32pm UTC](https://discourse.chef.io/t/node-save/2080/9 "2011-09-09T14:32:28Z")

</div>

On Fri, Sep 9, 2011 at 8:04 AM, Denis Barishev [denis.barishev@gmail.com](mailto:denis.barishev@gmail.com) wrote:

> On 09/09/2011 03:55 PM, Ranjib Dey wrote:
> 
> > node.save is not invoked if the run fails, but i guess we can hack it  
> > inside a report handler and save it there.
> 
> Okay good. But I think we'd better explicitly call node.save inside a recipe  
> (as others do it) if we expect fails latter in recipe... It's not good a  
> good practice to persist data of a node failed to run.
> 
> Denis

It's probably worth noting that this behavior is different from 0.9 to  
0.10. In 0.9, nodes were saved at the beginning of the run. In 0.10,  
nodes are not saved unless a run is successful.

If you absolutely need the node saved (so that it's available as a  
search result on another node for instance) then, yes, calling  
node.save is warranted. As you said though, it's important that you  
only call node.save at the point where the system is in a state where  
it's can perform whatever function is needed that makes it usable as a  
search result. This can get annoying because it means, in something  
like the case of MySQL, you need to ensure that services are starting  
immediately as opposed to delayed till the end of the run.

It's much better, imho, those more advanced sort of interactions  
outside of Chef or to employ some sort of blocking in another client's  
run.

---

<div class="post-metadata">

**Author:** ![Joshua\_Timberman](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/joshua_timberman/32/424_2.png) [@Joshua\_Timberman](https://discourse.chef.io/u/Joshua_Timberman)\
**Post date:** [September 11, 2011, 1:24am UTC](https://discourse.chef.io/t/node-save/2080/10 "2011-09-11T01:24:37Z")

</div>

I believe the conversation following answers the primary part of the query.

An unanswered question may be "So why doesn't Chef save the node when  
the run fails?"

The answer is, that by default, Chef would have no way of knowing  
whether that is a desirable thing to do. There may be something in  
your recipes that you wouldn't want saved until the full convergence  
finished. Chef doesn't make the assumption of doing so because it is  
easier for you to explicitly save in a recipe, or to call node.save in  
a report / exception handler than to try and undo that kind of  
behavior down in the core of Chef.

And to clarify, the attribute behavior in the mysql::server recipe is  
specifically because we're generating a random password so there's  
_something_ to use in preseeding the mysql installation, AND so the  
password is persisted as an attribute, so you don't have to do  
additional work in setting a password somewhere to use it.

Hope this helps.

On Fri, Sep 9, 2011 at 4:46 AM, Denis Barishev [denis.barishev@gmail.com](mailto:denis.barishev@gmail.com) wrote:

> Hey all,
> 
> How do you think why we meet the node.save command in many recipes? For  
> example, I've seen it in the mysql recipe.  
> As far as I understand any attributes that are set in recipes are  
> automatically persisted.  
> So what for is the 'node.save' command then?
> 
> Thanks for any ideas
> 
> Denis

--  
Opscode, Inc  
Joshua Timberman, Director of Training and Services  
IRC, Skype, Twitter, Github: jtimberman
