Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from pyinfra.modules import server
server.shell(
{'First task operation'},
'echo first_task_operation',
)
server.shell(
{'Second task operation'},
'echo second_task_operation',
)
# Create some conditional branches
if host.name == 'somehost':
server.shell(
{'Second main operation'},
'echo second_main_op',
)
elif host.name == 'anotherhost':
local.include('tasks/a_task.py')
# Include the whole file again, but for all hosts
local.include('tasks/a_task.py')
# Do a loop which will generate duplicate op hashes
for i in range(2):
server.shell(
{'Loop-{0} main operation'.format(i)},
'echo loop_{0}_main_operation'.format(i),
)
files.file(
{'Third main operation'},
'files/a_file',
'/a_file',
)
with state.preserve_loop_order([1, 2]) as loop_items:
for item in loop_items():
server.shell(
{'Order loop {0}'.format(item)},
'echo loop_{0}'.format(item),
)
if 'bsd' in host.groups:
server.shell({'BSD_GROUP'}, 'echo BSD_GROUP')
server.shell({'EVERYONE-2'}, 'echo EVERYONE-2')
if host.fact.os == 'OpenBSD':
server.shell({'OS_OPENBSD'}, 'echo OS_OPENBSD')
distro = host.fact.linux_name
if distro in ('Ubuntu', 'Debian'):
server.shell({'UBUNTU_DEBIAN'}, 'echo UBUNTU_DEBIAN')
elif distro in ('CentOS', 'Fedora'):
if distro == 'CentOS':
server.shell({'CENTOS-1'}, 'echo CENTOS-1')
server.shell({'CENTOS-2'}, 'echo CENTOS-2')
server.shell({'CENTOS-3'}, 'echo CENTOS-3')
server.shell({'CENTOS-4'}, 'echo CENTOS-4')
server.shell({'CENTOS_FEDORA-1'}, 'echo CENTOS_FEDORA-1')
if distro == 'Fedora':
server.shell({'FEDORA-1'}, 'echo FEDORA-1')
server.shell({'CENTOS_FEDORA-2'}, 'echo CENTOS_FEDORA-2')
if distro == 'CentOS':
# pyinfra
# File: example/roles/bsd_role.py
# Desc: example role that installs pip on (Open)BSD
from pyinfra.modules import pkg, server
# OpenBSD packages?
pkg.packages(
['py-pip', 'git'],
sudo=True,
)
# add_pkg does not automatically do this
server.shell(
'ln -sf /usr/local/bin/pip2.7 /usr/local/bin/pip',
sudo=True,
)
server.shell({'CENTOS-3'}, 'echo CENTOS-3')
server.shell({'CENTOS-4'}, 'echo CENTOS-4')
server.shell({'CENTOS_FEDORA-1'}, 'echo CENTOS_FEDORA-1')
if distro == 'Fedora':
server.shell({'FEDORA-1'}, 'echo FEDORA-1')
server.shell({'CENTOS_FEDORA-2'}, 'echo CENTOS_FEDORA-2')
if distro == 'CentOS':
server.shell({'CENTOS-5'}, 'echo CENTOS-5')
server.shell({'CENTOS-6'}, 'echo CENTOS-6')
if host.fact.os == 'OpenBSD':
server.shell({'OPENBSD-2'}, 'echo OPENBSD-2')
elif distro == 'Debian':
server.shell({'DEBIAN-3'}, 'echo DEBIAN-3')
server.shell({'DEBIAN-4'}, 'echo DEBIAN-4')
server.shell({'EVERYONE-3'}, 'echo EVERYONE-3')
)
# Manage pip (npm, gem) packages
did_install = pip.packages(
{'Install virtualenv with pip'},
['virtualenv'],
sudo=True,
)
# Use operation meta to affect the deploy
if did_install.changed:
server.shell(
'echo "Clean package build/etc"',
)
# Create a virtualenv
server.shell(
{'Setup the virtualenv'},
'virtualenv {{ host.data.env_dir }}',
sudo=True,
sudo_user='pyinfra',
)
# and manage pip within it
pip.packages(
{'Install Python packages with pip'},
['ElasticQuery', 'JsonTest'],
virtualenv=host.data.env_dir,
sudo=True,
sudo_user='pyinfra',
)
server.shell({'EVERYONE-2'}, 'echo EVERYONE-2')
if host.fact.os == 'OpenBSD':
server.shell({'OS_OPENBSD'}, 'echo OS_OPENBSD')
distro = host.fact.linux_name
if distro in ('Ubuntu', 'Debian'):
server.shell({'UBUNTU_DEBIAN'}, 'echo UBUNTU_DEBIAN')
elif distro in ('CentOS', 'Fedora'):
if distro == 'CentOS':
server.shell({'CENTOS-1'}, 'echo CENTOS-1')
server.shell({'CENTOS-2'}, 'echo CENTOS-2')
server.shell({'CENTOS-3'}, 'echo CENTOS-3')
server.shell({'CENTOS-4'}, 'echo CENTOS-4')
server.shell({'CENTOS_FEDORA-1'}, 'echo CENTOS_FEDORA-1')
if distro == 'Fedora':
server.shell({'FEDORA-1'}, 'echo FEDORA-1')
server.shell({'CENTOS_FEDORA-2'}, 'echo CENTOS_FEDORA-2')
if distro == 'CentOS':
server.shell({'CENTOS-5'}, 'echo CENTOS-5')
server.shell({'CENTOS-6'}, 'echo CENTOS-6')
if host.fact.os == 'OpenBSD':
server.shell({'OPENBSD-2'}, 'echo OPENBSD-2')
if distro in ('Ubuntu', 'Debian'):
server.shell({'UBUNTU_DEBIAN'}, 'echo UBUNTU_DEBIAN')
elif distro in ('CentOS', 'Fedora'):
if distro == 'CentOS':
server.shell({'CENTOS-1'}, 'echo CENTOS-1')
server.shell({'CENTOS-2'}, 'echo CENTOS-2')
server.shell({'CENTOS-3'}, 'echo CENTOS-3')
server.shell({'CENTOS-4'}, 'echo CENTOS-4')
server.shell({'CENTOS_FEDORA-1'}, 'echo CENTOS_FEDORA-1')
if distro == 'Fedora':
server.shell({'FEDORA-1'}, 'echo FEDORA-1')
server.shell({'CENTOS_FEDORA-2'}, 'echo CENTOS_FEDORA-2')
if distro == 'CentOS':
server.shell({'CENTOS-5'}, 'echo CENTOS-5')
server.shell({'CENTOS-6'}, 'echo CENTOS-6')
if host.fact.os == 'OpenBSD':
server.shell({'OPENBSD-2'}, 'echo OPENBSD-2')
elif distro == 'Debian':
server.shell({'DEBIAN-3'}, 'echo DEBIAN-3')
server.shell({'DEBIAN-4'}, 'echo DEBIAN-4')
server.shell({'EVERYONE-3'}, 'echo EVERYONE-3')
server.shell({'EVERYONE-2'}, 'echo EVERYONE-2')
if host.fact.os == 'OpenBSD':
server.shell({'OS_OPENBSD'}, 'echo OS_OPENBSD')
distro = host.fact.linux_name
if distro in ('Ubuntu', 'Debian'):
server.shell({'UBUNTU_DEBIAN'}, 'echo UBUNTU_DEBIAN')
elif distro in ('CentOS', 'Fedora'):
if distro == 'CentOS':
server.shell({'CENTOS-1'}, 'echo CENTOS-1')
server.shell({'CENTOS-2'}, 'echo CENTOS-2')
server.shell({'CENTOS-3'}, 'echo CENTOS-3')
server.shell({'CENTOS-4'}, 'echo CENTOS-4')
server.shell({'CENTOS_FEDORA-1'}, 'echo CENTOS_FEDORA-1')
if distro == 'Fedora':
server.shell({'FEDORA-1'}, 'echo FEDORA-1')
server.shell({'CENTOS_FEDORA-2'}, 'echo CENTOS_FEDORA-2')
if distro == 'CentOS':
server.shell({'CENTOS-5'}, 'echo CENTOS-5')
server.shell({'CENTOS-6'}, 'echo CENTOS-6')
if host.fact.os == 'OpenBSD':