molecule 需要我们额外编写对应ansible role的测试代码,通过一个例子来说明具体的使用方法 创建一个空的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 |
$ molecule init role -r ansible-apache -d docker --> Initializing new role ansible-apache... Initialized role in /Users/jeremyzhang/git/hack/molecule_example/ansible-apache successfully. $tree ansible-apache ansible-apache ├── 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 │ └── test_default.pyc ├── tasks │ └── main.yml └── vars └── main.yml 8 directories, 12 files |
我们来编写具体的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 |
cat tasks/main.yml --- # tasks file for ansible-apache - name: "Ensure required packages are present" yum: name: "{{ pkg_list }}" state: present - name: "Ensure latest index.html is present" template: src: index.html.j2 dest: /var/www/html/index.html - name: "Ensure httpd service is started and enabled" service: name: "{{ item }}" state: started enabled: true with_items: "{{ svc_list }}" - name: "Whitelist http in firewalld" firewalld: service: http state: enabled permanent: true immediate: true |
这个playbook book主要有4……