Skip to content

Commit 838737e

Browse files
authored
Docs: Add basic localhost example to "Using the API" page (#1212)
* Add basic localhost example to API docs index * show how to access add_op result stderr/stdout via OperationMeta
1 parent 3283167 commit 838737e

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

docs/api/index.rst

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,64 @@ Using the API
33

44
In addition to :doc:`the pyinfra CLI <../cli>`, pyinfra provides a full Python API. As of ``v3`` this API can be considered mostly stable. See the :doc:`./reference`.
55

6-
`An example of how to use the API can be found here <https://github.com/pyinfra-dev/pyinfra-examples/blob/main/.old/api_deploy.py>`_. Note: this is not yet tested and pending future updates.
6+
You can also reference `pyinfra's own main.py <https://github.com/pyinfra-dev/pyinfra/blob/3.x/pyinfra_cli/main.py>`_, and the `pyinfra API source code <https://github.com/pyinfra-dev/pyinfra/tree/3.x/pyinfra/api>`_.
7+
8+
Full Example
9+
============
10+
11+
`A full example of how to use the API can be found here <https://github.com/pyinfra-dev/pyinfra-examples/blob/main/.old/api_deploy.py>`_. (Note: this is not yet tested and is pending future updates)
12+
13+
Basic Localhost Example
14+
=======================
15+
16+
.. code:: python
17+
18+
from pyinfra.api import Config, Inventory, State
19+
from pyinfra.api.connect import connect_all
20+
from pyinfra.api.operation import add_op
21+
from pyinfra.api.operations import run_ops
22+
from pyinfra.api.facts import get_facts
23+
from pyinfra.facts.server import Os
24+
from pyinfra.operations import server
25+
26+
# Define your inventory (@local means execute on localhost using subprocess)
27+
# https://docs.pyinfra.com/en/3.x/apidoc/pyinfra.api.inventory.html
28+
inventory = Inventory((["@local"], {}))
29+
30+
# Define any config you need
31+
# https://docs.pyinfra.com/en/3.x/apidoc/pyinfra.api.config.html
32+
config = Config(SUDO=True)
33+
34+
# Set up the state object
35+
# https://docs.pyinfra.com/en/3.x/apidoc/pyinfra.api.state.html
36+
state = State(inventory=inventory, config=config)
37+
38+
# Connect to all the hosts
39+
connect_all(state)
40+
41+
# Start adding operations
42+
result1 = add_op(
43+
state,
44+
server.user,
45+
user="pyinfra",
46+
home="/home/pyinfra",
47+
shell="/bin/bash",
48+
)
49+
result2 = add_op(
50+
state,
51+
server.shell,
52+
name="Run some shell commands",
53+
commands=["whoami", "echo $PATH", "bash --version"]
54+
)
55+
56+
# And finally we run the ops
57+
run_ops(state)
58+
59+
# add_op returns an OperationMeta for each op, letting you access stdout, stderr, etc. after they run
60+
host = state.hosts.inventory['@local']
61+
print(result1.changed, result1[host].stdout, result1[host].stderr)
62+
print(result2.changed, result2[host].stdout, result2[host].stderr)
63+
64+
# We can also get facts for all the hosts
65+
# https://docs.pyinfra.com/en/3.x/apidoc/pyinfra.api.facts.html
66+
print(get_facts(state, Os))

0 commit comments

Comments
 (0)