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
Copy file name to clipboardExpand all lines: docs/using-operations.rst
+38-10Lines changed: 38 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,6 @@ For example, these two operations will ensure that user ``pyinfra`` exists with
24
24
user="pyinfra",
25
25
group="pyinfra",
26
26
mode="644",
27
-
_sudo=True,
28
27
)
29
28
30
29
@@ -127,20 +126,38 @@ Adding data to inventories is covered in detail here: :doc:`inventory-data`. Dat
127
126
Host Facts
128
127
~~~~~~~~~~
129
128
130
-
Facts allow you to use information about the target host to control and configure operations. A good example is switching between ``apt`` & ``yum`` depending on the Linux distribution. Facts are imported from ``pyinfra.facts.*`` and can be retrieved using the ``host.get_fact`` function:
129
+
Facts allow you to use information about the target host to control and configure operations. A good example is switching between ``apt`` & ``yum`` depending on the Linux distribution. You can get a fact like this:
130
+
131
+
.. code:: bash
132
+
133
+
pyinfra inventory.py fact server.LinuxName
134
+
135
+
Facts are imported from ``pyinfra.facts.*`` and can be retrieved using the ``host.get_fact`` function. If you save this in a file called `nano.py`:
131
136
132
137
.. code:: python
133
138
134
139
from pyinfra import host
135
140
from pyinfra.facts.server import LinuxName
136
-
from pyinfra.operations import yum
141
+
from pyinfra.operations import yum, apt
137
142
138
-
if host.get_fact(LinuxName) =="CentOS":
143
+
if host.get_fact(LinuxName) =="Fedora":
139
144
yum.packages(
140
145
name="Install nano via yum",
141
146
packages=["nano"],
142
147
_sudo=True
143
148
)
149
+
if host.get_fact(LinuxName) =="Ubuntu":
150
+
apt.packages(
151
+
name="Install nano via apt",
152
+
packages=["nano"],
153
+
update=True,
154
+
_sudo=True
155
+
)
156
+
157
+
.. code:: bash
158
+
159
+
pyinfra inventory.py nano.py
160
+
144
161
145
162
See :doc:`facts` for a full list of available facts and arguments.
146
163
@@ -322,24 +339,35 @@ Like ``host`` and ``inventory``, ``config`` can be used to set global defaults f
322
339
Enforcing Requirements
323
340
~~~~~~~~~~~~~~~~~~~~~~
324
341
325
-
The config object can be used to enforce a pyinfra version or Python package requirements. This can either be defined as a requirements text file path or simply a list of requirements:
342
+
The config object can be used to enforce a pyinfra version or Python package requirements. This can either be defined as a requirements text file path or simply a list of requirements. For example, if you create a `requirements.py` file with:
326
343
327
344
.. code:: python
328
345
329
346
# Require a certain pyinfra version
330
-
config.REQUIRE_PYINFRA_VERSION="~=1.1"
347
+
config.REQUIRE_PYINFRA_VERSION="~=3.0"
331
348
332
349
# Require certain packages
333
-
config.REQUIRE_PACKAGES="requirements.txt"# path relative to the current working directory
350
+
config.REQUIRE_PACKAGES="requirements.txt"# path is relative to the current working directory
334
351
config.REQUIRE_PACKAGES= [
335
-
"pyinfra~=1.1",
336
-
"pyinfra-docker~=1.0",
352
+
"pyinfra~=3.0",
337
353
]
338
354
355
+
And create a `requirements.txt` file with something like this:
356
+
357
+
.. code:: bash
358
+
359
+
pyinfra
360
+
361
+
Then modify the `nano.py` above to include these lines:
362
+
.. code:: python
363
+
364
+
from pyinfra import local
365
+
local.include("requirements.py")
366
+
339
367
340
368
Examples
341
369
--------
342
370
343
-
A great way to learn more about writing pyinfra deploys is to see some in action. There's a number of resources for this:
371
+
A great way to learn more about writing pyinfra deploys is to see some in action. Check out:
344
372
345
373
- `the pyinfra examples repository on GitHub <https://github.com/pyinfra-dev/pyinfra-examples>`_
0 commit comments