From 0a037c65f7804316d4b1c3f1753d4c35bc8fbfbb Mon Sep 17 00:00:00 2001 From: Emilien Macchi Date: Wed, 13 Nov 2019 17:12:01 +0100 Subject: [PATCH] tripleo-container-manage: restart systemd service if container changed If a podman_container resource changed, it'll be added to a fact, that later will be used when it comes to figure our if a systemd service needs to be restarted or not. It introduces a new filter: list_of_keys. This filter takes in input a list of dictionaries and for each of them it will add the key to list_of_keys and returns it. Change-Id: I0285b006c015f6cd223615ebdad52286f7683a87 --- tripleo_ansible/ansible_plugins/filter/helpers.py | 15 ++++++++++++++- .../tasks/podman/create.yml | 14 +++++++++++++- .../tasks/podman/systemd.yml | 3 +-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/tripleo_ansible/ansible_plugins/filter/helpers.py b/tripleo_ansible/ansible_plugins/filter/helpers.py index 605360c90..5baa90074 100644 --- a/tripleo_ansible/ansible_plugins/filter/helpers.py +++ b/tripleo_ansible/ansible_plugins/filter/helpers.py @@ -26,7 +26,8 @@ class FilterModule(object): 'singledict': self.singledict, 'subsort': self.subsort, 'needs_delete': self.needs_delete, - 'haskey': self.haskey + 'haskey': self.haskey, + 'list_of_keys': self.list_of_keys } def subsort(self, dict_to_sort, attribute, null_value=None): @@ -169,3 +170,15 @@ class FilterModule(object): if attribute not in v and reverse: return_list.append({k: v}) return return_list + + def list_of_keys(self, keys_to_list): + """Return a list of keys from a list of dictionaries. + + This filter takes in input a list of dictionaries and for each of them + it will add the key to list_of_keys and returns it. + """ + list_of_keys = [] + for i in keys_to_list: + for k, v in i.items(): + list_of_keys.append(k) + return list_of_keys diff --git a/tripleo_ansible/roles/tripleo-container-manage/tasks/podman/create.yml b/tripleo_ansible/roles/tripleo-container-manage/tasks/podman/create.yml index 9a2411420..068aa0590 100644 --- a/tripleo_ansible/roles/tripleo-container-manage/tasks/podman/create.yml +++ b/tripleo_ansible/roles/tripleo-container-manage/tasks/podman/create.yml @@ -14,7 +14,6 @@ # License for the specific language governing permissions and limitations # under the License. -# TODO(emilien) Re-add podman_container.changed from async - name: "Async container create/run" async: 300 poll: 0 @@ -74,3 +73,16 @@ until: create_async_poll_results.finished retries: 30 when: not ansible_check_mode|bool + +# This fact will be used in systemd playbook to figure out if whether or not +# a container managed by systemd needs to be restarted +- name: "Create a list of containers which changed" + set_fact: + containers_changed: >- + {{ create_async_results.results | selectattr('changed', 'equalto', true) | + map(attribute='container_data') | list | list_of_keys }} + +- name: "Print the list of containers which changed" + debug: + var: containers_changed + when: tripleo_container_manage_debug | bool diff --git a/tripleo_ansible/roles/tripleo-container-manage/tasks/podman/systemd.yml b/tripleo_ansible/roles/tripleo-container-manage/tasks/podman/systemd.yml index cbaa93868..b028e8aea 100644 --- a/tripleo_ansible/roles/tripleo-container-manage/tasks/podman/systemd.yml +++ b/tripleo_ansible/roles/tripleo-container-manage/tasks/podman/systemd.yml @@ -52,8 +52,7 @@ enabled: true daemon_reload: true when: - # Re-add podman_container.changed from async - - systemd_file.changed + - systemd_file is changed or container_sysd_name in containers_changed - name: "Manage systemd healthcheck for {{ container_sysd_name }}" when: - not tripleo_container_manage_healthcheck_disabled