持续集成

Molecule输出将使用 ANSI 如果stdout是交互式TTY和 TERM 价值观似乎支持了这一点。你可以定义 PY_COLORS=1 强迫使用 ANSI 颜色,对于某些CI系统可以很方便地使用。

GitHub操作

GitHub Actions 运行CI管道,就像其他管道一样,它内置在GitHub中。

将代码库克隆为 molecule_demo ,然后运行 molecule test 在ubuntu中。

---
name: Molecule Test
on: [push, pull_request]
jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.6, 3.7]

    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

如果您需要访问私有存储库中的需求, create a token 使用所需的权限,然后定义 GIT_CREDENTIALS 您的存储库的机密,其值如下 https://username:token@github.com/ ,最后添加以下步骤 Test with molecule

- name: Setup git credentials
  uses: fusion-engineering/setup-git-credentials@v2
  with:
    credentials: ${{secrets.GIT_CREDENTIALS}}

Travis CI

Travis 是一个CI平台,可用于测试Ansible角色。

A .travis.yml 使用Docker驱动程序测试名为foo1的角色。

---
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

A .travis.yml 使用 Tox 如下所述。

---
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中使用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[docker]
  - ansible --version
  - molecule --version

molecule:
  stage: test
  script:
    - cd roles/testrole && molecule test

GitLab Runner用于运行作业并将结果发送回GitLab。通过标记运行程序可以处理的作业类型,可以确保共享的运行程序只运行它们准备运行的作业。

下面是在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.yml 存储库根文件夹中的文件。如果您打算使用,则在自托管运行器上的Azure中运行CI/CD有许多前提条件 UsePythonVersion 任务。有关这方面的详细信息,请参阅 Use Python Version Task 文档。

---
trigger:
- master

pool:
  vmImage: ubuntu-16.04

steps:

- checkout: git://project-name/role-name
  path: role-name

- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.8'

- script: python -m pip install "molecule[lint]" "python-vagrant" "molecule-vagrant" "ansible"
  displayName: Install dependencies

- script: python -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

虽然管道最初将代码作为管道任务的一部分签出,但默认情况下,它会将代码签出到名为 s$(Agent.BuildDirectory) 。如果签出另一个存储库, s 替换为该签出中提供的路径。如果您签出多个角色(例如,Azure组织内的一些私有角色),则 s 结构,因此使用 cd $(Agent.BuildDirectory)/role-name 这可以确保您位于正确的目录中,而不管格式如何。检查 Azure Build Variables 有关这些方面的更详细信息,请参阅文档。

这个 export PATH is required to ensure you can use the 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
          pip3.6 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 使用工具集图像。

pipeline {
  agent {
    docker {
      image 'quay.io/ansible/toolset'
      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 用一个 Multibranch Pipeline 或A GitHub Organisation -正如Blue Ocean所使用的,场景中的角色名收敛.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 =
    -rrequirements.txt
    ansible20: ansible==2.0.2.0
    ansible21: ansible==2.1.2.0
    ansible22: ansible==2.2.0.0
commands =
    molecule test

要查看因子生成的毒性环境,请运行 tox -l .

如果使用 --parallel functionality 对于Tox(3.7版以后的版本),必须通过设置一个 MOLECULE_EPHEMERAL_DIRECTORY 每个环境的环境变量。此外,我们还出口 TOX_ENVNAME 环境变量,它是我们的毒物的名字。

[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}

您还必须包括 TOX_ENVNAME 中每个平台的名称中的变量 molecule.yml 配置文件。这样,他们的名字就不会产生任何冲突。

---
dependency:
  name: galaxy
driver:
  name: docker
platforms:
  - name: instance1-$TOX_ENVNAME
    image: mariadb
  - name: instance2-$TOX_ENVNAME
    image: retr0h/centos7-systemd-ansible:latest
    privileged: True
    command: /usr/sbin/init
provisioner:
  name: ansible
verifier:
  name: testinfra