Skip to content

Commit 7ae202d

Browse files
authored
operations: add import statements on at least one example of every operation
1 parent 1f3b7ea commit 7ae202d

37 files changed

+107
-26
lines changed

docs/cli.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pyinfra @local ...
7777
pyinfra my-server.net,@local ...
7878

7979
# Execute against a Docker container
80-
pyinfra @docker/centos:8 ...
80+
pyinfra @docker/fedora:43 ...
8181
```
8282

8383
### Limit
@@ -132,7 +132,7 @@ For example, here we ensure that `nginx` is installed on the remote servers:
132132
# Ubuntu example
133133
pyinfra inventory.py apt.packages nginx update=true _sudo=true
134134

135-
# Centos example
135+
# Fedora example
136136
pyinfra inventory2.py yum.packages nginx _sudo=true
137137
```
138138

docs/compatibility.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ To debug pyinfra within PyCharm, you need to [explicitly enable support for Geve
2727
pyinfra aims to be compatible with all Unix-like operating systems and is currently developed against:
2828

2929
+ Linux distros:
30-
* Ubuntu 18/20
30+
* Ubuntu 20/22/24
3131
* Debian 9/10
32-
* CentOS 7/8
33-
* Fedora 33
32+
* Fedora 43
3433
* Alpine 3.8
3534
+ BSD flavours:
3635
* OpenBSD 6

docs/faq.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The currently executing host can be fetched from the ``host`` context variable.
2020
# Get the actual current hostname from the host
2121
from pyinfra.facts.server import Hostname
2222
hostname = host.get_fact(Hostname)
23+
print(f"hostname:{hostname}")
2324
2425
How do I use sudo in an operation?
2526
----------------------------------
@@ -28,6 +29,7 @@ Sudo is controlled by one of the `privilege and user escalation arguments <argum
2829

2930
.. code:: python
3031
32+
from pyinfra.operations import apt
3133
apt.packages(
3234
packages=["iftop"],
3335
_sudo=True,
@@ -43,6 +45,7 @@ Use the LINK ``files.file``, ``files.directory`` or ``files.link`` operations to
4345

4446
.. code:: python
4547
48+
from pyinfra.operations import files
4649
files.file(
4750
path="/etc/default/elasticsearch",
4851
user="pyinfra",
@@ -57,6 +60,7 @@ Use the `retry behavior arguments <arguments.html#retry-behavior>`_ to automatic
5760

5861
.. code:: python
5962
63+
from pyinfra.operations import server
6064
# Retry a network operation up to 3 times
6165
server.shell(
6266
name="Download file with retries",

docs/getting-started.rst

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ You can start pyinfra immediately with some ad-hoc command execution. The CLI al
2929
# Execute over SSH
3030
pyinfra my-server.net exec -- echo "hello world"
3131
32+
# Execute over SSH with options
33+
pyinfra localhost --ssh-port 2222 --ssh-user vagrant exec -- echo "hello world"
34+
3235
# Execute within a new docker container
33-
pyinfra @docker/ubuntu:18.04 exec -- echo "hello world"
36+
pyinfra @docker/ubuntu:22.04 exec -- echo "hello world"
3437
3538
# Execute on the local machine (MacOS/Linux only - for now)
3639
pyinfra @local exec -- echo "hello world"
@@ -66,8 +69,19 @@ To get started create an ``inventory.py`` containing our hosts to target:
6669

6770
.. code:: python
6871
69-
# Define a group as a list of hosts
70-
my_hosts = ["my-server.net", "@docker/ubuntu:18.04"]
72+
# Define a group as a list of hosts
73+
my_hosts = [
74+
# vagrant instance
75+
("ubuntu2204", {
76+
"ssh_port": 2222,
77+
"ssh_hostname": "localhost",
78+
"ssh_user": "vagrant",
79+
"_sudo": True
80+
}),
81+
"my-server.net",
82+
"@docker/ubuntu:22.04"
83+
]
84+
7185
7286
Now create a ``deploy.py`` containing our operations to execute:
7387

@@ -79,7 +93,7 @@ Now create a ``deploy.py`` containing our operations to execute:
7993
apt.packages(
8094
name="Ensure the vim apt package is installed",
8195
packages=["vim"],
82-
_sudo=True, # use sudo when installing the packages
96+
update=True,
8397
)
8498
8599
This can now be executed like this:

docs/using-operations.rst

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ For example, these two operations will ensure that user ``pyinfra`` exists with
2424
user="pyinfra",
2525
group="pyinfra",
2626
mode="644",
27-
_sudo=True,
2827
)
2928
3029
@@ -127,20 +126,38 @@ Adding data to inventories is covered in detail here: :doc:`inventory-data`. Dat
127126
Host Facts
128127
~~~~~~~~~~
129128

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`:
131136

132137
.. code:: python
133138
134139
from pyinfra import host
135140
from pyinfra.facts.server import LinuxName
136-
from pyinfra.operations import yum
141+
from pyinfra.operations import yum, apt
137142
138-
if host.get_fact(LinuxName) == "CentOS":
143+
if host.get_fact(LinuxName) == "Fedora":
139144
yum.packages(
140145
name="Install nano via yum",
141146
packages=["nano"],
142147
_sudo=True
143148
)
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+
144161
145162
See :doc:`facts` for a full list of available facts and arguments.
146163

@@ -322,24 +339,35 @@ Like ``host`` and ``inventory``, ``config`` can be used to set global defaults f
322339
Enforcing Requirements
323340
~~~~~~~~~~~~~~~~~~~~~~
324341

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:
326343

327344
.. code:: python
328345
329346
# Require a certain pyinfra version
330-
config.REQUIRE_PYINFRA_VERSION = "~=1.1"
347+
config.REQUIRE_PYINFRA_VERSION = "~=3.0"
331348
332349
# 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
334351
config.REQUIRE_PACKAGES = [
335-
"pyinfra~=1.1",
336-
"pyinfra-docker~=1.0",
352+
"pyinfra~=3.0",
337353
]
338354
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+
339367
340368
Examples
341369
--------
342370

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:
344372

345373
- `the pyinfra examples repository on GitHub <https://github.com/pyinfra-dev/pyinfra-examples>`_

src/pyinfra/operations/apk.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Manage apk packages.
2+
Manage apk packages. (Alpine Linux)
33
"""
44

55
from __future__ import annotations
@@ -64,6 +64,7 @@ def packages(
6464
6565
.. code:: python
6666
67+
from pyinfra.operations import apk
6768
# Update package list and install packages
6869
apk.packages(
6970
name="Install Asterisk and Vim",

src/pyinfra/operations/apt.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def key(src: str | None = None, keyserver: str | None = None, keyid: str | list[
6767
6868
.. code:: python
6969
70+
from pyinfra.operations import apt
7071
# Note: If using URL, wget is assumed to be installed.
7172
apt.key(
7273
name="Add the Docker apt gpg key",

src/pyinfra/operations/brew.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def packages(
6161
6262
.. code:: python
6363
64+
from pyinfra.operations import brew
6465
# Update package list and install packages
6566
brew.packages(
6667
name='Install Vim and vimpager',

src/pyinfra/operations/crontab.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def crontab(
5454
5555
.. code:: python
5656
57+
from pyinfra.operations import crontab
5758
# simple example for a crontab
5859
crontab.crontab(
5960
name="Backup /etc weekly",

src/pyinfra/operations/dnf.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ def key(src: str):
2525
2626
.. code:: python
2727
28+
from pyinfra import host
29+
from pyinfra.operations import dnf
30+
from pyinfra.facts.server import LinuxDistribution
2831
linux_id = host.get_fact(LinuxDistribution)["release_meta"].get("ID")
2932
dnf.key(
30-
name="Add the Docker CentOS gpg key",
33+
name="Add the Docker gpg key",
3134
src=f"https://download.docker.com/linux/{linux_id}/gpg",
3235
)
3336

0 commit comments

Comments
 (0)