Ansible wrapper

The opslib.ansible module runs Ansible actions. The Ansible.Builtin collection is installed by default.

AnsibleAction component

AnsibleAction runs a single Ansible action on a single host.

After a successful deployment, the component instance is marked as “up to date”, and will be skipped in subsequent deployments, unless any of the props change. To check if remote state has changed, run opslib - diff.

from opslib import LocalHost, Stack
from opslib.ansible import AnsibleAction

stack = Stack(__name__)
stack.host = LocalHost()
stack.repo = AnsibleAction(
    host=stack.host,
    module="ansible.builtin.git",
    args=dict(
        repo="https://github.com/mgax/opslib",
        dest="/tmp/opslib",
    ),
)

Formatting output

Ansible returns rich output from actions, including data about differences between desired and actual state, but there doesn’t seem to be a uniform structure across modules. However, you can specify a format_output prop:

from opslib import run

def diffstat(result):
    diff = result.data["diff"]["prepared"]
    if result.data["before"]:
        diff = run("diffstat", "-C", input=diff).stdout
    return diff

stack.repo = AnsibleAction(
    host=stack.host,
    module="ansible.builtin.git",
    args=dict(
        repo="https://github.com/mgax/opslib",
        dest="/tmp/opslib",
    ),
    format_output=diffstat,
)

Invoking Ansible directly

run_ansible() is a standalone function that runs an Ansible action. It doesn’t depend on a stack being present and is a handy way to invoke Ansible and get an AnsibleResult object back.

import sys
from pprint import pprint
from opslib.ansible import run_ansible

result = run_ansible(
    hostname="localhost",
    ansible_variables=[
        ("ansible_connection", "local"),
        ("ansible_python_interpreter", sys.executable),
    ],
    action=dict(
        module="ansible.builtin.gather_facts",
    ),
)
pprint(result.data)