Updating hosts file is super easy! Any sed, echo, vim command will work.
You’re perfectly safe, if all changes only come from the automation. And your code is bug-free.
But in the reality, you might have surprises. Especially when people might change it manually.
My Suggestion? Before making any changes, confirm all customizations are expected. If not, abort the changes with proper alerting.