From bfc2681ad539e55087c1f4a15e638c34c31dc7e1 Mon Sep 17 00:00:00 2001 From: Emilien Macchi Date: Thu, 13 Aug 2020 14:24:38 -0400 Subject: [PATCH] tripleo_container_configs module To replace the task that looks over kolla_config.yaml and creates the json files per item in the YAML, create a module that will lead to one task creating all the files. Change-Id: I19d8b8c3bc37cca6fb2c9e535e70b43dabef58d6 --- .../modules-tripleo-container-configs.rst | 14 +++ .../modules/tripleo_container_configs.py | 92 +++++++++++++++++++ .../tripleo_container_configs/converge.yml | 33 +++++++ .../tripleo_container_configs/molecule.yml | 50 ++++++++++ .../tripleo_container_configs/prepare.yml | 20 ++++ 5 files changed, 209 insertions(+) create mode 100644 doc/source/modules/modules-tripleo-container-configs.rst create mode 100644 tripleo_ansible/ansible_plugins/modules/tripleo_container_configs.py create mode 100644 tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/converge.yml create mode 100644 tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/molecule.yml create mode 100644 tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/prepare.yml diff --git a/doc/source/modules/modules-tripleo-container-configs.rst b/doc/source/modules/modules-tripleo-container-configs.rst new file mode 100644 index 000000000..bacf73f22 --- /dev/null +++ b/doc/source/modules/modules-tripleo-container-configs.rst @@ -0,0 +1,14 @@ +================================== +Module - tripleo_container_configs +================================== + + +This module provides for the following ansible plugin: + + * tripleo_container_configs + + +.. ansibleautoplugin:: + :module: tripleo_ansible/ansible_plugins/modules/tripleo_container_configs.py + :documentation: true + :examples: true diff --git a/tripleo_ansible/ansible_plugins/modules/tripleo_container_configs.py b/tripleo_ansible/ansible_plugins/modules/tripleo_container_configs.py new file mode 100644 index 000000000..79b5ee3ec --- /dev/null +++ b/tripleo_ansible/ansible_plugins/modules/tripleo_container_configs.py @@ -0,0 +1,92 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright 2020 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. +__metaclass__ = type + +import json +import os +import yaml + +from ansible.module_utils.basic import AnsibleModule + +DOCUMENTATION = """ +--- +module: tripleo_container_configs +author: + - "TripleO team" +version_added: '2.9' +short_description: Generate Container configs in JSON +notes: [] +description: + - It will generate the Container JSON configs from config-download data in + YAML. +requirements: + - None +options: + config_data: + description: + - Content of kolla_config.yaml file (must be YAML format) + type: dict + required: true +""" + +EXAMPLES = """ +- name: Write container config json files + tripleo_container_configs: + config_data: + /var/lib/kolla/config_files/ceilometer_agent_compute.json: + command: /usr/bin/ceilometer-polling compute + config_files: + - dest: / + merge: true + preserve_properties: true + source: /var/lib/kolla/config_files/src/* + /var/lib/kolla/config_files/ceilometer_agent_notification.json: + command: /usr/bin/ceilometer-agent-notification + config_files: + - dest: / + merge: true + preserve_properties: true + source: /var/lib/kolla/config_files/src/* +""" + + +def main(): + module = AnsibleModule( + argument_spec=yaml.safe_load(DOCUMENTATION)['options'], + supports_check_mode=True, + ) + results = dict( + changed=False + ) + + # parse args + args = module.params + + # Set parameters + config_data = args['config_data'] + + for path, config in config_data.items(): + with open(path, "wb") as config_file: + config_file.write(json.dumps(config, indent=2).encode('utf-8')) + os.chmod(path, 0o600) + results['changed'] = True + + module.exit_json(**results) + + +if __name__ == '__main__': + main() diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/converge.yml b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/converge.yml new file mode 100644 index 000000000..f8a94dd97 --- /dev/null +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/converge.yml @@ -0,0 +1,33 @@ +--- +- name: Converge + hosts: all + tasks: + - name: Write container config json files + tripleo_container_configs: + config_data: + /tmp/container_config1.json: + command: /usr/bin/ceilometer-polling compute + config_files: + - dest: / + merge: true + preserve_properties: true + source: /var/lib/kolla/config_files/src/* + /tmp/container_config2.json: + command: /usr/bin/ceilometer-agent-notification + config_files: + - dest: / + merge: true + preserve_properties: true + source: /var/lib/kolla/config_files/src/* + - name: Check that container_config1.json file was created + stat: + path: "/tmp/container_config1.json" + register: st_config + failed_when: + - not st_config.stat.exists + - name: Check that container_config1.json configuration is correct + slurp: + src: "/tmp/container_config1.json" + register: slurp_config + failed_when: + - ('ceilometer-polling' not in slurp_config['content']|b64decode) diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/molecule.yml b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/molecule.yml new file mode 100644 index 000000000..631011e96 --- /dev/null +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/molecule.yml @@ -0,0 +1,50 @@ +--- +driver: + name: delegated + options: + managed: false + login_cmd_template: >- + ssh + -o UserKnownHostsFile=/dev/null + -o StrictHostKeyChecking=no + -o Compression=no + -o TCPKeepAlive=yes + -o VerifyHostKeyDNS=no + -o ForwardX11=no + -o ForwardAgent=no + {instance} + ansible_connection_options: + ansible_connection: ssh + +log: true + +platforms: + - name: instance + +provisioner: + name: ansible + config_options: + defaults: + fact_caching: jsonfile + fact_caching_connection: /tmp/molecule/facts + inventory: + hosts: + all: + hosts: + instance: + ansible_host: localhost + log: true + env: + ANSIBLE_STDOUT_CALLBACK: yaml + ANSIBLE_ROLES_PATH: "${ANSIBLE_ROLES_PATH:-/usr/share/ansible/roles}:${HOME}/zuul-jobs/roles" + ANSIBLE_LIBRARY: "${ANSIBLE_LIBRARY:-/usr/share/ansible/plugins/modules}" + ANSIBLE_FILTER_PLUGINS: "${ANSIBLE_FILTER_PLUGINS:-/usr/share/ansible/plugins/filter}" + +scenario: + name: tripleo_container_configs + test_sequence: + - prepare + - converge + +verifier: + name: testinfra diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/prepare.yml b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/prepare.yml new file mode 100644 index 000000000..805f61ee9 --- /dev/null +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_configs/prepare.yml @@ -0,0 +1,20 @@ +--- +# 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: Prepare + hosts: all + roles: + - role: test_deps