Skip to content

Instantly share code, notes, and snippets.

@lutter
Created October 28, 2016 19:32
Show Gist options
  • Save lutter/e57e0a0366aed6873b300cf4d9d048b5 to your computer and use it in GitHub Desktop.
Save lutter/e57e0a0366aed6873b300cf4d9d048b5 to your computer and use it in GitHub Desktop.
Idempotent augeas
# We want a function that makes it easier to write down how
# to make idempotent changes with augeas rather than the
# clunky mechanism the augeas Puppet type gives you today.
#
# I was thinking we could achieve this with a function like the following:
augchange(
:find => "/files/etc/hosts/*[ipaddr = '127.0.0.1']",
:create => ["ins 01 before /files/etc/hosts/*[1]",
"set /files/etc/hosts/01/ipaddr 127.0.0.1"],
:tree => {
"ipaddr" => "127.0.0.1",
"canonical" => "localhost",
"alias" => ["localhost.localdomain", "localhost4"]
}
}
# The parameters are as follows:
# :find : a path expression that finds the root of the tree we want to change
# :create : a sequence of statements that make it so that :find will find somehting,
# will only be run if :find doesn't find something initially. If it is
# the special value :find, we just do a 'clear <find>' with the path expression
# of :find
# :tree : the tree that should be constructed at the node that :find finds.
# If the value of an entry is an array, a series of nodes with that name is created
augchange(
:find => "/files/etc/ssh/sshd_config/PermitRootLogin",
:create => :find
:tree => { "." => "no" }
)
@domcleal
Copy link

Lines 10-14: the tree might be better represented as an array than a hash - it has a more predictable order (hashes are insertion order, which makes it difficult to change).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment