Backup List

This is a quick snippet that demonstrates how it’s easy to over-complicate what should be simple solutions.

Original Version

declare -a backuplist
backuplist=('/etc' '/srv' '/opt')
# Templated extra directories here...
{% if 'backup_dirs' in pillar %}{% for bdir in pillar['backup_dirs'] %}
backuplist+=("{{ bdir }}")
{% endfor %}{% endif %}

Biggest Issues

  • Creates a variable then loops through a list, adding to list for each iteration

  • Includes an if that is not needed

  • New version just adds them to the list/tuple being defined

New Version

declare -a backuplist
backuplist=('/etc' '/srv' '/opt'{% for b in salt['pillar.get']('backup_dirs:incl', []) %} '{{ b }}'{% endfor %})