diff --git a/tripleo_ansible/ansible_plugins/modules/container_puppet_config.py b/tripleo_ansible/ansible_plugins/modules/container_puppet_config.py index 957e23643..4007ce054 100644 --- a/tripleo_ansible/ansible_plugins/modules/container_puppet_config.py +++ b/tripleo_ansible/ansible_plugins/modules/container_puppet_config.py @@ -79,15 +79,27 @@ options: puppet_config: description: Path to the puppet configs type: str + default: "" short_hostname: description: - Short hostname type: str + default: "" step: description: - Step number default: 6 type: int + update_config_hash_only: + description: + - When set to True, the module will only inspect for new config hashes + in config_vol_prefix and make sure the container-startup-configs + are updated with these hashes. This is useful to execute + before we manage the startup containers, so they will be restarted + if needed (e.g. new config has been applied, container needs + restart). + type: bool + default: False """ EXAMPLES = """ @@ -96,6 +108,10 @@ EXAMPLES = """ step: 1 puppet-config: /var/lib/container-puppet/container-puppet.json short_hostname: "{{ ansible_hostname }}" + update_config_hash_only: false +- name: Update config hashes for container startup configs + container_puppet_config: + update_config_hash_only: true """ CONTAINER_PUPPET_CONFIG = '/var/lib/tripleo-config/container-puppet-config' @@ -121,31 +137,34 @@ class ContainerPuppetManager: # Set parameters puppet_config = args['puppet_config'] - data = json.loads(self._slurp(puppet_config)) - - self.step = args['step'] - self.net_host = args['net_host'] - self.debug = args['debug'] - self.check = args['check_mode'] - self.no_archive = args['no_archive'] + update_config_hash_only = args['update_config_hash_only'] self.config_vol_prefix = args['config_vol_prefix'] - self.hostname = args['short_hostname'] - config_path = os.path.join(CONTAINER_PUPPET_CONFIG, - 'step_' + str(self.step)) + if not update_config_hash_only: + data = json.loads(self._slurp(puppet_config)) - # Cleanup old configs generated in previous versions - self._cleanup_old_configs() + self.step = args['step'] + self.net_host = args['net_host'] + self.debug = args['debug'] + self.check = args['check_mode'] + self.no_archive = args['no_archive'] + self.hostname = args['short_hostname'] - # Make sure config_path exists - # Note: it'll cleanup old configs before creating new ones. - self._create_dir(config_path) + config_path = os.path.join(CONTAINER_PUPPET_CONFIG, + 'step_' + str(self.step)) - # Generate the container configs - config = self._get_config(self._merge_volumes_configs(data)) - for k, v in config.items(): - config_dest = os.path.join(config_path, k + '.json') - self._update_container_config(config_dest, v) + # Cleanup old configs generated in previous versions + self._cleanup_old_configs() + + # Make sure config_path exists + # Note: it'll cleanup old configs before creating new ones. + self._create_dir(config_path) + + # Generate the container configs + config = self._get_config(self._merge_volumes_configs(data)) + for k, v in config.items(): + config_dest = os.path.join(config_path, k + '.json') + self._update_container_config(config_dest, v) # Update container-startup-config with new config hashes self._update_hashes() @@ -415,6 +434,7 @@ class ContainerPuppetManager: f = open(path, 'wb') f.write(json.dumps(config, indent=2).encode('utf-8')) os.chmod(path, 0o600) + self.results['changed'] = True def _get_config_hash(self, config_volume): """Returns a config hash from a config_volume. @@ -470,8 +490,6 @@ class ContainerPuppetManager: def _update_hashes(self): """Update container startup config with new config hashes if needed. """ - startup_config_path = os.path.join(CONTAINER_STARTUP_CONFIG, - 'step_' + str(self.step)) configs = self._find(CONTAINER_STARTUP_CONFIG) for config in configs: old_config_hash = '' diff --git a/tripleo_ansible/roles/tripleo_container_manage/defaults/main.yml b/tripleo_ansible/roles/tripleo_container_manage/defaults/main.yml index c2aaf5a00..7a67ebb50 100644 --- a/tripleo_ansible/roles/tripleo_container_manage/defaults/main.yml +++ b/tripleo_ansible/roles/tripleo_container_manage/defaults/main.yml @@ -18,6 +18,7 @@ # All variables intended for modification should place placed in this file. # All variables within this role should have a prefix of "tripleo_container_manage" +tripleo_container_manage_check_puppet_config: false tripleo_container_manage_cli: podman tripleo_container_manage_concurrency: 1 tripleo_container_manage_config: "/var/lib/tripleo-config/" diff --git a/tripleo_ansible/roles/tripleo_container_manage/tasks/main.yml b/tripleo_ansible/roles/tripleo_container_manage/tasks/main.yml index 77fe4b9b8..1e448e79f 100644 --- a/tripleo_ansible/roles/tripleo_container_manage/tasks/main.yml +++ b/tripleo_ansible/roles/tripleo_container_manage/tasks/main.yml @@ -97,6 +97,10 @@ - tripleo_container_manage_cli == 'podman' become: true block: + - name: "Update container configs with new config hashes" + include_tasks: puppet_config.yml + when: + - tripleo_container_manage_check_puppet_config|bool - name: "Delete containers from {{ tripleo_container_manage_config }}" include_tasks: delete.yml - name: "Create containers from {{ tripleo_container_manage_config }}" diff --git a/tripleo_ansible/roles/tripleo_container_manage/tasks/puppet_config.yml b/tripleo_ansible/roles/tripleo_container_manage/tasks/puppet_config.yml new file mode 100644 index 000000000..d4dc170d2 --- /dev/null +++ b/tripleo_ansible/roles/tripleo_container_manage/tasks/puppet_config.yml @@ -0,0 +1,19 @@ +--- +# Copyright 2019 Red Hat, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +- name: Update config hashes for container startup configs + container_puppet_config: + update_config_hash_only: true