On 18/05/21 2:24 am, Brian Coca wrote:

For data manipulation you want to create a filter plugin, you still
need to be explicit about the data you feed it.

Thanks for that - this is what I've ended up with. There's probably some copy/paste boilerplate that's not needed in my case.

Does it look reasonable?
I've had a suggestion that I should avoid lineinfile, but I'm not sure what a good substitute is (other than rearranging my setup so I can template out individual files, rather than editing the existing one).

Cheers,
Richard

------task----------------
- name: update dirvish-forced-command list
  lineinfile:
    dest: "/usr/local/etc/dirvish-forced-command/default.list"
    regexp: "^{{ item }}$"
    line: "{{ item }}"
    insertafter: EOF
  delegate_to: "{{ container_host }}"
with_items: "{{ backup_paths | gen_dfc_lines(inventory_hostname, filesystems) }}"
  tags:
    - backup

------plugin------------

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.errors import AnsibleError, AnsibleFilterError, AnsibleFilterTypeError from ansible.module_utils.six import string_types, integer_types, reraise, text_type

def gen_dfc_lines(backup_paths, container_name, filesystems):
    '''Generate a list of lines for dirvish_forced_command'''
    if not isinstance(filesystems, list):
        raise AnsibleFilterTypeError('filesystems should be a list')
    if not isinstance(backup_paths, list):
        raise AnsibleFilterTypeError('backup_paths should be a list')

    fixed_backup_paths = []
    for path_entry in backup_paths:
        found = False
        for fs_entry in filesystems:
            path = path_entry['path']
            fs = fs_entry['container_mountpoint']
            if path.startswith(fs):
                # remove prefix from path
                path = '/' + path[len(fs):]
                # use guestfs path
                fixed_path = "/guestfs/{cname}-{suffix}{bpath}".format(
                    cname = container_name,
                    suffix = fs_entry['suffix'],
                    bpath = path
                )
                found = True
        if not found:
            # use rootfs path
            fixed_path = "/var/lib/lxc/{cname}/rootfs{bpath}".format(
                cname = container_name,
                bpath = path
            )
        fixed_backup_paths.append(fixed_path)
    return fixed_backup_paths

class FilterModule(object):
    ''' backup helper filters '''

    def filters(self):
        return {
            'gen_dfc_lines': gen_dfc_lines
        }

--
You received this message because you are subscribed to the Google Groups "Ansible 
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/11476de0-de71-5542-2501-45b268aee55e%40walnut.gen.nz.

Reply via email to