Last active
March 15, 2024 23:13
-
-
Save 0x17de/a37b4dec060c2aa769c53dd1d66988b7 to your computer and use it in GitHub Desktop.
Ansible connection plugin for chroot after ssh - no suppory for sudo included
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ansible.plugins.connection.ssh as ssh | |
import yaml | |
data = yaml.safe_load(ssh.DOCUMENTATION) | |
data['name'] = 'ssh_chroot' | |
data['options']['chroot_path'] = { | |
'description': ['This is a custom option to specify the chroot path.'], | |
'ini': [{ | |
'section': 'ssh_chroot', | |
'key': 'chroot_path', | |
}], | |
'env': [{'name': 'ANSIBLE_CHROOT_PATH'}], | |
'vars': [{'name': 'ansible_chroot_path'}], | |
'required': True, | |
} | |
DOCUMENTATION = yaml.dump(data) | |
class Connection(ssh.Connection): | |
def __init__(self, *args, **kwargs): | |
super(Connection, self).__init__(*args, **kwargs) | |
def exec_command(self, cmd, in_data = None, sudoable = True): | |
chroot_path = self.get_option('chroot_path') | |
cmd = f'chroot {chroot_path} {cmd}' | |
return super(Connection, self).exec_command(cmd, in_data, sudoable) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example use:
ansible myhost -e 'ansible_connection=ssh_chroot ansible_chroot_path=/root/chroottest' -m slurp -a 'path=/a'