在分子测试TestInfra中访问Ansible变量

时间:2019-09-06 11:18:17

标签: ansible molecule testinfra

我在研究inspec以及如何在ansible中使用它的同时拾起了分子。我发现分子非常酷,并采用了它。我想以两种方式使用它。

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

关于数字1 :我发现这个非常有用的question/ressponse on 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访问这些变量?

0 个答案:

没有答案