docker_compose_v2: add files option (#775)

* Add files option.

* Shorten lines.
This commit is contained in:
Felix Fontein 2024-01-24 07:15:00 +01:00 committed by GitHub
parent b2a79d9eb7
commit eebb73a503
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 2 deletions

View File

@ -0,0 +1,2 @@
minor_changes:
- "docker_compose_v2, docker_compose_v2_pull - support ``files`` parameter to specify multiple Compose files (https://github.com/ansible-collections/community.docker/issues/772, https://github.com/ansible-collections/community.docker/pull/775)."

View File

@ -15,13 +15,23 @@ class ModuleDocFragment(object):
options:
project_src:
description:
- Path to a directory containing a C(docker-compose.yml) or C(docker-compose.yaml) file.
- Path to a directory containing a Compose file
(C(compose.yml), C(compose.yaml), C(docker-compose.yml), or C(docker-compose.yaml)).
- If O(files) is provided, will look for these files in this directory instead.
type: path
required: true
project_name:
description:
- Provide a project name. If not provided, the project name is taken from the basename of O(project_src).
type: str
files:
description:
- List of Compose file names relative to O(project_src) to be used instead of the main Compose file
(C(compose.yml), C(compose.yaml), C(docker-compose.yml), or C(docker-compose.yaml)).
- Files are loaded and merged in the order given.
type: list
elements: path
version_added: 3.7.0
env_files:
description:
- By default environment files are loaded from a C(.env) file located directly under the O(project_src) directory.

View File

@ -399,6 +399,7 @@ def common_compose_argspec():
return dict(
project_src=dict(type='path', required=True),
project_name=dict(type='str'),
files=dict(type='list', elements='path'),
env_files=dict(type='list', elements='path'),
profiles=dict(type='list', elements='str'),
)
@ -421,6 +422,7 @@ class BaseComposeManager(DockerBaseClass):
self.project_src = parameters['project_src']
self.project_name = parameters['project_name']
self.files = parameters['files']
self.env_files = parameters['env_files']
self.profiles = parameters['profiles']
@ -439,7 +441,12 @@ class BaseComposeManager(DockerBaseClass):
if not os.path.isdir(self.project_src):
self.client.fail('"{0}" is not a directory'.format(self.project_src))
if all(not os.path.exists(os.path.join(self.project_src, f)) for f in DOCKER_COMPOSE_FILES):
if self.files:
for file in self.files:
path = os.path.join(self.project_src, file)
if not os.path.exists(path):
self.client.fail('Cannot find Compose file "{0}" relative to project directory "{1}"'.format(file, self.project_src))
elif all(not os.path.exists(os.path.join(self.project_src, f)) for f in DOCKER_COMPOSE_FILES):
filenames = ', '.join(DOCKER_COMPOSE_FILES[:-1])
self.client.fail('"{0}" does not contain {1}, or {2}'.format(self.project_src, filenames, DOCKER_COMPOSE_FILES[-1]))
@ -451,6 +458,8 @@ class BaseComposeManager(DockerBaseClass):
args.extend(['--project-directory', self.project_src])
if self.project_name:
args.extend(['--project-name', self.project_name])
for file in self.files or []:
args.extend(['--file', file])
for env_file in self.env_files or []:
args.extend(['--env-file', env_file])
for profile in self.profiles or []: