5

我在研究 inspec 以及如何在 ansible 中使用它时拿起了分子。我发现分子很酷并采用了它。我想以两种方式使用它。

1- When developing a role or playbook
2- After a particular playbook have been run on production.

在第 1点:我在 stackoverflow 上发现了这个非常有用的问题/回复,这帮助我塑造了我的想法。我将角色 kafka 的变量文件放在 group_vars/all 下,如 stackoverflow 帖子中所建议的那样

- kafka
- - molecule
- - - default
- - - - molecule.yml
- - - - playbook.yml
- - - - ...
- - - - group_vars
- - - - - all.yml
- - - - tests
- - - - - test_default.py
- - tasks
- - - main.yml
- - ....

test_default.py

import os

import testinfra.utils.ansible_runner
import pytest


testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


@pytest.fixture()
def AnsibleVars(host):
    all_vars = host.ansible.get_variables()
    return all_vars


def test_hosts_file(host):
    f = host.file('/etc/hosts')

    assert f.exists
    assert f.user == 'root'
    assert f.group == 'root'


def test_downloaded_binary(host, AnsibleVars):
    # arch = host.file(AnsibleVars['kafka_archive_temp'])
    result = host.ansible('debug','var=kafka_archive_temp')
    arch = host.file(result['kafka_archive_temp'])
    assert arch.exists
    assert arch.is_file

def test_installation_directory(host,AnsibleVars):
    # dir = host.file(AnsibleVars['kafka_final_path'])
    result = host.ansible('debug','var=kafka_final_path')
    dir = host.file(result['kafka_final_path'])
    assert dir.exists
    assert dir.is_directory
    assert dir.user == AnsibleVars['kafka_user_on_os']
    assert dir.group == AnsibleVars['kafka_group_on_os']


def test_user_created(host,AnsibleVars):
    user = host.user(AnsibleVars['kafka_user_on_os'])

    assert user.name == AnsibleVars['kafka_user_on_os']
    assert user.group == AnsibleVars['kafka_group_on_os']

group_vars/all.yml

kafka_version: "2.2.1"
kafka_file_name: "kafka_2.12-{{ kafka_version }}.tgz"
kafka_user_on_os: kafka
kafka_group_on_os: kafka
kafka_zookeeper_service: zookeeper
kafka_service: kafka
kafka_log_folder: /var/log/kafka
kafka_zookeeper_port: 2181
kafka_archive_temp: "/tmp/{{ kafka_file_name }}"
kafka_final_path: "/usr/local/kafka/{{ kafka_version }}"
kafka_get_binaries_details:
  - {
    dest: "{{ kafka_archive_temp }}",
    url: "http://www-us.apache.org/dist/kafka/2.2.1/kafka_2.12-2.2.1.tgz"
  }
  ....

分子验证

molecule verify
--> Validating schema /Users/joseph/Engineering/configuration-management-ansible/roles/kafka/molecule/default/molecule.yml.
Validation completed successfully.
--> Test matrix

└── default
    └── verify

--> Scenario: 'default'
--> Action: 'verify'
--> Executing Testinfra tests found in /Users/joseph/Engineering/configuration-management-ansible/roles/kafka/molecule/default/tests/...
============================= test session starts ==============================
platform darwin -- Python 3.7.4, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: /Users/joseph/Engineering/configuration-management-ansible/roles/kafka/molecule/default
plugins: testinfra-3.1.0
collected 8 items

tests/test_default.py ........                                             [100%]

============================== 8 passed in 18.34s  ==============================

验证程序成功完成。但是,该方法host.ansible.get_variables()无法解析另一个变量中的变量,例如 : kafka_final_path: "/usr/local/kafka/{{ kafka_version }}"

我最终使用了以下内容:

 result = host.ansible('debug','var=kafka_final_path')
 dir = host.file(result['kafka_final_path'])

获取 kafka_final_path 的值。

问题 1.1:看看在使用所有需要的插值检索变量的变量之前如何需要进行一些操作,我想知道有没有更好的方法来编写这些测试?

问题 2.1:在第 2 点上,我想创建一个不同的测试场景,例如 AWS 上的 EC2。在这些剧本上,我使用传递给 ansible-playbook 的外部变量文件,因为它们具有更高的优先级。我想知道在这种情况下如何从 testinfra 中的外部 vars_files 访问这些变量?

4

0 回答 0