From 429d8c8cfb3d95c4f2a04f9accbd26f53832ac9b Mon Sep 17 00:00:00 2001
From: corubba <corubba@gmx.de>
Date: Sun, 8 Oct 2023 18:30:20 +0200
Subject: [PATCH] Migrate to options

Because the lxc plugin was only using PlayContext properties, using host
vars like `ansible_lxc_host` didn't work. This is fixed by instead using
the `get_option` method inherited from `AnsiblePlugin`.
The options are not yet available in the `__init__` function, so the
determination of the container name is moved to the `_connect` method,
which is the first time it is actually needed.
The default for the `remote_addr` option is removed, because the string
`inventory_hostname` is not very useful. At all. This seams to have been
spread with copy&paste and a bit of cargo culting. The variable priority
already takes care of setting the value.
---
 plugins/connection/lxc.py                 |  7 ++++---
 tests/unit/plugins/connection/test_lxc.py | 13 +++++++++++++
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/plugins/connection/lxc.py b/plugins/connection/lxc.py
index e24a71248c..39723dd54a 100644
--- a/plugins/connection/lxc.py
+++ b/plugins/connection/lxc.py
@@ -17,7 +17,6 @@ DOCUMENTATION = '''
       remote_addr:
         description:
             - Container identifier
-        default: inventory_hostname
         vars:
             - name: inventory_hostname
             - name: ansible_host
@@ -60,7 +59,7 @@ class Connection(ConnectionBase):
     def __init__(self, play_context, new_stdin, *args, **kwargs):
         super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs)
 
-        self.container_name = self._play_context.remote_addr
+        self.container_name = None
         self.container = None
 
     def _connect(self):
@@ -74,6 +73,8 @@ class Connection(ConnectionBase):
         if self.container:
             return
 
+        self.container_name = self.get_option('remote_addr')
+
         self._display.vvv("THIS IS A LOCAL LXC DIR", host=self.container_name)
         self.container = _lxc.Container(self.container_name)
         if self.container.state == "STOPPED":
@@ -118,7 +119,7 @@ class Connection(ConnectionBase):
         super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable)
 
         # python2-lxc needs bytes. python3-lxc needs text.
-        executable = to_native(self._play_context.executable, errors='surrogate_or_strict')
+        executable = to_native(self.get_option('executable'), errors='surrogate_or_strict')
         local_cmd = [executable, '-c', to_native(cmd, errors='surrogate_or_strict')]
 
         read_stdout, write_stdout = None, None
diff --git a/tests/unit/plugins/connection/test_lxc.py b/tests/unit/plugins/connection/test_lxc.py
index 4d50df3a94..eb1ec35894 100644
--- a/tests/unit/plugins/connection/test_lxc.py
+++ b/tests/unit/plugins/connection/test_lxc.py
@@ -71,3 +71,16 @@ class TestLXCConnectionClass():
 
         with pytest.raises(AnsibleError, match='lxc python bindings are not installed'):
             conn._connect()
+
+    def test_remote_addr_option(self):
+        """Test that the remote_addr option is used"""
+        play_context = PlayContext()
+        in_stream = StringIO()
+        conn = connection_loader.get('lxc', play_context, in_stream)
+
+        container_name = 'my-container'
+        conn.set_option('remote_addr', container_name)
+        assert conn.get_option('remote_addr') == container_name
+
+        conn._connect()
+        assert conn.container_name == container_name