场景
我想发展不可信的角色。这些角色应通过分子CI/CD过程进行验证,并利用对接者作为驱动程序。这个验证步骤应该包括多个Linux口味(例如centos/ubuntu/debian),而不是支持的ansible版本。
然后,应该执行这些测试,以便使用
centos:7 + ansible:2.5
centos:7 + ansible:2.6
centos:7 + ansible:2.7
...
ubuntu:1604 + ansible:2.5
ubuntu:1604 + ansible:2.6
ubuntu:1604 + ansible:2.7
...眼前的问题
第1期:无官方图片
ansible团队的官方图片已经被废弃了大约3年了:
此外,为了找到支持ansible的新映像,所引用的不推荐图像的链接是非常无用的,因为结果非常多。
https://hub.docker.com/search/?q=ansible&page=1&isAutomated=0&isOfficial=0&pullCount=1&starCount=0
是否有一个维护良好的社区(或ansible)维护良好的可移植码头形象,以填补空白?
更好的有多个版本,可以拉,一个CI过程,建立和验证所创建的形象定期。
,为什么我要寻找不可信的图片?,我不想重新发明轮子(如果可能的话)。我想使用这些图像通过分子测试不可接受的角色,因为版本不兼容。
我搜索了一下,但找不到真正有用的东西。您使用哪些映像在容器/调度器中运行ansible?你自己建立和维护这些形象吗?
例如https://hub.docker.com/r/ansibleplaybookbundle/apb-base/tags
看起来很有希望,然而,在那里的图像也有超过7个月的历史(至少)。
问题2:如何最好地测试一个角色以实现不可接受版本的兼容性?
是否为操作系统和ansible版本的每个组合创建对接图像是通过分子和码头作为驱动程序进行测试的最佳方法?还是有一种更聪明的方法来测试多个操作系统和不同的ansible版本之间的向后兼容性?
我已经测试了我的角色,分子和码头作为司机。这些测试目前只是在各种Linux发行版上测试该角色的功能,而不是通过运行该角色与旧版本的ansible向后兼容性。
这里有一个针对centos7 7/ubuntu1604/ubuntu1804的travis测试的示例角色,它基于geerlingguy的ntp角色:https://github.com/Gepardec/ansible-role-ntp
发布于 2019-11-14 13:00:00
解决方案
为了测试多个版本的ansible、python和各种Linux版本的ansible角色,我们可以使用
这将是一个相当长/详细的答案。您可以在这里查看带有整个设置的ansible角色示例
第一步:用分子测试不可接受的作用
分子文档:https://molecule.readthedocs.io/en/stable/
修复问题: 1)没有官方的不可接受的图像
我可以为我想测试的每个发行版创建ansible图像,就像jeff在他的博客文章中描述的那样。
这种方法的明显缺点是:图像需要维护(最终)。
然而,利用分子,我们可以将基本图像与Dockerfile.j2 (Jinja2模板)结合起来,创建运行ansible所需的最小要求的图像。通过这种方法,我们现在能够使用来自docker的正式linux发行版映像,并且不需要为每个linux发行版和不同版本维护一个docker。
这里是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摘要
我们使用来自码头枢纽的官方发行版图像,通过分子测试我们的不可接受的角色。
本步骤中使用的文件
源
步骤2:检查角色的兼容性(发行版)
修复问题2:如何最好地测试一个角色以实现不可接受版本的兼容性?
让我们使用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中定义的每个兼容性组合创建虚拟env。
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发行版。
太棒了!我们已经创建了兼容性检查的测试,它的额外好处是始终使用ansible最新的测试来注意早期的中断更改。
本步骤中使用的文件
源
第三步: CI与travis
在本地运行检查是好的,在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构建。
我们需要码头,所以我们通过services: docker添加它。
测试将花费相当长的时间,让我们缓存pip和我们的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最新版本中即将进行的更改。
本步骤中使用的文件
源
奖励:并行运行毒理。
您可以通过执行并行运行测试(例如3次同时测试)
tox -p 3然而,这不会给出分子的输出。你可以用
tox -p 3 -o true这种方法的明显缺点是找出哪一行属于并行执行中的哪个进程是痛苦的。
源
发布于 2019-11-06 14:36:59
这里没有真正的答案,但有一些想法:
无壳筒仓可能适合,但一年内没有承诺。
这并不是你要找的东西,但是应该是“Runner”用例的合适对象。它是Ansible Tower / AWS的一部分,所以应该会持续很久。
作为自动化和工具的一部分,运行程序最有用,这些工具需要调用Ansible并使用其结果。
他们确实提到了从容器这里执行
Ansible Runner的设计使得它特别适合于从容器内控制单目的自动化工作流中的Ansible执行。
但是对于您来说,问题是官方的ansible/ ansible -runner容器是在ansible-runner版本之后标记的,ansible本身是在容器构建时通过pip install ansible安装的。
https://stackoverflow.com/questions/58706600
复制相似问题