While adding an alias to a host is not really hard, what's usually more interesting is to ensure a host entry has an alias, i.e. make the operation idempotent.
Here is how you can do that with Augeas:
set /files/etc/hosts/*[ipaddr="127.0.0.1"]/alias[.="mycouchdb"] "mycouchdb"
which will only add the alias if it doesn't exist yet.
Explanation:
alias[.="mycouchdb"]
refers to the alias with value mycouchdb
(since .
refers to the current node).
When there is no alias yet with value mycouchdb
, alias[.="mycouchdb"]
will not match anything and Augeas will create a node with label alias
and value mycouchdb
. The rule when the node doesn't exist is to use the path label without filters, in this case alias
, so it creates a new label node and assigns it the value mycouchdb
When there is an alias already, the expression will match and the set command will replace the value with mycouchdb
, which will do nothing.