因为工作中要用到molecule作为ansible role的测试工具,之前没有使用过,正好整理一下相关的资料
首先,什么是molecule? molecule是一个专为ansible role 编写及测试的一个便捷工具
,它可以帮我们生成必要的代码,必要的文件
安装:以mac为例:
1 |
brew install molecule |
molecule底层还是调用的ansible Galaxy来帮我们生产代码,我们看一下生成一个role的基本框架的代码:
1 2 3 |
$ molecule init role -r my-new-role --> Initializing new role my-new-role... Initialized role in /Users/abc/git/hack/molecule_example/my-new-role successfully. |
然后我们看一下molecule为我们生成了哪些文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
tree . . └── my-new-role ├── README.md ├── defaults │ └── main.yml ├── handlers │ └── main.yml ├── meta │ └── main.yml ├── molecule │ └── default │ ├── Dockerfile.j2 │ ├── INSTALL.rst │ ├── molecule.yml │ ├── playbook.yml │ └── tests │ └── test_default.py ├── tasks │ └── main.yml └── vars └── main.yml 9 directories, 11 files |
除了最基本的tasks,vars,handlers,defaults等文件夹(貌似没办我们生成files文件夹,我现在开始不怎么相信这个框架了,虽然我本来就知道这个工具的缺点,没想到连基本的文件夹都能漏掉),还额外帮我们生成了一个文件夹叫molecule,并切里边有很多文件,Dockerfile.j2,molecule.yml等,我们看一下最主要的文件:
molecule.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
--- dependency: name: galaxy driver: name: docker lint: name: yamllint platforms: - name: instance image: centos:7 provisioner: name: ansible lint: name: ansible-lint verifier: name: testinfra lint: name: flake8 |
dependency: 默认情况下molecule 用的是ansible galaxy 来解决role之间的依赖问题
driver:默认情况下molecule使用的是docker来作为测试的平台(就是我们会把host 设定为一个container然后执行我们的playbook然后测试效果),molecule也支持其他的例如ec2,gce,linode,azure(其实底层都是ansible的module)
platforms:这个地方我们指定我们需要测试的具体版本,此处可以指定多个版本,多个系统
provisioner:不可修改,仅仅支持ansible
verifier:molecule使用testinfa来做所有的特殊状态的测试
例子
在执行命令以前,我们需要确保我们本地机器上的docker是运行状态的
1 2 3 4 5 6 7 8 9 |
$docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:4df8ca8a7e309c256d60d7971ea14c27672fc0d10c5f303856d7bc48f8cc17ff Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. |
然后我们就可以开始我们的命令了!
首先,我们创建一个docker container来作为我们的的测试环境,注意执行命令所在路径必须是role的根目录(pip install molecule 如果错误提示molecule 模块找不到的话seems to be invalid: No module named ‘molecule’)
(pip install docker 如果报错:ERROR: Missing Docker driver dependency)
1 |
molecule creat |
这一步的操作就是按照上边配置的文件创建一个container,我们可以通过两种方式查看
1 2 3 4 5 6 7 8 9 10 |
$docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES da733c060640 molecule_local/centos:7 "bash -c 'while true…" 38 seconds ago Up 37 seconds instance $molecule list --> Validating schema /Users/jeremyzhang/git/hack/molecule_example/my-new-role/molecule/default/molecule.yml. Validation completed successfully. Instance Name Driver Name Provisioner Name Scenario Name Created Converged --------------- ------------- ------------------ --------------- --------- ----------- instance docker ansible default true false |
我们来添加一个简单的命令到我们的role中,在文件 task/main.yml
1 2 3 4 5 6 |
cat tasks/main.yml --- # tasks file for my-new-role - name: Molecule Hello World! debug: msg: Hello, World! |
然后我们可以通过命令来测试刚刚我们创建的只有一个task的role
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
$molecule converge --> Validating schema /Users/jeremyzhang/git/hack/molecule_example/my-new-role/molecule/default/molecule.yml. Validation completed successfully. --> Test matrix └── default ├── dependency ├── create ├── prepare └── converge --> Scenario: 'default' --> Action: 'dependency' Skipping, missing the requirements file. --> Scenario: 'default' --> Action: 'create' Skipping, instances already created. --> Scenario: 'default' --> Action: 'prepare' Skipping, prepare playbook not configured. --> Scenario: 'default' --> Action: 'converge' PLAY [Converge] **************************************************************** TASK [Gathering Facts] ********************************************************* ok: [instance] TASK [my-new-role : Molecule Hello World!] ************************************* ok: [instance] => { "msg": "Hello, World!" } PLAY RECAP ********************************************************************* instance : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
这样我们就完成了一个最基本的语法检查之类的测试例子
清理
1 |
molecule destroy |
以上内容均为简单的例子,实际情况中我们需要编写的内容远不止这些,如果我们创建了一个文件,我们要判断文件是否正常创建了,内容是否正常等等
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021