mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-06 14:14:22 -07:00
feat: add options for setting remote user and become method
This commit is contained in:
parent
28fb13bdff
commit
4a661375bb
1 changed files with 45 additions and 19 deletions
|
@ -32,6 +32,13 @@ options:
|
||||||
vars:
|
vars:
|
||||||
- name: ansible_executable
|
- name: ansible_executable
|
||||||
- name: ansible_lxd_executable
|
- name: ansible_lxd_executable
|
||||||
|
lxd_become_method:
|
||||||
|
description:
|
||||||
|
- Become command used to switch to a non-root user.
|
||||||
|
type: str
|
||||||
|
default: /bin/su
|
||||||
|
vars:
|
||||||
|
- name: lxd_become_method
|
||||||
remote:
|
remote:
|
||||||
description:
|
description:
|
||||||
- Name of the LXD remote to use.
|
- Name of the LXD remote to use.
|
||||||
|
@ -40,6 +47,21 @@ options:
|
||||||
vars:
|
vars:
|
||||||
- name: ansible_lxd_remote
|
- name: ansible_lxd_remote
|
||||||
version_added: 2.0.0
|
version_added: 2.0.0
|
||||||
|
remote_user:
|
||||||
|
description:
|
||||||
|
- User to login/authenticate as.
|
||||||
|
- Can be set from the CLI via the C(--user) or C(-u) options.
|
||||||
|
type: string
|
||||||
|
vars:
|
||||||
|
- name: ansible_user
|
||||||
|
- name: ansible_ssh_user
|
||||||
|
env:
|
||||||
|
- name: ANSIBLE_REMOTE_USER
|
||||||
|
ini:
|
||||||
|
- section: defaults
|
||||||
|
key: remote_user
|
||||||
|
keyword:
|
||||||
|
- name: remote_user
|
||||||
project:
|
project:
|
||||||
description:
|
description:
|
||||||
- Name of the LXD project to use.
|
- Name of the LXD project to use.
|
||||||
|
@ -63,7 +85,6 @@ class Connection(ConnectionBase):
|
||||||
|
|
||||||
transport = 'community.general.lxd'
|
transport = 'community.general.lxd'
|
||||||
has_pipelining = True
|
has_pipelining = True
|
||||||
default_user = None
|
|
||||||
|
|
||||||
def __init__(self, play_context, new_stdin, *args, **kwargs):
|
def __init__(self, play_context, new_stdin, *args, **kwargs):
|
||||||
super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs)
|
super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs)
|
||||||
|
@ -73,8 +94,6 @@ class Connection(ConnectionBase):
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise AnsibleError("lxc command not found in PATH")
|
raise AnsibleError("lxc command not found in PATH")
|
||||||
|
|
||||||
self._play_user = self._play_context.remote_user or self.default_user
|
|
||||||
|
|
||||||
def _host(self):
|
def _host(self):
|
||||||
""" translate remote_addr to lxd (short) hostname """
|
""" translate remote_addr to lxd (short) hostname """
|
||||||
return self.get_option("remote_addr").split(".", 1)[0]
|
return self.get_option("remote_addr").split(".", 1)[0]
|
||||||
|
@ -84,33 +103,40 @@ class Connection(ConnectionBase):
|
||||||
super(Connection, self)._connect()
|
super(Connection, self)._connect()
|
||||||
|
|
||||||
if not self._connected:
|
if not self._connected:
|
||||||
self._display.vvv(f"ESTABLISH LXD CONNECTION FOR USER: {self._play_user}", host=self._host())
|
self._display.vvv(f"ESTABLISH LXD CONNECTION FOR USER: {self.get_option("remote_user")}", host=self._host())
|
||||||
self._connected = True
|
self._connected = True
|
||||||
|
|
||||||
def _build_exec_command(self, cmd):
|
def _build_exec_command(self, cmd) -> str:
|
||||||
""" build the command to execute """
|
""" build the command to execute on the lxd host """
|
||||||
if self._play_user != "root":
|
|
||||||
cmd = f"su {self._play_user} {cmd}"
|
|
||||||
return cmd
|
|
||||||
|
|
||||||
def exec_command(self, cmd, in_data=None, sudoable=True):
|
exec_cmd = [self._lxc_cmd]
|
||||||
""" execute a command on the lxd host """
|
|
||||||
|
|
||||||
cmd = self._build_exec_command(cmd)
|
|
||||||
super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable)
|
|
||||||
|
|
||||||
self._display.vvv(f"EXEC {cmd}", host=self._host())
|
|
||||||
|
|
||||||
local_cmd = [self._lxc_cmd]
|
|
||||||
if self.get_option("project"):
|
if self.get_option("project"):
|
||||||
local_cmd.extend(["--project", self.get_option("project")])
|
exec_cmd.extend(["--project", self.get_option("project")])
|
||||||
local_cmd.extend([
|
|
||||||
|
if self.get_option("remote_user") != "root":
|
||||||
|
self._display.vvv(
|
||||||
|
f"INFO: Running as non-root user: {self.get_option('remote_user')}, trying to run 'lxc exec' with become method: {self.get_option('lxd_become_method')}",
|
||||||
|
host=self._host(),
|
||||||
|
)
|
||||||
|
exec_cmd.extend([self.get_option("lxd_become_method"), self.get_option("remote_user")])
|
||||||
|
|
||||||
|
exec_cmd.extend([
|
||||||
"exec",
|
"exec",
|
||||||
f"{self.get_option('remote')}:{self._host()}",
|
f"{self.get_option('remote')}:{self._host()}",
|
||||||
"--",
|
"--",
|
||||||
self.get_option("executable"), "-c", cmd
|
self.get_option("executable"), "-c", cmd
|
||||||
])
|
])
|
||||||
|
|
||||||
|
return exec_cmd
|
||||||
|
|
||||||
|
def exec_command(self, cmd, in_data=None, sudoable=True):
|
||||||
|
""" execute a command on the lxd host """
|
||||||
|
super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable)
|
||||||
|
|
||||||
|
self._display.vvv(f"EXEC {cmd}", host=self._host())
|
||||||
|
|
||||||
|
local_cmd = self._build_exec_command(cmd)
|
||||||
self._display.vvvvv(f"EXEC {local_cmd}", host=self._host())
|
self._display.vvvvv(f"EXEC {local_cmd}", host=self._host())
|
||||||
|
|
||||||
local_cmd = [to_bytes(i, errors='surrogate_or_strict') for i in local_cmd]
|
local_cmd = [to_bytes(i, errors='surrogate_or_strict') for i in local_cmd]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue