I'm using pyinfra to provision some files. I would like to know the best place to put custom modules.
Example
Given a custom fact that returns True if a system is not headless:
class HasGui(FactBase):
default = list
command = 'ls /usr/share/xsessions/*.desktop || true'
def process(self, output):
return output
Questions
Where do I put this? I imagine I can code this snippet directly into an "operation" file, but what if I want to reuse this code in several modules? How do I abstract this into a separate module or access it from the API?
While data can be shared across modules, the recommended layout does not appear to easily permit custom modules to hook into the API.
Approaches
- I've tried to make a separate task-level module, but it is not recognized when invoked at command-line.
- A last resort may be adding the path to
sys.path, but I'd prefer a cleaner option.
This bug suggests custom facts can be detected from a top-level
config.pyfile located next to "deploy" files.Code
A custom fact coded into the config (optional). See also a sample layout:
Note: the trailing
|| truekeeps pyinfra from erroring out on fails. Failures seem to be handled internally despite the continuation.When a custom fact subclasses
FactBase, it is added to a facts index. You can access them via snake-cased attributes:Demo
Run in command line.