Using Augeas to manage resources in Puppet

January 31, 2020 – Samuli Seppänen

This translated from the original Finnish article that is available here.

The Augeas resource in Puppet allows one to manage parts of a complex configuration file instead of managing the whole file using a template or a static file. For trivial cases stdlib's file_line resource may be sufficient. While use of templates or static files is in general more reliable and easier than use of Augeas, the latter is quite useful in many cases. As using Puppet Augeas resource directly is quite challenging, a useful practice is to first make the changes manually with "augtool" and only then convert the results into Puppet Augeas resources. We use PostgreSQL's pg_hba.conf file here as an example.

The first step is to check what Augeas' view of a configuration file is:

$ augtool
augtool> ls /files/etc/postgresql/9.4/main/pg_hba.conf
--- snip ---
#comment[65] = Database administrative login by Unix domain socket
1/ = (none)
2/ = (none)
3/ = (none)
4/ = (none)

The lines (or objects) to edit have been numbered with 1-4 in the above example. To view their contents use the "print" command:

augtool> print /files/etc/postgresql/9.4/main/pg_hba.conf/1
/files/etc/postgresql/9.4/main/pg_hba.conf/1
/files/etc/postgresql/9.4/main/pg_hba.conf/1/type = "local"
/files/etc/postgresql/9.4/main/pg_hba.conf/1/database = "all"
/files/etc/postgresql/9.4/main/pg_hba.conf/1/user = "postgres"
/files/etc/postgresql/9.4/main/pg_hba.conf/1/method = "peer"

Augeas allows modifying these four objects. For example, to set method = "password" you would use the "set" command:

augtool> set /files/etc/postgresql/9.4/main/pg_hba.conf/1/method password
augtool> print /files/etc/postgresql/9.4/main/pg_hba.conf/1
/files/etc/postgresql/9.4/main/pg_hba.conf/1
/files/etc/postgresql/9.4/main/pg_hba.conf/1/type = "local"
/files/etc/postgresql/9.4/main/pg_hba.conf/1/database = "all"
/files/etc/postgresql/9.4/main/pg_hba.conf/1/user = "postgres"
/files/etc/postgresql/9.4/main/pg_hba.conf/1/method = "password"

If you want to add a new line to the file you need to use the "ins" command. The first step is to add a new line after the authentication settings, i.e. object 1 above:

augtool> ins 0444 after /files/etc/postgresql/9.4/main/pg_hba.conf/1

Finally you define the fields you want the line to contain:

augtool> set /files/etc/postgresql/9.4/main/pg_hba.conf/0444/type local
augtool> set /files/etc/postgresql/9.4/main/pg_hba.conf/0444/database mydatabase
augtool> set /files/etc/postgresql/9.4/main/pg_hba.conf/0444/user johndoe
augtool> set /files/etc/postgresql/9.4/main/pg_hba.conf/0444/method password

The line number is set high (0444) to avoid other lines (objects) getting the same identifier. A zero further reduces that risk, because anecdotal evindence tells us that Augeas never prepends its line numbers with a zero. In case of pg_hba.conf the risk of line numbering conflicts is rather minimal anyways because usually there only few lines in it.

Finally you need to save the changes you made:

augtool> save

Now that you know the incantations to use with augtool you can convert those into a Puppet Augeas resource:

augeas { 'postgresql-pg_hba.conf':
  context => "/files/etc/postgresql/9.4/main/pg_hba.conf",
  changes => [
    "ins 0444 after 1",
    "set 0444/type local",
    "set 0444/database mydatabase",
    "set 0444/user johndoe",
    "set 0444/method password"
  ],
  lens => 'Pg_hba.lns',
  incl => "/etc/postgresql/9.4/main/pg_hba.conf",
  # Prevent the same line from being created again
  onlyif => "match *[user = 'johndoe'] size == 0",
}

The "context" parameter gets added to the beginning of the Augeas path. That allows you to avoid repeating the full path in every change you make, like you have to do when using augtool directly.

Further reading

Want to talk to an expert?

If you want to reach us, just send us a message or book a free call!
Categories

Tags

#aad #Access #acl #alertmanager #ansible #ansible module development #Apache #API #augeas #authentication #authorization #automation #automatization #aws #azure #backup #bash #bitbucket #buildbot #cache #centos #cloud #cloud-init #cloudflare #cloudfront #cluster #connectionsJpa #control repo #custom fact #database #debian #devops #digital sovereignty #DNS #docker #domain mode #duplo #ejabberd #email #encryption #erb #europe #eyaml #fabric #facter #facts #fargate #fedora #file #finnish #foreman #freeipa #git #github #gitlab #gnome #google #grafana #hammer #hiera #IAM #import #infinispan #Infrastructure as Code #ipmi #irc #jboss #jdk #jenkins #JMESPath #kanban #keycloak #librarian-puppet #librenms #linkedin #Linux #Location #loop #marketing #mautic #Mellon #mfa #monitoring #mysql #nagios #network-manager #oauth #oauth2 #office365 #open source #openvpn #oxygen #packer #paranormal #pdk #people #php #pkcs7 #pomodoro #Powershell #preseed #presentation #profiles #prometheus #provisioning #puppet #puppet-bolt #puppet-litmus #puppetboard #puppetdb #Puppetfile #puppetserver #puppet types and providers #pxeboot #qemu #quality #r10k #recruitment #redirect #Restrict #Reverse Proxy #roles #rspec #ruby #SAML #sem #shell #showsql #snmp #snmpd #software developement #spam #ssh #sso #standardization #systemd #systemd-resolved #teams #terraform #ubuntu #user-data #vagrant #vanity awards #variable #vim #virtualbox #visualstudio #webdevelopment #wildfly #Windows #wireguard #wordpress #workflow #x11 #xmpp #zimbra
We are
 Puppeteers
menucross-circle