解决方案
为了使用多个版本的 ansible、python 和我们可以使用的各种 Linux 版本来测试 ansible 角色
-
molecule 用于我们的 ansible 角色功能
-
docker 作为我们为我们的 ansible 角色运行目标系统的抽象层
-
tox 设置通用 virtualenvs 并测试我们的各种组合而不会产生副作用
-
travis 将这一切自动化
这将是一个相当长/详细的答案。您可以在此处查看包含整个设置的示例 ansible 角色
第 1 步:用分子测试 ansible 角色
分子文档:https://molecule.readthedocs.io/en/stable/
修复问题:1) 没有官方的 ansible 图片
我可以为我想测试的每个发行版创建 ansible 图像,正如 jeff gerling 在他的博客文章中所描述的那样。
这种方法的明显缺点:图像需要维护(最终)
但是,通过分子,我们可以将基础镜像与 Dockerfile.j2(Jinja2 模板)结合起来,以创建运行 ansible 的最低要求的镜像。通过这种方法,我们现在可以使用来自 docker hub 的官方 linux 发行版镜像,并且不需要为每个 linux 发行版和各种版本维护一个 docker repo。
这里是molecule.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
这种方法的明显缺点是在并行执行中找出哪一行属于哪个进程的痛苦。
来源