1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

fix lxd connection plugin inventory_hostname (#4912)

* fixes lxd connection plugin issue #4886

remote_addr value was set to literal string 'inventory_hostname' instead
of the value for inventory_hostname variable. solution found in PR
ansible/ansible#77894

* changelog fragment - bugfix - lxd connection plugin

* correct changelog fragment

* Update changelogs/fragments/4886-fix-lxd-inventory-hostname.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* replace _host instance variable with calls to get 'remote_addr' option

suggested by felixfontein

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
antonc42 2022-07-07 14:49:10 -05:00 committed by GitHub
parent 1c06e237c8
commit 905f9ec399
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 7 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- "lxd connection plugin - fix incorrect ``inventory_hostname`` in ``remote_addr``. This is needed for compatibility with ansible-core 2.13 (https://github.com/ansible-collections/community.general/issues/4886)."

View file

@ -18,6 +18,7 @@ DOCUMENTATION = '''
- Container identifier. - Container identifier.
default: inventory_hostname default: inventory_hostname
vars: vars:
- name: inventory_hostname
- name: ansible_host - name: ansible_host
- name: ansible_lxd_host - name: ansible_lxd_host
executable: executable:
@ -61,7 +62,6 @@ class Connection(ConnectionBase):
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)
self._host = self._play_context.remote_addr
try: try:
self._lxc_cmd = get_bin_path("lxc") self._lxc_cmd = get_bin_path("lxc")
except ValueError: except ValueError:
@ -75,14 +75,14 @@ class Connection(ConnectionBase):
super(Connection, self)._connect() super(Connection, self)._connect()
if not self._connected: if not self._connected:
self._display.vvv(u"ESTABLISH LXD CONNECTION FOR USER: root", host=self._host) self._display.vvv(u"ESTABLISH LXD CONNECTION FOR USER: root", host=self.get_option('remote_addr'))
self._connected = True self._connected = True
def exec_command(self, cmd, in_data=None, sudoable=True): def exec_command(self, cmd, in_data=None, sudoable=True):
""" execute a command on the lxd host """ """ execute a command on the lxd host """
super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable) super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable)
self._display.vvv(u"EXEC {0}".format(cmd), host=self._host) self._display.vvv(u"EXEC {0}".format(cmd), host=self.get_option('remote_addr'))
local_cmd = [self._lxc_cmd] local_cmd = [self._lxc_cmd]
if self.get_option("project"): if self.get_option("project"):
@ -104,10 +104,10 @@ class Connection(ConnectionBase):
stderr = to_text(stderr) stderr = to_text(stderr)
if stderr == "error: Container is not running.\n": if stderr == "error: Container is not running.\n":
raise AnsibleConnectionFailure("container not running: %s" % self._host) raise AnsibleConnectionFailure("container not running: %s" % self.get_option('remote_addr'))
if stderr == "error: not found\n": if stderr == "error: not found\n":
raise AnsibleConnectionFailure("container not found: %s" % self._host) raise AnsibleConnectionFailure("container not found: %s" % self.get_option('remote_addr'))
return process.returncode, stdout, stderr return process.returncode, stdout, stderr
@ -115,7 +115,7 @@ class Connection(ConnectionBase):
""" put a file from local to lxd """ """ put a file from local to lxd """
super(Connection, self).put_file(in_path, out_path) super(Connection, self).put_file(in_path, out_path)
self._display.vvv(u"PUT {0} TO {1}".format(in_path, out_path), host=self._host) self._display.vvv(u"PUT {0} TO {1}".format(in_path, out_path), host=self.get_option('remote_addr'))
if not os.path.isfile(to_bytes(in_path, errors='surrogate_or_strict')): if not os.path.isfile(to_bytes(in_path, errors='surrogate_or_strict')):
raise AnsibleFileNotFound("input path is not a file: %s" % in_path) raise AnsibleFileNotFound("input path is not a file: %s" % in_path)
@ -138,7 +138,7 @@ class Connection(ConnectionBase):
""" fetch a file from lxd to local """ """ fetch a file from lxd to local """
super(Connection, self).fetch_file(in_path, out_path) super(Connection, self).fetch_file(in_path, out_path)
self._display.vvv(u"FETCH {0} TO {1}".format(in_path, out_path), host=self._host) self._display.vvv(u"FETCH {0} TO {1}".format(in_path, out_path), host=self.get_option('remote_addr'))
local_cmd = [self._lxc_cmd] local_cmd = [self._lxc_cmd]
if self.get_option("project"): if self.get_option("project"):