【问题标题】:How to test ansible roles?如何测试 ansible 角色?
【发布时间】:2019-11-05 07:41:28
【问题描述】:

场景

我想开发 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

【问题讨论】:

  • 安装 ansible 就像 pip install 'ansible ==x.y.z' 一样简单,我只需将它添加到任何需要它的镜像构建中。
  • 好点,我更新了问题。是的,我可以创建自己的 dockerfile 并使用 buildargs 指定 ansible 版本,创建一个 CI 进程来构建所有可用的 ansible 版本并将它们推送到 dockerhub。但如果社区维护的图像已经存在,我想避免这种情况。如果没有,我会这样做。
  • 不是史诗 ci,但我每天都使用这个 Dockerfile 构建开发分支 (cron)。

标签: docker testing ansible tox molecule


【解决方案1】:

解决方案

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

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

来源

【讨论】:

  • 感谢您提供如此详细的答案,我会用“分子”一词来回答!
【解决方案2】:

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

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

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

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

他们确实提到了从容器执行 here

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

但你的问题是官方的ansible/ansible-runner容器是在ansible-runner版本之后标记的,而ansible本身是在容器构建时通过pip install ansible安装的。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多