【发布时间】:2021-03-19 21:18:29
【问题描述】:
testinfra,我正在使用
get_variables() 来自 testinfra.modules.ansible.Ansible 的方法,但似乎 testinfra 无法评估我的 Ansible 变量。
让我们描述一下我的 poc。
~/tmp/ansible-testinfra
├── inventories
│ ├── group_vars
│ │ └── all.yml
│ └── inventory.ini
└── test_simple.py
我定义了 2 个变量:
# inventories/group_vars/all.yml
---
one: "value one"
two: "{{ one }} and value two"
我的inventory.ini 是一个简单的本地主机:
# inventories/inventory
[all]
localhost
我创建了一个非常简单的测试,如下所示:
# test_simple.py
import pprint
def test_print_variables(host):
pprint.pprint(host.ansible.get_variables())
def test_variables(host):
my_vars = host.ansible.get_variables()
assert my_vars['two'] == 'value one and value two'
当我运行 pytest 时,这是我的 sdtout:
~/tmp/ansible-testinfra$ pytest --hosts "ansible://all?ansible_inventory=inventories/inventory.ini" -s --tb=no
============================================================================ test session starts ============================================================================
platform linux -- Python 3.6.9, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/olhoa/tmp/ansible-testinfra
plugins: testinfra-6.1.0
collected 2 items
test_simple.py {'group_names': ['ungrouped'],
'groups': {'all': ['localhost'], 'ungrouped': ['localhost']},
'inventory_hostname': 'localhost',
'one': 'value one',
'two': '{{ one }} and value two'}
.F
========================================================================== short test summary info ==========================================================================
FAILED test_simple.py::test_variables[ansible://localhost] - AssertionError: assert '{{ one }} and value two' == 'value one and value two'
如您所见,变量one 在变量two 中使用时不会被解释。
那么,这可能吗?如何做到这一点?
感谢您的反馈! :)
【问题讨论】:
标签: python-3.x ansible testinfra