Hi all,
Well, it's not really that complex, but seems complex to do within the constraints of ansible/jinja.
My scenario is that I run dirvish to back up multiple containers on multiple hosts. The containers are backed up via the filesystems on the hosts.
The root filesystem of the container lives under /var/lib/lxc/<container>/rootfs.
Sometimes, extra filesystems are also used; they live under /guestfs/<container>-<name>, where 'name' is a possibly shortened version of the mountpoint.
Relevant sections of host_vars files look like this:
filesystems:
- { index: 1, suffix: srv, size: 2, container_mountpoint: '/srv/' }
backup_paths:
- { shortname: 'ROOT', path: '/' }
- { shortname: 'var', path: '/var/' }
- { shortname: 'home', path: '/home/' }
- { shortname: 'srv', path: '/srv/' }
So when I'm setting up backups, I need to treat any backup path for this container starting with '/srv/' differently, in that I need to backup something under /guestfs/ rather than under /var/lib/lxc.
I have a template that does this for the dirvish config:
client: {{ hostvars[container_host].fqdn }}
{% set ns = namespace(extra_fs='') %}
{% for mountpoint in filesystems %}
{% if item['path'] | regex_search(mountpoint['container_mountpoint']) %}
{% set ns.extra_fs = mountpoint %}
{% endif %}
{% endfor %}
{% if ns.extra_fs %}
tree: /guestfs/{{ inventory_hostname + '-' + ns.extra_fs['suffix'] + '/' + item['path'] |regex_replace(ns.extra_fs['container_mountpoint'], '') }}
{% else %}
tree: /var/lib/lxc/{{ inventory_hostname }}/rootfs{{ item['path'] }}
{% endif %}
rsh: ssh -i /root/.ssh/id_rsa_dirvish {{ hostvars[container_host].fqdn }}
{% for backup_path in (backup_paths | select('search', item['path'] + '.*')) if not (backup_path['shortname'] == item['shortname']) %}
{% if loop.index == 1 %}
excludes:
{% endif %}
{{ backup_path['path'] }}*
{% endfor %}
As you can see, that's quite complicated.
Then in addition, I need to add to a list of acceptable paths on the host (for my ssh forced command script to read).
That looks like this (for the same container described in the host_vars above):
...
/var/lib/lxc/example-web1/rootfs/
/var/lib/lxc/example-web1/rootfs/var/
/var/lib/lxc/example-web1/rootfs/home/
/guestfs/example-web1-srv/
...
That isn't a template, because the file contains paths for all the containers. I suppose I could modify my script to read any files in a directory instead of a single file, but that would be a separate project, and the template would be just as nasty as the one above.
I'm not even sure where to start with generating lines to add to the file.
Any suggestions?
Is this stuff reasonable to do with ansible, and a templating language?
Do I need to write a custom plugin of some kind?
If I had a dynamic inventory, then I could probably generate extra variables at that stage, but that's further away too.
Cheers,
Richard