You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Rename files fact arguments from `name` -> `path`.
Previously (with "magic" facts) only positional arguments were used,
the move to explicit imports switches to explicit kwargs, so this
updates that to match the file operations which use `path`.
* Rename `server.Which` argument `name` -> `command`.
* Handle kwargs properly in `Host.{create,delete}_fact`.
* Use explicit fact imports in files operations.
* Implement `create_fact` & `delete_fact` in the test `FakeHost`.
* Update all the operations tests using file facts for explicit imports.
* Rename files fact tests to be explicit style.
* Accept positional arguments on `Host.get_fact`.
* Update fact examples in docs to be explicit style.
Copy file name to clipboardExpand all lines: docs/api/facts.rst
+13-6Lines changed: 13 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,14 @@ passed (as a ``list`` of lines) to the ``process`` handler to generate fact data
8
8
Importing & Using Facts
9
9
~~~~~~~~~~~~~~~~~~~~~~~
10
10
11
-
Facts are available in a global namespace as attributes on host objects: ``host.fact.X``. Custom fact classes will automatically be added to the namespace when the module containing them is imported. Note that the class name is converted to snake case, so ``MyFact`` becomes ``my_fact``, available as ``host.fact.my_fact``.
11
+
Like operations, facts are imported from Python modules and executed by calling `Host.get_fact`. For example:
12
+
13
+
.. code:: python
14
+
15
+
from pyinfra import host
16
+
from pyinfra.facts.server import Which
17
+
18
+
host.get_fact(Which, command='htop')
12
19
13
20
14
21
Example: getting swap status
@@ -34,7 +41,7 @@ This fact could then be used like so:
34
41
35
42
.. code:: python
36
43
37
-
is_swap_enabled = host.fact.swap_enabled
44
+
is_swap_enabled = host.get_fact(SwapEnabled)
38
45
39
46
40
47
Example: getting the list of files in a directory
@@ -51,9 +58,9 @@ This fact returns a list of files found in a given directory. For this fact the
51
58
Returns a list of files from a start point, recursively using find.
52
59
'''
53
60
54
-
defcommand(self, name):
61
+
defcommand(self, path):
55
62
# Find files in the given location
56
-
return'find {0} -type f'.format(name)
63
+
return'find {0} -type f'.format(path)
57
64
58
65
defprocess(self, output):
59
66
return output # return the list of lines (files) as-is
@@ -62,7 +69,7 @@ This fact could then be used like so:
0 commit comments