Automatic attributes can't be modified in the context of a chef client run. Even if you set an attribute on the saved node object (which is what your code is doing), it will be reset at the start of every chef client run and the value from ohai will override it.
Here's the rough sequence of events during a chef client run:
- Chef retrieves the saved node object from the chef server (if one exists)
- It applies any attribute overrides from cookbooks/roles/recipes
- It applies the automatic attributes provided by ohai
- It executes the run list using this updated state
- It saves the updated node object on the chef server if the run succeeded
So if you only need correct automatic attributes within your chef-client run, you don't have to do anything since it all happens automatically.
EDIT: You can find more details about attribute persistence and precedence here.