diff --git a/ansible/library/kolla_docker.py b/ansible/library/kolla_docker.py index a7f89aaf07..b220a9667e 100644 --- a/ansible/library/kolla_docker.py +++ b/ansible/library/kolla_docker.py @@ -163,6 +163,15 @@ options: - Name or id of container(s) to use volumes from required: True type: list + state: + description: + - Check container status + required: False + type: str + choices: + - running + - exited + - paused author: Sam Yaple ''' @@ -291,7 +300,8 @@ class DockerWorker(object): self.compare_pid_mode(container_info) or self.compare_volumes(container_info) or self.compare_volumes_from(container_info) or - self.compare_environment(container_info) + self.compare_environment(container_info) or + self.compare_container_state(container_info) ) def compare_ipc_mode(self, container_info): @@ -420,6 +430,12 @@ class DockerWorker(object): if current_env[k] != v: return True + def compare_container_state(self, container_info): + new_state = self.params.get('state') + current_state = container_info['State'].get('Status') + if new_state != current_state: + return True + def parse_image(self): full_image = self.params.get('image') @@ -738,6 +754,10 @@ def generate_module(): 'always', 'unless-stopped']), restart_retries=dict(required=False, type='int', default=10), + state=dict(required=False, type='str', default='running', + choices=['running', + 'exited', + 'paused']), tls_verify=dict(required=False, type='bool', default=False), tls_cert=dict(required=False, type='str'), tls_key=dict(required=False, type='str'), diff --git a/tests/test_kolla_docker.py b/tests/test_kolla_docker.py index 578a3ef3ec..4179f9cb38 100644 --- a/tests/test_kolla_docker.py +++ b/tests/test_kolla_docker.py @@ -71,6 +71,10 @@ class ModuleArgsTest(base.BaseTestCase): 'always', 'unless-stopped']), restart_retries=dict(required=False, type='int', default=10), + state=dict(required=False, type='str', default='running', + choices=['running', + 'exited', + 'paused']), tls_verify=dict(required=False, type='bool', default=False), tls_cert=dict(required=False, type='str'), tls_key=dict(required=False, type='str'), @@ -772,3 +776,13 @@ class TestAttrComp(base.BaseTestCase): KOLLA_INSTALL_TYPE='binary')}) self.assertTrue(self.dw.compare_environment(container_info)) + + def test_compare_container_state_neg(self): + container_info = {'State': dict(Status='running')} + self.dw = get_DockerWorker({'state': 'running'}) + self.assertFalse(self.dw.compare_container_state(container_info)) + + def test_compare_container_state_pos(self): + container_info = {'State': dict(Status='running')} + self.dw = get_DockerWorker({'state': 'exited'}) + self.assertTrue(self.dw.compare_container_state(container_info))