0

I am trying to use augtool to auto edit my /etc/hosts, as I wish to add an alias for localhost (so that I can test my webserver with a different host-name, localy).

I have been looking every-ware to find good documentation.

I with to find the node with ipaddr of 127.0.0.1 and add an alias to it. I would also like to find some good documentation to Augeas.

ctrl-alt-delor
  • 27,473
  • 9
  • 58
  • 102

2 Answers2

1

To do the specific task of “find the node with ipaddr of 127.0.0.1 and add an alias to it”

Do

augtool -b -s set '/files/etc/hosts/*[ipaddr = "127.0.0.1"]/alias[last()+1]' mycouchdb

This looks in file /etc/host/ any node(*), that has a sub-node of ipaddr=127.0.0.1 and adds an alias of mycouchdb

Documentation

The path is explained here https://github.com/hercules-team/augeas/wiki/Path-expressions it uses XPath.

Sorry I can't find anything else good.

ctrl-alt-delor
  • 27,473
  • 9
  • 58
  • 102
1

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.

ctrl-alt-delor
  • 27,473
  • 9
  • 58
  • 102
raphink
  • 289
  • 1
  • 8
  • That seems to work, but can you explain how. I am stuck on `alias[.="mycouchdb"]`. – ctrl-alt-delor Sep 19 '17 at 08:17
  • `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`. When there there is an alias already, the expression will match and the set command will replace the value with `mycouchdb`, which will do nothing. – raphink Sep 19 '17 at 10:24
  • I get the first bit, and that is what I thought the patterns mean, but it is the when it does not match bit, that I am confused about. What are the rules for that? Also could you update the answer, then I can ✓ it. – ctrl-alt-delor Sep 19 '17 at 12:16
  • @ctrl-alt-delor 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`. – raphink Sep 19 '17 at 15:46