From 6528aefcb5b5ac3bd8e9f55f5ff18fcdb6211537 Mon Sep 17 00:00:00 2001 From: Ishwar Kanse <10349173+IshwarKanse@users.noreply.github.com> Date: Mon, 4 Jan 2021 11:00:47 +0530 Subject: [PATCH] Added support for remote project in the LXD connection plugin (#1515) --- plugins/connection/lxd.py | 43 +++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/plugins/connection/lxd.py b/plugins/connection/lxd.py index 754b4f9dbc..ab317b0f7a 100644 --- a/plugins/connection/lxd.py +++ b/plugins/connection/lxd.py @@ -14,7 +14,7 @@ DOCUMENTATION = ''' options: remote_addr: description: - - Container identifier + - Container identifier. default: inventory_hostname vars: - name: ansible_host @@ -26,6 +26,19 @@ DOCUMENTATION = ''' vars: - name: ansible_executable - name: ansible_lxd_executable + remote: + description: + - Name of the LXD remote to use. + default: local + vars: + - name: ansible_lxd_remote + version_added: 2.0.0 + project: + description: + - Name of the LXD project to use. + vars: + - name: ansible_lxd_project + version_added: 2.0.0 ''' import os @@ -70,7 +83,15 @@ class Connection(ConnectionBase): self._display.vvv(u"EXEC {0}".format(cmd), host=self._host) - local_cmd = [self._lxc_cmd, "exec", self._host, "--", self._play_context.executable, "-c", cmd] + local_cmd = [self._lxc_cmd] + if self.get_option("project"): + local_cmd.extend(["--project", self.get_option("project")]) + local_cmd.extend([ + "exec", + "%s:%s" % (self.get_option("remote"), self._host), + "--", + self._play_context.executable, "-c", cmd + ]) local_cmd = [to_bytes(i, errors='surrogate_or_strict') for i in local_cmd] in_data = to_bytes(in_data, errors='surrogate_or_strict', nonstring='passthru') @@ -98,7 +119,14 @@ class Connection(ConnectionBase): if not os.path.isfile(to_bytes(in_path, errors='surrogate_or_strict')): raise AnsibleFileNotFound("input path is not a file: %s" % in_path) - local_cmd = [self._lxc_cmd, "file", "push", in_path, self._host + "/" + out_path] + local_cmd = [self._lxc_cmd] + if self.get_option("project"): + local_cmd.extend(["--project", self.get_option("project")]) + local_cmd.extend([ + "file", "push", + in_path, + "%s:%s/%s" % (self.get_option("remote"), self._host, out_path) + ]) local_cmd = [to_bytes(i, errors='surrogate_or_strict') for i in local_cmd] @@ -111,7 +139,14 @@ class Connection(ConnectionBase): self._display.vvv(u"FETCH {0} TO {1}".format(in_path, out_path), host=self._host) - local_cmd = [self._lxc_cmd, "file", "pull", self._host + "/" + in_path, out_path] + local_cmd = [self._lxc_cmd] + if self.get_option("project"): + local_cmd.extend(["--project", self.get_option("project")]) + local_cmd.extend([ + "file", "pull", + "%s:%s/%s" % (self.get_option("remote"), self._host, in_path), + out_path + ]) local_cmd = [to_bytes(i, errors='surrogate_or_strict') for i in local_cmd]