持续集成¶
如果标准输出是交互式TTY并且`TERM`值似乎支持,Molecule输出将使用`ANSI`颜色。您可以定义`PY_COLORS=1`强制使用`ANSI`颜色,这对于某些CI系统来说非常方便。
GitHub Actions¶
GitHub Actions运行CI管道,与任何其他CI管道类似,它内置于GitHub中。
一个操作,将仓库克隆为`molecule_demo`,并在ubuntu中运行`molecule test`。
---
name: Molecule Test
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: ["3.10", "3.11"]
steps:
- uses: actions/checkout@v2
with:
path: molecule_demo
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements.txt
- name: Test with molecule
run: |
molecule test
如果您需要访问私有仓库中的需求,请创建一个令牌,赋予其所需的权限,然后为您的仓库定义一个`GIT_CREDENTIALS`密钥,其值类似于`https://username:token@github.com/`,最后在“使用molecule测试”之前添加以下步骤。
- name: Setup git credentials
uses: fusion-engineering/setup-git-credentials@v2
with:
credentials: ${{ secrets.GIT_CREDENTIALS }}
Travis CI¶
Travis是一个CI平台,可用于测试Ansible角色。
一个使用Docker驱动程序测试名为foo1的角色的`.travis.yml`。
---
sudo: required
language: python
services:
- docker
install:
- python3 -m pip install molecule
# - python3 -m pip install required driver (e.g. docker, shade, boto, apache-libcloud)
script:
- molecule test
一个使用Tox的`.travis.yml`,如下所述。
---
sudo: required
language: python
services:
- docker
install:
- python3 -m pip install tox-travis
script:
- tox
Gitlab CI¶
Gitlab包含其自身的CI。管道通常定义在仓库顶级目录中的`.gitlab-ci.yml`文件中,以便在Gitlab Runners上运行。
这是一个在Docker in Docker中使用的示例
---
image: docker:stable-dind
services:
- docker:dind
before_script:
- apk add --no-cache
python3 python3-dev py3-pip gcc git curl build-base
autoconf automake py3-cryptography linux-headers
musl-dev libffi-dev openssl-dev openssh
- docker info
- python3 --version
- python3 -m pip install ansible molecule-plugins[docker]
- ansible --version
- molecule --version
molecule:
stage: test
script:
- cd roles/testrole && molecule test
GitLab Runner用于运行您的作业并将结果发送回GitLab。通过为Runner标记其可以处理的作业类型,您可以确保共享Runner只会运行它们能够运行的作业。
这是另一个在CentOS 7上使用Docker、virtualenv和标签的示例。
---
stages:
- test
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.pip"
GIT_STRATEGY: clone
cache:
paths:
- .pip/
- virtenv/
before_script:
- python -V
- pip install virtualenv
- virtualenv virtenv
- source virtenv/bin/activate
- pip install ansible molecule docker
- ansible --version
- molecule --version
- docker --version
molecule:
stage: test
tags:
- molecule-jobs
script:
- molecule test
Azure Pipelines¶
Azure Pipelines项目依赖于仓库根目录中的`azure-pipelines.yml`文件。如果您打算使用`UsePythonVersion`任务,则在Azure上使用自托管运行程序运行CI/CD有一些先决条件。有关此内容的详细信息,请参阅Use Python Version Task文档。
---
trigger:
- main
pool:
vmImage: ubuntu-16.04
steps:
- checkout: git://project-name/role-name
path: role-name
- task: UsePythonVersion@0
inputs:
versionSpec: "3.10"
- script: python3 -m pip install "molecule[lint]" "python-vagrant" "molecule-vagrant" "ansible"
displayName: Install dependencies
- script: python3 -m pip install "python-tss-sdk"
displayName: Role-specific dependencies
- script: |
export PATH="$PATH:/home/<user>/.local/bin/"
cd $(Agent.BuildDirectory)/role-name
molecule test
displayName: Test relevant platforms
虽然管道最初在管道任务的一部分中检出您的代码,但默认情况下,它会将其检出到`$(Agent.BuildDirectory)`中的名为`s`的目录中。如果您检出另一个仓库,则`s`将被该检出中提供的路径替换。如果您检出多个角色(例如,您Azure组织中的某些私有角色),则使用`s`结构,因此`cd $(Agent.BuildDirectory)/role-name`非常重要,它确保您位于正确的目录中,无论格式如何。有关这些内容的更多详细信息,请查看Azure Build Variables文档。
`export PATH`是必需的,以确保您可以使用`molecule`/`ansible` shell脚本。Azure默认情况下不会添加这些脚本。
Jenkins Pipeline¶
Jenkins项目也可以在文件中定义,默认情况下名为仓库顶级目录中的`Jenkinsfile`。有两种语法可用,声明式和脚本式。这是一个使用声明式语法的示例,它设置了一个virtualenv并通过Molecule测试Ansible角色。
pipeline {
agent {
// Node setup : minimal centos7, plugged into Jenkins, and
// git config --global http.sslVerify false
// sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm
// sudo yum -y install python36u python36u-pip python36u-devel git curl gcc
// git config --global http.sslVerify false
// sudo curl -fsSL get.docker.com | bash
label 'Molecule_Slave'
}
stages {
stage ('Get latest code') {
steps {
checkout scm
}
}
stage ('Setup Python virtual environment') {
steps {
sh '''
export HTTP_PROXY=http://10.123.123.123:8080
export HTTPS_PROXY=http://10.123.123.123:8080
python3 -m pip install virtualenv
virtualenv virtenv
source virtenv/bin/activate
python3 -m pip install --upgrade ansible molecule docker
'''
}
}
stage ('Display versions') {
steps {
sh '''
source virtenv/bin/activate
docker -v
python -V
ansible --version
molecule --version
'''
}
}
stage ('Molecule test') {
steps {
sh '''
source virtenv/bin/activate
molecule test
'''
}
}
}
}
以下Jenkinsfile使用Ansible Creator Execution Environment镜像。
pipeline {
agent {
docker {
image 'quay.io/ansible/creator-ee'
args '-v /var/run/docker.sock:/var/run/docker.sock'
}
}
stages {
stage ('Display versions') {
steps {
sh '''
docker -v
python -V
ansible --version
molecule --version
'''
}
}
stage ('Molecule test') {
steps {
sh 'sudo molecule test --all'
}
}
} // close stages
} // close pipeline
注意
为了使Jenkins与多分支管道或GitHub组织(如Blue Ocean所用)一起正常工作,scenario converge.yml中的角色名称应更改为执行角色根目录的查找。例如
---
- name: Converge
hosts: all
roles:
- role: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') | basename }}"
这是当前选择中比较简洁的方法。有关更多详细信息,请参阅issue1567_comment。
Tox¶
Tox是一个通用的virtualenv管理和测试命令行工具。Tox可以与Factors和Molecule结合使用,以执行场景测试。
测试针对多个Ansible版本的该角色。
[tox]
minversion = 1.8
envlist = py{27}-ansible{20,21,22}
skipsdist = true
[testenv]
passenv = *
deps =
-r requirements.txt
ansible20: ansible==2.0.2.0
ansible21: ansible==2.1.2.0
ansible22: ansible==2.2.0.0
commands =
molecule test
要查看因子生成的tox环境,请运行`tox -l`。
如果使用Tox(3.7版及更高版本)的--parallel功能,则必须通过为每个环境设置`MOLECULE_EPHEMERAL_DIRECTORY`环境变量来使Molecule了解并行测试。此外,我们导出`TOX_ENVNAME`环境变量,它是我们的tox环境的名称。
[tox]
minversion = 3.7
envlist = py{36}_ansible{23,24}
skipsdist = true
[testenv]
deps =
-rrequirements.txt
ansible23: ansible==2.3
ansible24: ansible==2.4
commands =
molecule test
setenv =
TOX_ENVNAME={envname}
MOLECULE_EPHEMERAL_DIRECTORY=/tmp/{envname}
您还必须在`molecule.yml`配置文件中每个平台的名称中包含`TOX_ENVNAME`变量。这样,它们的名称就不会产生任何冲突。