Closed mkinney closed 4 years ago
So templates are currently rendered using jinja2 - this definitely needs better documenting!
Two globals are passed into the template: host
and inventory
as per https://github.com/Fizzadar/pyinfra/blob/master/pyinfra/modules/files.py#L461.
I'll leave this issue open to add documentation to the module explaining the above.
For instance: {{ host.fact.deb_packages|to_json }}
pyinfra error (operation=Create a templated file): Error in template: templates/foo (L1578): no filter named 'to_json'
inventory: {{ inventory|tojson }}
but I got this error:
--> An unexpected exception occurred:
File "/usr/local/Cellar/python/3.7.6/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Inventory is not JSON serializable
I've added an issue to make Inventory
objects JSON serialisable @ https://github.com/Fizzadar/pyinfra/issues/215.
Have now added a link to jinja2 in the files.template
docstring (https://github.com/Fizzadar/pyinfra/commit/efcfe50ff34b08abc896fa4b31cec085d8cf5f74), so closing this.
Hi @Fizzadar , sorry to necropost this, but from the docmentation is still unclear that the objects passed to files.template are host
, state
, inventory
and facts
.
It would be nice to have a (simple) example before the one that passes variables as arguments, or even complete that example to use both arguments and the objects passed by default.
Looking at the files.template() operation and wondering what variables are available.
Is there a templating library included?
Can you have things like "{{ date }}" or "{{ some_system_command|some_formatting }}"?
Should there be a fact called "internals" that would show things like date task was started, args passed to pyinfra, etc?