6

设想

我想开发ansible角色。这些角色应该通过 CI/CD 流程与分子进行验证,并利用 docker 作为驱动程序。这个验证步骤应该包括多个 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 图像
  2. 如何最好地测试角色的 ansible 版本兼容性?

问题 1:没有官方的 ansible 图像

ansible 团队的官方图片已经弃用了大约 3 年:

此外,由于结果数量庞大,不推荐使用的图像所指的链接以查找支持 ansible 的新图像非常无用

https://hub.docker.com/search/?q=ansible&page=1&isAutomated=0&isOfficial=0&pullCount=1&starCount=0

社区(或 ansible)是否有一个维护良好的 ansible docker 镜像来填补空白?

最好使用可以拉取的多个版本以及定期构建和验证创建的映像的 CI 流程。

为什么我要寻找 ansible 图像?我不想重新发明轮子(如果可能的话)。我想使用图像通过分子测试 ansible 角色的版本不兼容性。

我搜索但找不到任何真正有用的东西。您使用什么图像在容器/协调器中运行 ansible?您自己构建和维护图像吗?

例如https://hub.docker.com/r/ansibleplaybookbundle/apb-base/tags

看起来很有希望,然而,那里的图像也有超过 7 个月的历史(至少)。


问题 2:如何最好地测试角色的 ansible 版本兼容性?

为 OS 和 ansible 版本的每个组合创建 docker 映像是通过分子和 docker 作为驱动程序进行测试的最佳方式吗?或者是否有更聪明的方法来测试 ansible 角色与多个操作系统时间不同的 ansible 版本的向后兼容性?

我已经用分子和 docker 作为驱动程序测试了我的 ansible 角色。这些测试目前仅测试角色在各种 Linux 发行版上的功能,而不是通过使用旧版 ansible 运行角色来测试 ansible 向后兼容性。

这里是一个基于 geerlingguy 的 ntp 角色对 centos7/ubuntu1604/ubuntu1804 进行 travis 测试的示例角色:https ://github.com/Gepardec/ansible-role-ntp

4

2 回答 2

11

解决方案

为了使用多个版本的 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

这种方法的明显缺点是在并行执行中找出哪条线属于哪个进程的痛苦。

资源

于 2019-11-14T13:00:00.330 回答
2

这里没有真正的答案,但有一些想法:

Ansible Silo可能已经安装,但一年内没有提交。

这并不是您正在寻找的,但 Ansible Runner 旨在适合“运行 ansible”用例。它是 Ansible Tower / AWS 的一部分,所以它应该持续存在。

Runner 旨在作为需要调用 Ansible 并使用其结果的自动化和工具的一部分最有用。

他们确实提到在这里从容器执行

Ansible Runner 的设计使其特别适合从容器内控制 Ansible 的执行,以实现单一用途的自动化工作流程

但是对你来说问题是官方的 ansible/ansible-runner 容器是在 ansible-runner 版本之后标记的,并且 ansible 本身是pip install ansible在容器构建时安装的。

于 2019-11-06T14:36:59.513 回答