Create tripleo_container_config_scripts module

This module will help to simplify the task that creates the container
config scripts, generated via config download.

It'll replace the loop in THT that iterate over a file to create the
scripts and their content, and executer faster on module.

Change-Id: I4bbd54d193271daa10383b8cb48c9ea9aa0b5b74
This commit is contained in:
Emilien Macchi 2020-08-13 15:38:37 -04:00
parent 2980f52f1b
commit b6f979ca1e
5 changed files with 201 additions and 0 deletions

View File

@ -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

View File

@ -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()

View File

@ -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)

View File

@ -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

View File

@ -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