From cc867481092160dc6f36dbbaa233e9f40941e504 Mon Sep 17 00:00:00 2001 From: tottoto Date: Sun, 16 Feb 2020 02:39:05 +0900 Subject: [PATCH] Fix url handling in lxd_container and lxd_profile module (#66097) * Fix url handling in lxd_container module * Fix url handling in lxd_profile module --- changelogs/fragments/lxd_container_url.yaml | 2 ++ changelogs/fragments/lxd_profile_url.yaml | 2 ++ lib/ansible/modules/cloud/lxd/lxd_container.py | 9 +++++++-- lib/ansible/modules/cloud/lxd/lxd_profile.py | 8 ++++++-- 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/lxd_container_url.yaml create mode 100644 changelogs/fragments/lxd_profile_url.yaml diff --git a/changelogs/fragments/lxd_container_url.yaml b/changelogs/fragments/lxd_container_url.yaml new file mode 100644 index 00000000000..77953c66064 --- /dev/null +++ b/changelogs/fragments/lxd_container_url.yaml @@ -0,0 +1,2 @@ +bugfixes: + - Fixes the url handling in lxd_container module that url cannot be specified in lxd environment created by snap. diff --git a/changelogs/fragments/lxd_profile_url.yaml b/changelogs/fragments/lxd_profile_url.yaml new file mode 100644 index 00000000000..1a598e052dc --- /dev/null +++ b/changelogs/fragments/lxd_profile_url.yaml @@ -0,0 +1,2 @@ +bugfixes: + - Fixes the url handling in lxd_profile module that url cannot be specified in lxd environment created by snap. diff --git a/lib/ansible/modules/cloud/lxd/lxd_container.py b/lib/ansible/modules/cloud/lxd/lxd_container.py index fd82d99b789..a75767e244d 100644 --- a/lib/ansible/modules/cloud/lxd/lxd_container.py +++ b/lib/ansible/modules/cloud/lxd/lxd_container.py @@ -300,6 +300,9 @@ ANSIBLE_LXD_STATES = { 'Frozen': 'frozen', } +# ANSIBLE_LXD_DEFAULT_URL is a default value of the lxd endpoint +ANSIBLE_LXD_DEFAULT_URL = 'unix:/var/lib/lxd/unix.socket' + # CONFIG_PARAMS is a list of config attribute names. CONFIG_PARAMS = [ 'architecture', 'config', 'devices', 'ephemeral', 'profiles', 'source' @@ -329,7 +332,9 @@ class LXDContainerManagement(object): self.debug = self.module._verbosity >= 4 try: - if os.path.exists(self.module.params['snap_url'].replace('unix:', '')): + if self.module.params['url'] != ANSIBLE_LXD_DEFAULT_URL: + self.url = self.module.params['url'] + elif os.path.exists(self.module.params['snap_url'].replace('unix:', '')): self.url = self.module.params['snap_url'] else: self.url = self.module.params['url'] @@ -623,7 +628,7 @@ def main(): ), url=dict( type='str', - default='unix:/var/lib/lxd/unix.socket' + default=ANSIBLE_LXD_DEFAULT_URL ), snap_url=dict( type='str', diff --git a/lib/ansible/modules/cloud/lxd/lxd_profile.py b/lib/ansible/modules/cloud/lxd/lxd_profile.py index 06e7f93143f..033fb39d43f 100644 --- a/lib/ansible/modules/cloud/lxd/lxd_profile.py +++ b/lib/ansible/modules/cloud/lxd/lxd_profile.py @@ -182,6 +182,8 @@ import os from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.lxd import LXDClient, LXDClientException +# ANSIBLE_LXD_DEFAULT_URL is a default value of the lxd endpoint +ANSIBLE_LXD_DEFAULT_URL = 'unix:/var/lib/lxd/unix.socket' # PROFILE_STATES is a list for states supported PROFILES_STATES = [ @@ -212,7 +214,9 @@ class LXDProfileManagement(object): self.debug = self.module._verbosity >= 4 try: - if os.path.exists(self.module.params['snap_url'].replace('unix:', '')): + if self.module.params['url'] != ANSIBLE_LXD_DEFAULT_URL: + self.url = self.module.params['url'] + elif os.path.exists(self.module.params['snap_url'].replace('unix:', '')): self.url = self.module.params['snap_url'] else: self.url = self.module.params['url'] @@ -366,7 +370,7 @@ def main(): ), url=dict( type='str', - default='unix:/var/lib/lxd/unix.socket' + default=ANSIBLE_LXD_DEFAULT_URL ), snap_url=dict( type='str',