LoginSignup
1
0

Ansible Molecule バージョン間の違いやハマりやすい部分について初心者が知っておいた方が良い事

Last updated at Posted at 2023-09-21

概要

AnsibleやAnsible Molecule初心者ですが、Web上でドキュメントを探索していて色々と混乱することが多かったので備忘録として残しておきます。

Ansible Moleculeはこの記事を記載した時点でバージョン6系ですが、昔のバージョンと比較して必要な設定ファイルが変わっていたり、デフォルト値が変わっていたり、特定の機能がなくなっていたりして、Molecule周りはドキュメントが多かったのですが最新の状況と比較すると変わっていることも多く混乱することが多かったです。

そこで、Moleculeを調査する際に押さえておいた方が良い知識について記載しておきます。

Molecule自体は素晴らしいツールの予感がしております。

バージョンによる違い (Breaking Changes系)

とりあえずメジャーバージョンだけチェックして主観で必要そうな部分を抜粋しています。

v2とv3の違い

https://github.com/ansible/molecule/releases/tag/3.0.0
https://github.com/ssbarnea/molecule/blob/f83063a7ae70a8539a2c9fbfc7ec88295d8cde1c/CHANGELOG.rst

以下のPRにチェックリストがあるようです
https://github.com/ansible/molecule/issues/2560

下記は押さえておくのが良いでしょう

  • コアからAzure, EC2, DigitalOcean, GCE, HetznerCloud, Linode, LXD, OpenStack, Vagrantを削除しました。

  • molecule.yamlから「lint:」セクションの記法が改善された

  • testinfra, lintersはpipで明示的にインストールする必要がある

  • playbook.ymlはconverge.ymlに名称変更となった。playbook.ymlはdeprecate。

  • verifierの1つであるgossが削除された。

  • verifierのデフォルトがtestinfraからansibleへと変更になった。testinfraがデフォルトでインストールされないようになった。

  • molecule.yamlからscenario.nameの機能が削除された

  • Dockerfileテンプレートがmolecule中に組み込まれる様になった

  • デフォルトのtestやlintシーケンスの際に「lint」よりも前に「dependency」が実行されるようになった

下記も参考にさせていただきました。
https://qiita.com/answer_d/items/78b047eb5708dda1375d

v3とv4の違い

  • python3.6とpython3.7のサポートをやめた

v4とv5の違い

  • 「molecule lint」コマンドが削除された
  • python3.8のサポートをやめた

v5とv6の違い

yamlファイルについて

以下の4つは「molecule init scenario」のコマンドで生成した際にもデフォルトで生成されます。

  • molecule.yml
  • create.yml
  • converge.yml
  • destroy.yml

記事を探索していると上記とは異なる以下のファイル名を見かける場合があるかもしれません。これらについての機能も予め押さえておくのが良いでしょう。

  • verify.yml

verifier.nameにansibleが指定された場合に「molecule test」を実行すると動的に呼ばれます。

verifier:
  name: ansible

下記にverify.ymlの例を示しておきます。

$ cat molecule/helloworld/verify.yml 
---
- name: Verify
  hosts: all
  gather_facts: false
  tasks:
  - name: Get Apache service status
    command: "service apache2 status"
   register: apache_status
    
  - name: Check if Apache is running
    assert:
      that: apache_status.stdout == " * apache2 is running"
      fail_msg: "Apache is not running"
      success_msg: "Apache is running"
  • prepare.yml
    prepareフェーズ時に呼ばれます。

下記のprepare.ymlを新規作成します。

$ cat molecule/helloworld/prepare.yml
---
- name: Prepare
  hosts: all
  gather_facts: false
  tasks:
    - name: Task is running from prepare.yml
      ansible.builtin.debug:
        msg: "This is a task from prepare.yml."

実行してprepare.ymlが呼ばれることを確認します。

$ molecule prepare --scenario-name helloworld 
WARNING  The scenario config file ('/home/tsuyoshi/test/azarashi/utils/roles/azarashi.utils/molecule/helloworld/molecule.yml') has been modified since the scenario was created. If recent changes are important, reset the scenario with 'molecule destroy' to clean up created items or 'molecule reset' to clear current configuration.
INFO     helloworld scenario test matrix: prepare
INFO     Performing prerun with role_name_check=0...
INFO     Running from /home/tsuyoshi/test/azarashi/utils/roles/azarashi.utils : ansible-galaxy collection install -vvv --force ../..
INFO     Running helloworld > prepare

PLAY [Prepare] *****************************************************************

TASK [Task is running from prepare.yml] ****************************************
ok: [molecule-ubuntu] => {
    "msg": "This is a task from prepare.yml."
}

PLAY RECAP *********************************************************************
molecule-ubuntu            : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  • playbook.yml
    このファイルはv3でconverge.ymlにdeprecateになりました。

lintセクション

v5で削除されたlintセクションの機能はどこにいってしまったのでしょうか?

関連PRの下記をみると、PRの参照元では「.github/workflows/ansible-lint.yml」に役割を変更してるといったケースもあるようです。
独自でansible-lint, yamllint, flake8などのコマンドを動かすように変更していると思われます。
https://github.com/ansible/molecule/pull/3802

実行フェーズの確認

「molecule test」、「molecule converge」、「molecule check」など様々なmoleculeのサブオプションがありますが(詳細はmolecule --helpで確認可能)、実行されるフェーズや順番については次のコマンドで確認できます。
バージョン間での実行順の違いもあったりするようなので、覚えておくと役に立つと思います。

$ molecule matrix --scenario-name helloworld test
INFO     Test matrix
---                                                                                                                                                                                         
helloworld:
  - dependency
  - cleanup
  - destroy
  - syntax
  - create
  - prepare   
  - converge
  - idempotence
  - side_effect
  - verify
  - cleanup
  - destroy

Moleculeを手元で動かしたい

自分の記事ですが、Molecule 6系で手元で動かしたい場合の参考になるかもしれません
https://qiita.com/tsuyopon/items/533b3fa356fb7458e5fb

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