I'm writing a script to automate setting up Puppet agent configuration files in Docker.
Basically, I need to ensure that the following section is in /etc/puppet/puppet.conf
:
[agent]
server=$PUPPETMASTER_HOSTNAME
masterport=$PUPPETMASTER_PORT
What I've been doing so far in my Puppet agent runit script is this:
function write_puppet_config () {
read -d '' puppet_config <<EOF
[agent]
server=$1
masterport=$2
EOF
echo -e "$puppet_config" >> /etc/puppet/puppet.conf
}
# default puppet master port is 8410
test -z "$PUPPET_MASTER_TCP_PORT" && export PUPPET_MASTER_TCP_PORT="8410"
# if there is a puppet master host defined, rewrite the config to match
if [ ! -z "$PUPPET_MASTER_TCP_HOST" ]; then
write_puppet_config "$PUPPET_MASTER_TCP_HOST" "$PUPPET_MASTER_TCP_PORT"
fi
The problem should be pretty apparent. If the Puppet configuration already specifies the configuration, I'm just appending another [agent]
section, which is bad.
I could just switch on conditional logic (ie: grep if it's there and then rewrite it with sed if it is), but is there a way to do an edit from the command line? I'd like to basically run a command which says "if there isn't an agent section, add it, and then make sure that server and masterport are set to the right values in that section."
I know that structured tools like this exist for XML, but what about INI-style files?
puppet agent -t --server yourmaster
.