Skip to content

Commit f0c891e

Browse files
committed
Pass facts variable into templates mapping to pyinfra.facts.
This allows templates to use the new `host.get_fact` explicit import style for fact data.
1 parent 0ffc3c1 commit f0c891e

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

examples/templates/foo.j2

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
This file was last modified by pyinfra on {{ host.fact.date }}
1+
This file was last modified by pyinfra on {{ host.get_fact(facts.server.Date) }}
22

33
You can use a variable from the deploy
44
foo_variable contents is:{{ foo_variable }}
55

66
Example host templating values:
7-
hostname: {{ host.fact.hostname }}
8-
os: {{ host.fact.os }}
9-
arch: {{ host.fact.arch }}
10-
cpus: {{ host.fact.cpus }}
11-
os_version: {{ host.fact.os_version }}
12-
13-
deb_packages: {{ host.fact.deb_packages|join(', ') }}
7+
hostname: {{ host.get_fact(facts.server.Hostname) }}
8+
os: {{ host.get_fact(facts.server.Os) }}
9+
arch: {{ host.get_fact(facts.server.Arch) }}
10+
cpus: {{ host.get_fact(facts.hardware.Cpus) }}
11+
os_version: {{ host.get_fact(facts.server.OsVersion) }}
1412

13+
deb_packages: {{ host.get_fact(facts.deb.DebPackages)|join(', ') }}

pyinfra/operations/files.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
import posixpath
88
import sys
99
import traceback
10-
1110
from datetime import timedelta
1211
from fnmatch import fnmatch
1312
from os import makedirs, path as os_path, walk
1413

1514
import six
16-
1715
from jinja2 import TemplateRuntimeError, TemplateSyntaxError, UndefinedError
1816

17+
import pyinfra
1918
from pyinfra import logger
2019
from pyinfra.api import (
2120
FileDownloadCommand,
@@ -901,6 +900,7 @@ def template(
901900
data.setdefault('host', host)
902901
data.setdefault('state', state)
903902
data.setdefault('inventory', state.inventory)
903+
data.setdefault('facts', pyinfra.facts)
904904

905905
# Render and make file-like it's output
906906
try:

0 commit comments

Comments
 (0)