From 3ce3aa6d25dad8221b89c8648534d76c18e1ce58 Mon Sep 17 00:00:00 2001 From: Paul Belanger Date: Tue, 30 May 2017 11:01:06 -0400 Subject: [PATCH] Refactor role to latest changes Over the last 2 years we have learned a lot of things with ansible. Bump this role to be inline with others. This also import our role in to openstack-infra testing. Change-Id: I63f9c936fae40e536699cd612c0505e0f3a72d5d Signed-off-by: Paul Belanger --- .gitreview | 2 +- bindep.txt | 10 ++++++ defaults/main.yaml | 15 +++++++- requirements.txt | 2 +- tasks/config.yaml | 3 +- tasks/install.yaml | 6 +--- tasks/install/{debian.yaml => package.yaml} | 7 ++-- tasks/install/redhat.yaml | 18 ---------- tasks/main.yaml | 9 +++-- tests/ansible.cfg | 2 +- tests/inventory | 2 +- tests/test.yaml | 8 ++--- tox.ini | 40 +++++++++++---------- 13 files changed, 65 insertions(+), 59 deletions(-) create mode 100644 bindep.txt rename tasks/install/{debian.yaml => package.yaml} (84%) delete mode 100644 tasks/install/redhat.yaml diff --git a/.gitreview b/.gitreview index cdc80e2..43c3bc5 100644 --- a/.gitreview +++ b/.gitreview @@ -1,4 +1,4 @@ [gerrit] host=review.openstack.org port=29418 -project=openstack/ansible-role-nodepool.git +project=openstack/ansible-role-logrotate.git diff --git a/bindep.txt b/bindep.txt new file mode 100644 index 0000000..cb8c48d --- /dev/null +++ b/bindep.txt @@ -0,0 +1,10 @@ +# This is a cross-platform list tracking distribution packages needed by tests; +# see http://docs.openstack.org/infra/bindep/ for additional information. + +git +libffi-devel [platform:rpm] +libffi-dev [platform:dpkg] +libselinux-python [platform:rpm] +libssl-dev [platform:dpkg] +openssl-devel [platform:rpm] +python2-dnf [platform:fedora] diff --git a/defaults/main.yaml b/defaults/main.yaml index c9d1508..58be96c 100644 --- a/defaults/main.yaml +++ b/defaults/main.yaml @@ -1,4 +1,4 @@ -# Copyright 2015 Red Hat, Inc. +# Copyright 2017 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,6 +12,19 @@ # License for the specific language governing permissions and limitations # under the License. --- + +# tasks/main.yaml +logrotate_task_manager: + - install + - config + +# tasks/install.yaml +logrotate_install_method: package + +logrotate_package_name: logrotate +logrotate_package_state: installed + +# tasks/config.yaml logrotate_configs: [] logrotate_template_logrotate_config_dest: /etc/logrotate.d diff --git a/requirements.txt b/requirements.txt index 90d4055..1ab0ddd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -ansible +ansible>=2.0.0 diff --git a/tasks/config.yaml b/tasks/config.yaml index 3636233..be563c8 100644 --- a/tasks/config.yaml +++ b/tasks/config.yaml @@ -13,10 +13,11 @@ # under the License. --- - name: Template logrotate.d config files. + become: yes template: dest: "{{ logrotate_template_logrotate_config_dest }}/{{ item.name }}" group: root mode: 0644 owner: root src: "{{ logrotate_template_logrotate_config_src }}" - with_items: logrotate_configs + with_items: "{{ logrotate_configs }}" diff --git a/tasks/install.yaml b/tasks/install.yaml index eff9719..97302ac 100644 --- a/tasks/install.yaml +++ b/tasks/install.yaml @@ -12,8 +12,4 @@ # License for the specific language governing permissions and limitations # under the License. --- -- include: install/debian.yaml - when: ansible_os_family == 'Debian' - -- include: install/redhat.yaml - when: ansible_os_family == 'RedHat' +- include: "install/{{ logrotate_install_method }}.yaml" diff --git a/tasks/install/debian.yaml b/tasks/install/package.yaml similarity index 84% rename from tasks/install/debian.yaml rename to tasks/install/package.yaml index 0d08513..e62c2f4 100644 --- a/tasks/install/debian.yaml +++ b/tasks/install/package.yaml @@ -13,6 +13,7 @@ # under the License. --- - name: Ensure logrotate is installed. - apt: - pkg: logrotate - state: installed + become: yes + package: + name: "{{ logrotate_package_name }}" + state: "{{ logrotate_package_state }}" diff --git a/tasks/install/redhat.yaml b/tasks/install/redhat.yaml deleted file mode 100644 index 339f63b..0000000 --- a/tasks/install/redhat.yaml +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright 2015 Red Hat, Inc. -# -# 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: Ensure logrotate is installed. - yum: - pkg: logrotate - state: installed diff --git a/tasks/main.yaml b/tasks/main.yaml index 5c02baf..d9365e8 100644 --- a/tasks/main.yaml +++ b/tasks/main.yaml @@ -12,8 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. --- -- include: install.yaml - sudo: yes - -- include: config.yaml - sudo: yes +- include: "{{ logrotate_task }}.yaml" + with_items: "{{ logrotate_task_manager }}" + loop_control: + loop_var: logrotate_task diff --git a/tests/ansible.cfg b/tests/ansible.cfg index 2d4c5f9..6c8a344 100644 --- a/tests/ansible.cfg +++ b/tests/ansible.cfg @@ -1,2 +1,2 @@ [defaults] -roles_path = .. +roles_path = ../.. diff --git a/tests/inventory b/tests/inventory index df8b5f6..59f3595 100644 --- a/tests/inventory +++ b/tests/inventory @@ -1,2 +1,2 @@ [all] -localhost ansible_connection=local +localhost ansible_connection=ssh diff --git a/tests/test.yaml b/tests/test.yaml index 5fa16b8..008b7bf 100644 --- a/tests/test.yaml +++ b/tests/test.yaml @@ -13,15 +13,15 @@ # under the License. --- - hosts: localhost - sudo: yes vars: rolename: "{{ lookup('pipe', 'pwd') | dirname | basename }}" + pre_tasks: # Make sure OS does not have a stale package cache. - name: Update apt cache. - apt: - update_cache: yes - when: ansible_os_family == 'Debian' + become: yes + package: + update_cache: "{{ (ansible_pkg_mgr == 'apt') | ternary('yes', omit) }}" roles: - "{{ rolename }}" diff --git a/tox.ini b/tox.ini index 23aec5d..60b7f91 100644 --- a/tox.ini +++ b/tox.ini @@ -1,27 +1,19 @@ [tox] minversion = 1.4.2 -envlist = ansible-lint,docs,pep8 +envlist = docs,linters skipsdist = True [testenv] deps = -r{toxinidir}/requirements.txt -r{toxinidir}/test-requirements.txt -[testenv:ansible-lint] -setenv = - ANSIBLE_CONFIG = tests/ansible.cfg -whitelist_externals = bash +[testenv:functional] commands = - bash -c "find . -type f -regex '.*.y[a]?ml' -print0 | xargs -t -n1 -0 \ - ansible-lint" - bash -c "find tests -type f -regex '.*.y[a]?ml' -print | xargs -t -n1 \ - ansible-playbook --syntax-check -i tests/inventory \ - -e rolename=$(basename $(pwd)) > /dev/null" - -[testenv:ansible-functional] -commands = - ansible-playbook -i tests/inventory tests/test.yaml -passenv = HOME + # NOTE(pabelanger): Because ansible default ansible_user to null now, we need to pass it via CLI. + ansible-playbook -i tests/inventory tests/test.yaml -e ansible_user={env:USER} +passenv = + HOME + USER setenv = ANSIBLE_CONFIG = {toxinidir}/tests/ansible.cfg PYTHONUNBUFFERED = 1 @@ -29,9 +21,6 @@ setenv = [testenv:docs] commands = python setup.py build_sphinx -[testenv:pep8] -commands = flake8 - [testenv:venv] commands = {posargs} @@ -42,3 +31,18 @@ show-source = True ignore = E123,E125 builtins = _ exclude=.venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build + +[testenv:linters] +setenv = + ANSIBLE_CONFIG = tests/ansible.cfg +whitelist_externals = bash +commands = + # PEP8 Lint Check + flake8 + # Ansible Lint Check + bash -c "find . -type f -regex '.*.y[a]?ml' -print0 | xargs -t -n1 -0 \ + ansible-lint" + # Ansible Syntax Check + bash -c "find tests -type f -regex '.*.y[a]?ml' -print | xargs -t -n1 \ + ansible-playbook --syntax-check -i tests/inventory \ + -e rolename=$(basename $(pwd)) > /dev/null"