解决方案
为了使用多个版本的 ansible、python 和各种 Linux 风格测试 ansible 角色,我们可以使用
- 用于我们的 ansible 角色功能的分子
- docker作为我们为我们的 ansible 角色运行目标系统的抽象层
- tox设置通用 virtualenvs 并测试我们的各种组合而没有副作用
- travis让这一切自动化
这将是一个相当长/详细的答案。您可以在此处查看带有整个设置的示例 ansible 角色
第 1 步:用分子测试 ansible 角色
分子文档:https ://molecule.readthedocs.io/en/stable/
修复问题:1) 没有官方的 ansible 图像
正如 jeff geerling 在他的博客文章中所描述的那样,我可以为我想测试的每个发行版创建 ansible 图像。
这种方法的明显缺点:图像需要维护(最终)
但是,使用分子,我们可以将基础镜像与 Dockerfile.j2(Jinja2 模板)结合起来,以创建运行 ansible 的最低要求的镜像。通过这种方法,我们现在可以使用来自 docker hub 的官方 linux 发行版镜像,并且不需要为每个 linux 发行版和各种版本维护一个 docker repo。
这里是分子.yml 中的重要位
platforms:
- name: instance-${TOX_ENVNAME}
image: ${MOLECULE_DISTRO:-'centos:7'}
command: /sbin/init
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
分子默认的 dockerfile.j2 已经很好了,但我有一些补充
# Molecule managed
{% if item.registry is defined %}
FROM {{ item.registry.url }}/{{ item.image }}
{% else %}
FROM {{ item.image }}
{% endif %}
{% if item.env is defined %}
{% for var, value in item.env.items() %}
{% if value %}
ENV {{ var }} {{ value }}
{% endif %}
{% endfor %}
{% endif %}
RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates iproute2 && apt-get clean; \
elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml iproute2 && zypper clean -a; \
elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \
elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates iproute2 && xbps-remove -O; \
elif [ $(command -v swupd) ]; then swupd bundle-add python3-basic sudo iproute2; \
elif [ $(command -v dnf) ] && cat /etc/os-release | grep -q '^NAME=Fedora'; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash iproute && dnf clean all; \
elif [ $(command -v dnf) ] && cat /etc/os-release | grep -q '^NAME="CentOS Linux"' ; then dnf makecache && dnf --assumeyes install python36 sudo platform-python-devel python*-dnf bash iproute && dnf clean all && ln -s /usr/bin/python3 /usr/bin/python; \
elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl bash iproute && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \
fi
# Centos:8 + ansible 2.7 failed with error: "The module failed to execute correctly, you probably need to set the interpreter"
# Solution: ln -s /usr/bin/python3 /usr/bin/python
默认情况下,这将使用 centos:7 测试角色。但是,我们可以将环境变量 MOLECULE_DISTRO 设置为我们想要测试并运行它的任何图像
MOLECULE_DISTRO=ubuntu:18.04 molecule test
概括
我们使用来自 docker hub 的官方发行版镜像来通过分子测试我们的 ansible 角色。
此步骤中使用的文件
资源
第 2 步:检查您的角色的兼容性(python 版本 X ansible 版本 X linux 发行版)
修复问题 2:如何最好地测试角色的 ansible 版本兼容性?
让我们使用 tox 创建虚拟环境,以避免在测试各种兼容性场景时产生副作用。
这里是 tox.ini 中的重要部分
[tox]
minversion = 3.7
envlist = py{3}-ansible{latest,29,28}-{ alpinelatest,alpine310,alpine39,alpine38, centoslatest,centos8,centos7, debianlatest,debian10,debian9,debian8, fedoralatest,fedora30,fedora29,fedora28, ubuntulatest,ubuntu2004,ubuntu1904,ubuntu1804,ubuntu1604 }
# only test currently supported ansible versions
# https://docs.ansible.com/ansible/latest/reference_appendices/release_and_maintenance.html#release-status
skipsdist = true
[base]
passenv = *
deps =
-rrequirements.txt
ansible25: ansible==2.5
...
ansiblelatest: ansible
commands =
molecule test
setenv =
TOX_ENVNAME={envname}
MOLECULE_EPHEMERAL_DIRECTORY=/tmp/{envname}
[testenv]
passenv =
{[base]passenv}
deps =
{[base]deps}
commands =
{[base]commands}
setenv =
...
centoslatest: MOLECULE_DISTRO="centos:latest"
centos8: MOLECULE_DISTRO="centos:8"
centos7: MOLECULE_DISTRO="centos:7"
...
{[base]setenv}
完整的 requirements.txt
docker
molecule
通过简单地执行
tox
它将为 tox.ini 中定义的每个兼容性组合创建虚拟环境
envlist = py{3}-ansible{latest,29,28}-{ alpinelatest,alpine310,alpine39,alpine38, centoslatest,centos8,centos7, debianlatest,debian10,debian9,debian8, fedoralatest,fedora30,fedora29,fedora28, ubuntulatest,ubuntu2004,ubuntu1904,ubuntu1804,ubuntu1604 }
在这种特殊情况下转换为:python3 x ansible version x linux distro
伟大的!我们已经为兼容性检查创建了测试,其额外的好处是始终使用 ansible latest 进行测试以尽早发现重大变化。
此步骤中使用的文件
资源
第 3 步:使用 travis 进行 CI
在本地运行检查很好,在 CI 工具中运行很好。所以让我们这样做。
为此目的,.travis.yml 中的以下位很重要
---
version: ~> 1.0
os: linux
language: python
python:
- "3.8"
- "3.7"
- "3.6"
- "3.5"
services: docker
cache:
pip: true
directories:
- .tox
install:
- pip install tox-travis
env:
jobs:
# ansible:latest - check for breaking changes
...
- TOX_DISTRO="centoslatest" TOX_ANSIBLE="latest"
- TOX_DISTRO="centos8" TOX_ANSIBLE="latest"
- TOX_DISTRO="centos7" TOX_ANSIBLE="latest"
...
# ansible: check version compatibility
# only test currently supported ansible versions
#
https://docs.ansible.com/ansible/latest/reference_appendices/release_and_maintenance.html#release-status
- TOX_DISTRO="centoslatest" TOX_ANSIBLE="{29,28}"
- TOX_DISTRO="centos8" TOX_ANSIBLE="{29,28}"
- TOX_DISTRO="centos7" TOX_ANSIBLE="{29,28}"
...
script:
- tox -e $(echo py${TRAVIS_PYTHON_VERSION} | tr -d .)-ansible${TOX_ANSIBLE}-${TOX_DISTRO}
# remove logs/pycache before caching .tox folder
- |
rm -r .tox/py*/log/*
find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -delete
首先,我们指定使用列表language: python
中定义的多个 python 版本运行构建。python:
我们需要 docker,所以我们通过services: docker
.
测试需要相当长的时间,让我们缓存 pip 和我们由 tox 创建的 virtenv
cache:
pip: true
directories:
- .tox
我们需要毒...
install:
- pip install tox-travis
最后,我们定义了所有的测试用例
env:
jobs:
# ansible:latest - check for breaking changes
...
- TOX_DISTRO="centoslatest" TOX_ANSIBLE="latest"
...
请注意,我对最新版本和不同版本有单独的工作。那是故意的。我想很容易地看到什么坏了。是版本兼容性还是 ansible 最新版本中即将发生的变化。
此步骤中使用的文件
资源
奖励:并行运行 tox
您可以通过执行并行运行测试(例如同时进行 3 个测试)
tox -p 3
但是,这不会给出分子的输出。你可以启用它
tox -p 3 -o true
这种方法的明显缺点是在并行执行中找出哪条线属于哪个进程的痛苦。
资源