Skip to content

Instantly share code, notes, and snippets.

@0x17de
Last active March 15, 2024 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x17de/a37b4dec060c2aa769c53dd1d66988b7 to your computer and use it in GitHub Desktop.
Save 0x17de/a37b4dec060c2aa769c53dd1d66988b7 to your computer and use it in GitHub Desktop.
Ansible connection plugin for chroot after ssh - no suppory for sudo included
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)
@0x17de
Copy link
Author

0x17de commented Mar 15, 2024

example use: ansible myhost -e 'ansible_connection=ssh_chroot ansible_chroot_path=/root/chroottest' -m slurp -a 'path=/a'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment