2017-10-17 3 views
1

TestInfraをテスト用に使用することができます。すべてはzabbix_agent_versionがgroup_varsからAnsible変数であるテストtestinfraで使用可能な変数を使用する

test.py

import pytest 
def test_zabbix_agent_package(host): 
    package = host.package("zabbix-agent") 
    assert package.is_installed 
    package_version = host.ansible("debug", "msg={{ zabbix_agent_version }}")["msg"] 
    (...) 

を実行している間Ansible自体を使用することを除いて罰金行きます。これは、テスト

pytest --connection=ansible --ansible-inventory=inventory --hosts=$hosts -v test.py 
私が手

ansible.cfg

[defaults] 
timeout = 10 
host_key_checking = False 
library=library/ 
retry_files_enabled = False 
roles_path=roles/ 
pipelining=true 
ConnectTimeout=60 
remote_user=deploy 
private_key_file=/opt/jenkins/.ssh/deploy 

出力は

self = <ansible>, module_name = 'debug', module_args = 'msg={{ zabbix_agent_version }}', check = True, kwargs = {} 
result = {'failed': True, 'msg': "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'zabbix_agent_version' is undefined"} 

    def __call__(self, module_name, module_args=None, check=True, **kwargs): 
     if not self._host.backend.HAS_RUN_ANSIBLE: 
      raise RuntimeError((
       "Ansible module is only available with ansible " 
       "connection backend")) 
     result = self._host.backend.run_ansible(
      module_name, module_args, check=check, **kwargs) 
     if result.get("failed", False) is True: 
>   raise AnsibleException(result) 
E   AnsibleException: Unexpected error: {'failed': True, 
E   'msg': u"the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'zabbix_agent_version' is undefined"} 

/usr/lib/python2.7/site-packages/testinfra/modules/ansible.py:70: AnsibleException 

任意のアイデア理由で実行この脚本

- hosts: all 
    become: true 
    tasks: 
    - name: debug 
    debug: msg={{ zabbix_agent_version }} 

コマンドを実行することによって得ることができます不可能な場合、この変数は表示されませんtestinfraのAnsibleモジュールを走らせながら走っている間に走り回ることができますか? zabbix_agent_versiongroup_varsを使用して変数セットがある場合は、host.ansible.get_variables()を使用してアクセスするのではなくdebugタスクを実行する必要があるかのように

+0

Ansibleバージョン2.2.1.0 Pythonのバージョン2.7.5 Testinfraバージョン手動Ansibleを実行しているとき 'zabbix_agent_version'が定義されている1.6.4 Pytestバージョン3.1.3 – FRC

+0

?これはリモートホスト上のカスタムファクトから来るのでしょうか?これはローカル変数ファイルに設定されていますか? – larsks

+0

zabbix_agent_versionはgroup_varsで定義されています。この事実をチェックするプレイブックを実行すると(デバッグ:msg = {{zabbix_agent_version}})、これはAnsibleが取得できます。 – FRC

答えて

1

は、それはそうです。いずれにしても、どちらもうまくいくはずです。私が持っている場合は、私の現在のディレクトリに:

myvar: value 

をそしてtest_myvar.pyに私が持っている::group_vars/all.yml

test_myvar.py 
group_vars/ 
    all.yml 

そして、私は持っている

def test_myvar_using_get_variables(host): 
    all_variables = host.ansible.get_variables() 
    assert 'myvar' in all_variables 
    assert all_variables['myvar'] == 'myvalue' 


def test_myvar_using_debug_var(host): 
    result = host.ansible("debug", "var=myvar") 
    assert 'myvar' in result 
    assert result['myvar'] == 'myvalue' 


def test_myvar_using_debug_msg(host): 
    result = host.ansible("debug", "msg={{ myvar }}") 
    assert 'msg' in result 
    assert result['msg'] == 'myvalue' 

はその後、すべてのテストに合格:

$ py.test --connection=ansible --ansible-inventory=hosts -v 
test_myvar.py 
============================= test session starts ============================== 
platform linux2 -- Python 2.7.13, pytest-3.2.3, py-1.4.34, pluggy-0.4.0 -- /home/lars/env/common/bin/python2 
cachedir: .cache 
rootdir: /home/lars/tmp/testinfra, inifile: 
plugins: testinfra-1.8.1.dev2 
collected 3 items                

test_myvar.py::test_myvar_using_get_variables[ansible://localhost] PASSED 
test_myvar.py::test_myvar_using_debug_var[ansible://localhost] PASSED 
test_myvar.py::test_myvar_using_debug_msg[ansible://localhost] PASSED 

=========================== 3 passed in 1.77 seconds =========================== 

ファイルのレイアウト(特に、group_varsディレクトリの場所)がここに示したレイアウトと一致することを確認できますか?

+0

はい、私はあなたが提示したのと同じレイアウトを持っています。それ以上のテストをしている間は、Ansible(ansible_ssh_hostのような)テストインフラが問題なく見つけることができますが、group_varsで設定されているものを試してみると、デバッグを実行しているときには、それが見えます。 – FRC

0

私は数日間この回答を追跡しました。ここに私のために最終的に働いたものがあります。基本的には、testinfraのAnsibleモジュールを使用して、Ansibleのinclude_vars関数にアクセスしています。このソリューションは、https://github.com/metacloud/molecule/issues/151

から部分的に撮影された

def test_something(host, AnsibleVars): 

私は私には、しようとしていた興味深い問題があった:私のテストで続いて

import pytest 

@pytest.fixture() 
def AnsibleVars(host): 
ansible_vars = host.ansible(
    "include_vars", "file=./group_vars/all/vars.yml") 
return ansible_vars["ansible_facts"] 

は、私は、パラメータとしての機能が含まれてplaybook.ymlファイルをインクルードするときに "私は辞書/ハッシュとして保存しなければなりません"というエラーを受け取っていました。変数をgroup_vars/all/vars.ymlファイルに分けて、そのエラーを解決しました。

関連する問題