From 9bbd3b26df331ad7307d8f0fe69894277eafdecc Mon Sep 17 00:00:00 2001 From: Gael Chamoulaud Date: Mon, 22 Jul 2019 16:04:23 +0200 Subject: [PATCH] Adding new role automation with Ansible This patch adds a basic skeleton to create a new roles including molecule structure. Change-Id: I483d5e7b11376dc0141c506bf07ccd5876e4076e Patch-Inspired-By: tripleo-ansible-Team++ Signed-off-by: Gael Chamoulaud --- _skeleton_role_/defaults/main.yml.j2 | 22 +++++++++ _skeleton_role_/handlers/main.yml | 15 ++++++ .../molecule/default/Dockerfile.j2 | 38 ++++++++++++++ _skeleton_role_/molecule/default/molecule.yml | 49 +++++++++++++++++++ .../molecule/default/playbook.yml.j2 | 22 +++++++++ _skeleton_role_/molecule/default/verify.yml | 15 ++++++ _skeleton_role_/tasks/main.yml.j2 | 18 +++++++ _skeleton_role_/vars/main.yml.j2 | 22 +++++++++ doc/source/readme.rst | 13 +++++ role-addition.yml | 35 +++++++++++++ 10 files changed, 249 insertions(+) create mode 100644 _skeleton_role_/defaults/main.yml.j2 create mode 100644 _skeleton_role_/handlers/main.yml create mode 100644 _skeleton_role_/molecule/default/Dockerfile.j2 create mode 100644 _skeleton_role_/molecule/default/molecule.yml create mode 100644 _skeleton_role_/molecule/default/playbook.yml.j2 create mode 100644 _skeleton_role_/molecule/default/verify.yml create mode 100644 _skeleton_role_/tasks/main.yml.j2 create mode 100644 _skeleton_role_/vars/main.yml.j2 create mode 100644 role-addition.yml diff --git a/_skeleton_role_/defaults/main.yml.j2 b/_skeleton_role_/defaults/main.yml.j2 new file mode 100644 index 000000000..7e0e13ce7 --- /dev/null +++ b/_skeleton_role_/defaults/main.yml.j2 @@ -0,0 +1,22 @@ +--- +# 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. + + +# All variables intended for modification should place placed in this file. + +# All variables within this role should have a prefix of "{{ role_name }}" +{{ role_name }}_debug: false + diff --git a/_skeleton_role_/handlers/main.yml b/_skeleton_role_/handlers/main.yml new file mode 100644 index 000000000..dfd4c7352 --- /dev/null +++ b/_skeleton_role_/handlers/main.yml @@ -0,0 +1,15 @@ +--- +# 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. diff --git a/_skeleton_role_/molecule/default/Dockerfile.j2 b/_skeleton_role_/molecule/default/Dockerfile.j2 new file mode 100644 index 000000000..d18ed1764 --- /dev/null +++ b/_skeleton_role_/molecule/default/Dockerfile.j2 @@ -0,0 +1,38 @@ +# Molecule managed +# 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. + +{% raw %} +{% if item.registry is defined %} +FROM {{ item.registry.url }}/{{ item.image }} +{% else %} +FROM {{ item.image }} +{% endif %} + +RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates && apt-get clean; \ + elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash {{ item.pkg_extras | default('') }} && dnf clean all; \ + elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl python-setuptools bash {{ item.pkg_extras | default('') }} && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ + elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml {{ item.pkg_extras | default('') }} && zypper clean -a; \ + elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates {{ item.pkg_extras | default('') }}; \ + elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates {{ item.pkg_extras | default('') }} && xbps-remove -O; fi + +{% for pkg in item.easy_install | default([]) %} +# install pip for centos where there is no python-pip rpm in default repos +RUN easy_install {{ pkg }} +{% endfor %} +{% endraw %} + +CMD ["sh", "-c", "while true; do sleep 10000; done"] + diff --git a/_skeleton_role_/molecule/default/molecule.yml b/_skeleton_role_/molecule/default/molecule.yml new file mode 100644 index 000000000..1dbdf8927 --- /dev/null +++ b/_skeleton_role_/molecule/default/molecule.yml @@ -0,0 +1,49 @@ +--- +driver: + name: docker + +log: true + +platforms: + - name: centos7 + hostname: centos7 + image: centos:7 + dockerfile: Dockerfile + pkg_extras: python-setuptools + easy_install: + - pip + environment: &env + http_proxy: "{{ lookup('env', 'http_proxy') }}" + https_proxy: "{{ lookup('env', 'https_proxy') }}" + + - name: fedora28 + hostname: fedora28 + image: fedora:28 + dockerfile: Dockerfile + pkg_extras: python*-setuptools + environment: + <<: *env + +provisioner: + name: ansible + log: true + env: + ANSIBLE_STDOUT_CALLBACK: yaml + ANSIBLE_LIBRARY: "../../../../library" + +scenario: + test_sequence: + - destroy + - create + - prepare + - converge + - verify + - destroy + +lint: + enabled: false + +verifier: + name: testinfra + lint: + name: flake8 diff --git a/_skeleton_role_/molecule/default/playbook.yml.j2 b/_skeleton_role_/molecule/default/playbook.yml.j2 new file mode 100644 index 000000000..0a22b2a1b --- /dev/null +++ b/_skeleton_role_/molecule/default/playbook.yml.j2 @@ -0,0 +1,22 @@ +--- +# 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: Converge + hosts: all + roles: + - role: "{{ role_name }}" + diff --git a/_skeleton_role_/molecule/default/verify.yml b/_skeleton_role_/molecule/default/verify.yml new file mode 100644 index 000000000..dfd4c7352 --- /dev/null +++ b/_skeleton_role_/molecule/default/verify.yml @@ -0,0 +1,15 @@ +--- +# 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. diff --git a/_skeleton_role_/tasks/main.yml.j2 b/_skeleton_role_/tasks/main.yml.j2 new file mode 100644 index 000000000..79eda3b36 --- /dev/null +++ b/_skeleton_role_/tasks/main.yml.j2 @@ -0,0 +1,18 @@ +--- +# 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. + + +# "{{ role_name }}" tasks diff --git a/_skeleton_role_/vars/main.yml.j2 b/_skeleton_role_/vars/main.yml.j2 new file mode 100644 index 000000000..d4719abd0 --- /dev/null +++ b/_skeleton_role_/vars/main.yml.j2 @@ -0,0 +1,22 @@ +--- +# 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. + + +# While options found within the vars/ path can be overridden using extra +# vars, items within this path are considered part of the role and not +# intended to be modified. + +# All variables within this role should have a prefix of "{{ role_name }}" diff --git a/doc/source/readme.rst b/doc/source/readme.rst index 4d23ebc42..d82d6f28e 100644 --- a/doc/source/readme.rst +++ b/doc/source/readme.rst @@ -497,3 +497,16 @@ is low enough and the other too high) And that's it! The validation is now finished and you can start using it in earnest. + +Create a new role with automation +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The role addition process is also automated using ansible. If ansible is +available on the development workstation change directory to the root of +the `tripleo-validations` repository and run the the following command which +will perform the basic tasks noted above. + +.. code-block:: console + + $ cd tripleo-validations/ + $ ansible-playbook -i localhost, role-addition.yml -e role_name=${NEWROLENAME} diff --git a/role-addition.yml b/role-addition.yml new file mode 100644 index 000000000..aa9dbe37b --- /dev/null +++ b/role-addition.yml @@ -0,0 +1,35 @@ +--- +# 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: Create a new role for TripleO-Validations + hosts: localhost + connection: local + gather_facts: false + tasks: + - name: Check for role name + fail: + msg: >- + The required variable `role_name` is undefined. Check your settings. + when: + - role_name is undefined + + - name: Create role + command: >- + ansible-galaxy init + --role-skeleton=_skeleton_role_ + --init-path=roles {{ role_name }} + args: + creates: "roles/{{ role_name }}"