bashpuppetaugeas

remove multiple line from /etc/hosts using augtool


My /etc/hosts look like this:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.13.11   idm.myhost.com
192.168.13.10   dnsf.myhost.com
192.168.13.11   idm.myhost.com
192.168.13.10   dnsf.myhost.com
192.168.13.11   idm.myhost.com
192.168.13.10   dnsf.myhost.com
192.168.13.10   temp1.myhost.com

I can query duplicated lines by query below:

 print /files/etc/hosts/*/canonical["idm.myhost.com"]

or either use regex

defvar mypath /files/etc/hosts
print $mypath/*/*[.=~ regexp("dnsf.myhost.com")]

But I can't remove these matched lines with a simple rm

rm /files/etc/hosts/*/canonical["idm.myhost.com"]

The problem is, augtool just removes the canonical leaf from the node and ipaddr remains. it cause the save function to returns error. I don't want to use bash script and I try to solve the problem with augeas itself. Assist, please...

P.S. I couldn't find a complete tutorial or document for this useful tool. I would be thankful is someone can propose a good document. I've searched all the web and just found a few simple examples.


Solution

  • Your expression selects the canonical node instead of the host node. If you want to delete the host nodes, you need to use:

    rm /files/etc/hosts/*[canonical="idm.myhost.com"]
    

    This selects any entry under /files/etc/hosts, whose canonical subnode has value idm.myhost.com.

    As for documentation, there's a tutorial on the Augeas website, as well as some more docs on the Augeas wiki.

    There's also a video intro to Augeas on Youtube.

    I started writing a book some 7 years ago, which can still be found online, but never tpok the time to finish it.