MrMarmot
MrMarmot

Reputation: 21

Pyinfra - host/config objects

I was trying to run through some of the examples from pyinfra's docs to see if this tool would work for me (so I could manage some servers using good old Python) and I even found they had an example for installing virtualbox on a host machine, but I can't get even the example working. I'm getting a weird error that makes me think there's been an update to the tool since the examples, and even the docs, have been updated. Just wondering if anyone else has a way to get this to work with pyinfra (currently trying with version 1.5).

The example code:

from pyinfra import config, host
from pyinfra.facts.server import LinuxDistribution, LinuxName, OsVersion
from pyinfra.operations import apt, python, server

config.SUDO = True

virtualbox_version = '6.1'


def verify_virtualbox_version(state, host, version):
    command = '/usr/bin/virtualbox --help'
    status, stdout, stderr = host.run_shell_command(state, command=command, sudo=config.SUDO)
    assert status is True  # ensure the command executed OK
    if version not in str(stdout):
        raise Exception('`{}` did not work as expected.stdout:{} stderr:{}'.format(
            command, stdout, stderr))


if host.get_fact(LinuxName) == 'Ubuntu':
    code_name = host.get_fact(LinuxDistribution)['release_meta'].get('DISTRIB_CODENAME')

    apt.packages(
        name='Install packages',
        packages=['wget'],
        update=True,
    )

    apt.key(
        name='Install VirtualBox key',
        src='https://www.virtualbox.org/download/oracle_vbox_2016.asc',
    )

    apt.repo(
        name='Install VirtualBox repo',
        src='deb https://download.virtualbox.org/virtualbox/debian {} contrib'.format(code_name),
    )

    # install kernel headers
    # Note: host.get_fact(OsVersion) is the same as `uname -r` (ex: '4.15.0-72-generic')
    apt.packages(
        {
            'Install VirtualBox version {} and '
            'kernel headers for {}'.format(virtualbox_version, host.get_fact(OsVersion)),
        },
        [
            'virtualbox-{}'.format(virtualbox_version),
            'linux-headers-{}'.format(host.get_fact(OsVersion)),
        ],
        update=True,
    )

    server.shell(
        name='Run vboxconfig which will stop/start VirtualBox services and build kernel modules',
        commands='/sbin/vboxconfig',
    )

    python.call(
        name='Verify VirtualBox version',
        function=verify_virtualbox_version,
        version=virtualbox_version,
    )

Here is the error that I'm seeing:

File "install_virtualbox.py", line 21, in <module>
    if host.get_fact(LinuxName) == 'Ubuntu':
AttributeError: module 'pyinfra.api.host' has no attribute 'get_fact'

I checked the source and I can't argue with my editor when it says it 'cannot find a reference to config' or 'init', as I don't see them either. But then how is anyone getting facts about hosts? Looks like the logic may have been moved to the 'pyinfra.api.host' and 'pyinfra.api.config' packages, but the logic there looks totally different.

Hoping I'm just missing something that someone who's been using the tool can help explain to me.

Upvotes: 2

Views: 752

Answers (1)

Irmantas J
Irmantas J

Reputation: 1

For me working: if host.get_fact(LinuxName) in ["Ubuntu"]:

Upvotes: 0

Related Questions