diff --git a/doc/source/modules/modules-tripleo-container-config-scripts.rst b/doc/source/modules/modules-tripleo-container-config-scripts.rst new file mode 100644 index 000000000..294467f0f --- /dev/null +++ b/doc/source/modules/modules-tripleo-container-config-scripts.rst @@ -0,0 +1,14 @@ +========================================= +Module - tripleo_container_config_scripts +========================================= + + +This module provides for the following ansible plugin: + + * tripleo_container_config_scripts + + +.. ansibleautoplugin:: + :module: tripleo_ansible/ansible_plugins/modules/tripleo_container_config_scripts.py + :documentation: true + :examples: true diff --git a/tripleo_ansible/ansible_plugins/modules/tripleo_container_config_scripts.py b/tripleo_ansible/ansible_plugins/modules/tripleo_container_config_scripts.py new file mode 100644 index 000000000..d1a8934f7 --- /dev/null +++ b/tripleo_ansible/ansible_plugins/modules/tripleo_container_config_scripts.py @@ -0,0 +1,88 @@ +#!/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 os +import yaml + +from ansible.module_utils.basic import AnsibleModule + +DOCUMENTATION = """ +--- +module: tripleo_container_config_scripts +author: + - "TripleO team" +version_added: '2.9' +short_description: Generate container config scripts +notes: [] +description: + - It will generate the TripleO container config scripts. +requirements: + - None +options: + config_data: + description: + - Content of container_config_scripts.yaml file (must be YAML format) + type: dict + required: true + config_dir: + description: + - Directory where config scripts will be written. + type: str + default: /var/lib/container-config-scripts +""" + +EXAMPLES = """ +- name: Write container config scripts + tripleo_container_config_scripts: + config_data: + container_puppet_apply.sh: + content: "#!/bin/bash\npuppet apply" + mode: "0700" + config_dir: /var/lib/container-config-scripts +""" + + +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'] + config_dir = args['config_dir'] + + for path, config in config_data.items(): + # this is specific to how the files are written in config-download + mode = config.get('mode', '0600') + config_path = os.path.join(config_dir, path) + with open(config_path, "w") as config_file: + config_file.write(config['content']) + os.chmod(config_path, int(mode, 8)) + results['changed'] = True + + module.exit_json(**results) + + +if __name__ == '__main__': + main() diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/converge.yml b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/converge.yml new file mode 100644 index 000000000..5b1fa30bc --- /dev/null +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/converge.yml @@ -0,0 +1,29 @@ +--- +- name: Converge + hosts: all + tasks: + - name: Create temporary directory for container config scripts + tempfile: + state: directory + suffix: container_config_scripts_tmp + register: container_config_scripts_tmp_dir + - name: Write container config scripts + tripleo_container_config_scripts: + config_data: + container_puppet_apply.sh: + content: "#!/bin/bash\npuppet apply" + mode: "0700" + config_dir: "{{ container_config_scripts_tmp_dir.path }}" + - name: Check that container_puppet_apply.sh file was created with right permissions + stat: + path: "{{ container_config_scripts_tmp_dir.path }}/container_puppet_apply.sh" + register: st_config + failed_when: + - not st_config.stat.exists + - not (st_config.stat.mode == '0700') + - name: Check that container_puppet_apply.sh script is correct + slurp: + src: "{{ container_config_scripts_tmp_dir.path }}/container_puppet_apply.sh" + register: slurp_config + failed_when: + - ('puppet apply' not in slurp_config['content']|b64decode) diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/molecule.yml b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/molecule.yml new file mode 100644 index 000000000..ff0274cc6 --- /dev/null +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/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_config_scripts + test_sequence: + - prepare + - converge + +verifier: + name: testinfra diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/prepare.yml b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/prepare.yml new file mode 100644 index 000000000..805f61ee9 --- /dev/null +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_container_config_scripts/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