Instantly share code, notes, and snippets.

Embed
What would you like to do?
Configuring Voyage Linux with Ansible
from __future__ import (absolute_import, division, print_function)
from functools import wraps
from ansible.plugins.connection import ConnectionBase
from ansible.plugins.connection.ssh import Connection as SSHConnection
def voyage_mount_ro(func):
@wraps(func)
def _inner(self, *args, **kwargs):
super(Connection, self).exec_command('[[ -x /usr/local/sbin/remountro ]] && /usr/local/sbin/remountro')
return func(self, *args, **kwargs)
return _inner
def voyage_mount_rw(func):
@wraps(func)
def _inner(self, *args, **kwargs):
super(Connection, self).exec_command('[[ -x /usr/local/sbin/remountrw ]] && /usr/local/sbin/remountrw')
return func(self, *args, **kwargs)
return _inner
class Connection(SSHConnection, ConnectionBase):
"""
Connect to voyage linux installs over SSH. This requires running the remountrw
command before and the remountro command after to make the filesystem writable
for the duration of the command.
Use a little meta-programming, and the decorator pattern to provide a thin
layer over the existing SSH connection plugin.
The `remountro` command is only issued when a persistent connection is closed,
but in most cases this won't matter as issuing it is just an attempt to clean
up after ourselves - voyage has a cronjob that does this periodically.
Check out the source of the SSH connection plugin here:
https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/connection/ssh.py
NOTE: This plugin does not have support for the `sudoable` argument that may
be passed in when doing exec_command on a connection plugin.
"""
transport = 'voyage_ssh'
@voyage_mount_rw
def exec_command(self, *args, **kwargs):
return super(Connection, self).exec_command(*args, **kwargs)
@voyage_mount_rw
def put_file(self, *args, **kwargs):
return super(Connection, self).put_file(*args, **kwargs)
@voyage_mount_ro
def close(self, *args, **kwargs):
voyage_mount_ro(self)
return super(Connection, self).close(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment