summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/general/plugins/connection/lxd.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-18 05:52:22 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-18 05:52:22 +0000
commit38b7c80217c4e72b1d8988eb1e60bb6e77334114 (patch)
tree356e9fd3762877d07cde52d21e77070aeff7e789 /ansible_collections/community/general/plugins/connection/lxd.py
parentAdding upstream version 7.7.0+dfsg. (diff)
downloadansible-38b7c80217c4e72b1d8988eb1e60bb6e77334114.tar.xz
ansible-38b7c80217c4e72b1d8988eb1e60bb6e77334114.zip
Adding upstream version 9.4.0+dfsg.upstream/9.4.0+dfsg
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ansible_collections/community/general/plugins/connection/lxd.py')
-rw-r--r--ansible_collections/community/general/plugins/connection/lxd.py42
1 files changed, 26 insertions, 16 deletions
diff --git a/ansible_collections/community/general/plugins/connection/lxd.py b/ansible_collections/community/general/plugins/connection/lxd.py
index affb87dfd..0e784b85f 100644
--- a/ansible_collections/community/general/plugins/connection/lxd.py
+++ b/ansible_collections/community/general/plugins/connection/lxd.py
@@ -10,13 +10,15 @@ __metaclass__ = type
DOCUMENTATION = '''
author: Matt Clay (@mattclay) <matt@mystile.com>
name: lxd
- short_description: Run tasks in lxc containers via lxc CLI
+ short_description: Run tasks in LXD instances via C(lxc) CLI
description:
- - Run commands or put/fetch files to an existing lxc container using lxc CLI
+ - Run commands or put/fetch files to an existing instance using C(lxc) CLI.
options:
remote_addr:
description:
- - Container identifier.
+ - Instance (container/VM) identifier.
+ - Since community.general 8.0.0, a FQDN can be provided; in that case, the first component (the part before C(.))
+ is used as the instance identifier.
default: inventory_hostname
vars:
- name: inventory_hostname
@@ -24,7 +26,7 @@ DOCUMENTATION = '''
- name: ansible_lxd_host
executable:
description:
- - shell to use for execution inside container
+ - Shell to use for execution inside instance.
default: /bin/sh
vars:
- name: ansible_executable
@@ -69,32 +71,38 @@ class Connection(ConnectionBase):
raise AnsibleError("lxc command not found in PATH")
if self._play_context.remote_user is not None and self._play_context.remote_user != 'root':
- self._display.warning('lxd does not support remote_user, using container default: root')
+ self._display.warning('lxd does not support remote_user, using default: root')
+
+ def _host(self):
+ """ translate remote_addr to lxd (short) hostname """
+ return self.get_option("remote_addr").split(".", 1)[0]
def _connect(self):
"""connect to lxd (nothing to do here) """
super(Connection, self)._connect()
if not self._connected:
- self._display.vvv(u"ESTABLISH LXD CONNECTION FOR USER: root", host=self.get_option('remote_addr'))
+ self._display.vvv(u"ESTABLISH LXD CONNECTION FOR USER: root", host=self._host())
self._connected = True
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(u"EXEC {0}".format(cmd), host=self.get_option('remote_addr'))
+ self._display.vvv(u"EXEC {0}".format(cmd), host=self._host())
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.get_option("remote_addr")),
+ "%s:%s" % (self.get_option("remote"), self._host()),
"--",
self.get_option("executable"), "-c", cmd
])
+ self._display.vvvvv(u"EXEC {0}".format(local_cmd), host=self._host())
+
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')
@@ -104,11 +112,13 @@ class Connection(ConnectionBase):
stdout = to_text(stdout)
stderr = to_text(stderr)
- if stderr == "error: Container is not running.\n":
- raise AnsibleConnectionFailure("container not running: %s" % self.get_option('remote_addr'))
+ self._display.vvvvv(u"EXEC lxc output: {0} {1}".format(stdout, stderr), host=self._host())
+
+ if "is not running" in stderr:
+ raise AnsibleConnectionFailure("instance not running: %s" % self._host())
- if stderr == "error: not found\n":
- raise AnsibleConnectionFailure("container not found: %s" % self.get_option('remote_addr'))
+ if stderr.strip() == "Error: Instance not found" or stderr.strip() == "error: not found":
+ raise AnsibleConnectionFailure("instance not found: %s" % self._host())
return process.returncode, stdout, stderr
@@ -116,7 +126,7 @@ class Connection(ConnectionBase):
""" put a file from local to lxd """
super(Connection, self).put_file(in_path, out_path)
- self._display.vvv(u"PUT {0} TO {1}".format(in_path, out_path), host=self.get_option('remote_addr'))
+ self._display.vvv(u"PUT {0} TO {1}".format(in_path, out_path), host=self._host())
if not os.path.isfile(to_bytes(in_path, errors='surrogate_or_strict')):
raise AnsibleFileNotFound("input path is not a file: %s" % in_path)
@@ -127,7 +137,7 @@ class Connection(ConnectionBase):
local_cmd.extend([
"file", "push",
in_path,
- "%s:%s/%s" % (self.get_option("remote"), self.get_option("remote_addr"), out_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]
@@ -139,14 +149,14 @@ class Connection(ConnectionBase):
""" fetch a file from lxd to local """
super(Connection, self).fetch_file(in_path, out_path)
- self._display.vvv(u"FETCH {0} TO {1}".format(in_path, out_path), host=self.get_option('remote_addr'))
+ self._display.vvv(u"FETCH {0} TO {1}".format(in_path, out_path), host=self._host())
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.get_option("remote_addr"), in_path),
+ "%s:%s/%s" % (self.get_option("remote"), self._host(), in_path),
out_path
])