LoginSignup
11
6

More than 3 years have passed since last update.

Moleculeに入門してみたよ(v3差分確認編)

Last updated at Posted at 2020-03-21

Molecule v3がリリースされました

差分確認

Changelog

重要っぽい変更点

  • ドライバ:Azure, EC2, DigitalOcean, GCE, HetznerCloud, Linode, LXD, OpenStack, Vagrantが削除された
  • goss verifierが削除された
  • playbook.yml という名前は廃止して converge.yml
  • デフォルトのverifierがAnsibleになった
  • Testinfraはデフォルトでインストールされなくなった
  • lintの設定がリファクタされた
  • Dockerfileテンプレートがmolecule組み込みになった

v2との差分チェック

作業環境

品目 バージョン
OS macOS Catalina 10.15.3
シェル fish, version 3.1.0
Python 3.8.0
Ansible 2.9.5
Molecule 3.0.2

molecule initでロールの雛形を作成する

  • ロール置き場を作って移動
$ mkdir roles
$ cd roles
$ molecule init role apache_vhost
--> Initializing new role apache_vhost...
Initialized role in /Users/answer_d/repos/molecule_test/roles/apache_vhost successfully.
$ cd apache_vhost
  • こんなかんじのディレクトリが生成される
apache_vhost/
├── .travis.yml
├── .yamllint
├── README.md
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── molecule
│   └── default
│       ├── INSTALL.rst
│       ├── converge.yml
│       ├── molecule.yml
│       └── verify.yml
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml
  • v2と違うポイント
    • Travis CI用のファイルが生成される?!
      • はえ〜、時代はCI/CD
    • lintはデフォルトで動作しないようになりました
      • 使用したければmolecule.ymlでlintをenableしましょう
    • README.mdが生成されてる
    • Dockerfile.j2無い
      • Dockerfileはv3からMolecule組み込みになったらしい
      • シナリオのディレクトリに Dockerfile.j2 を用意するとそっちを見に行くっぽい

molecule.yml

---
dependency:
  name: galaxy
driver:
  name: docker
platforms:
  - name: instance
    image: docker.io/pycontribs/centos:7
    pre_build_image: true
provisioner:
  name: ansible
verifier:
  name: ansible
  • ちょっと変わってんね(lintが消えてるとか)

converge.yml

---
- name: Converge
  hosts: all
  tasks:
    - name: "Include apache_vhost"
      include_role:
        name: "apache_vhost"
  • rolesモジュールじゃなくてinclude_roleを使うようになってる

テスト実行してみる

molecule test
--> Test matrix

└── default
    ├── dependency
    ├── lint
    ├── cleanup
    ├── destroy
    ├── syntax
    ├── create
    ├── prepare
    ├── converge
    ├── idempotence
    ├── side_effect
    ├── verify
    ├── cleanup
    └── destroy
  • lintdependency が入れ替わってる、他はv2と一緒
    • 実はChangelogに書いてあった

lint

--> Action: 'lint'
--> Lint is disabled.
  • lintはデフォルトDisableに変更、Changelog通り

これ以降の処理はv2から変わらん!

ドライバがいろいろなくなったのどうするのか問題

  • ec2ドライバ作ろうとしても、v2通りやろうとするとec2ドライバ無いよって怒られる
$ molecule init scenario -d ec2
Usage: molecule init scenario [OPTIONS] [SCENARIO_NAME]
Try "molecule init scenario --help" for help.

Error: Invalid value for "--driver-name" / "-d": invalid choice: ec2. (choose from delegated, docker, podman)
  • ec2ドライバをインストールする
pip install molecule-ec2
  • で再実行したらできた
$ molecule init scenario -d ec2
--> Initializing new scenario default...
Initialized scenario in /Users/answer_d/mac_os_setup_playbook/molecule/default successfully.
$ pip search molecule
molecule (3.0.2)                           - Molecule aids in the development and testing of Ansible roles
  INSTALLED: 3.0.2 (latest)
pytest-molecule (1.2.5)                    - PyTest Molecule Plugin :: discover and run molecule tests
molecule-containers (0.1.dev0)             - Molecule Containers Driver :: run molecule tests on containers
molecule-openstack (0.1)                   - Molecule OpenStack Plugin :: run molecule tests on openstack
molecule-libvirt (0.0.2)                   - libvirt Molecule Plugin :: run molecule tests on libvirt
molecule-digitalocean (0.1)                - digitalocean Molecule Plugin :: run molecule tests on digitalocean
molecule-azure (0.1)                       - Azure Molecule Plugin :: run molecule tests on Azure
molecule-vagrant (0.2)                     - Vagrant Molecule Plugin :: run molecule tests using Vagrant
molecule-inspec (1.0)                      - Inspec Molecule Plugin :: run molecule tests with inspec as verifier
molecule-goss (1.0)                        - Goss Molecule Plugin :: run molecule tests with Goss as verifier
molecule-gce (0.1)                         - Molecule GCE Plugin :: run molecule tests on Google Cloud Engine
molecule-ec2 (0.2)                         - EC2 Molecule Plugin :: run molecule tests using AWS EC2
11
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
6