Python 3.x testinfra-未正确解释可解释变量

Python 3.x testinfra-未正确解释可解释变量,python-3.x,ansible,testinfra,Python 3.x,Ansible,Testinfra,用,我用 get_variables()方法,但testinfra似乎无法计算我的Ansible变量 让我们描述一下我的poc ~/tmp/ansible-testinfra ├── inventories │   ├── group_vars │   │   └── all.yml │   └── inventory.ini └── test_simple.py 我定义了两个变量: #库存/集团变量/all.yml --- 一:“价值一” 二:“{1}}和值二” 我的inventory.i

用,我用
get_variables()
方法,但testinfra似乎无法计算我的Ansible变量

让我们描述一下我的poc

~/tmp/ansible-testinfra
├── inventories
│   ├── group_vars
│   │   └── all.yml
│   └── inventory.ini
└── test_simple.py
我定义了两个变量:

#库存/集团变量/all.yml
---
一:“价值一”
二:“{1}}和值二”
我的
inventory.ini
是一个简单的本地主机:

# inventories/inventory

[all]
localhost
我创建了一个非常简单的测试,如下所示:

#test_simple.py
导入pprint
def测试打印变量(主机):
pprint.pprint(host.ansible.get_variables())
def测试变量(主机):
my_vars=host.ansible.get_variables()
断言my_vars['two']=='value one和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
中使用时不会被解释

那么,这有可能做到吗?如何做到


感谢您的反馈!:)

您正在测试库存。结果是正确的

shell>ansible inventory-i inventory.ini--graph--vars
@全部:
|--@未分组:
||--localhost
|| |--{one=value one}
|| |--{two={{one}}和值two}
|--{one=value one}
|--{two={one}和值two}
引述自:

惰性评估:通常,Ansible会在最后一秒评估playbook内容中的任何变量,这意味着如果您定义了一个数据结构,那么该数据结构本身就可以定义其中的变量值,并且一切都会按照您的预期“正常工作”。这也意味着变量字符串可以在这些字符串中包含其他变量

要评估剧本内容中的变量,请编写一个,例如

shell>cat剧本
-主机:本地主机
任务:
-调试:
一个
-调试:
变量:两个
给予

shell>ansible playbook playbook.yml-i inventory.ini
...
任务[调试]************************************************************
确定:[本地主机]=>
一:价值一
任务[调试]************************************************************
确定:[本地主机]=>
二:价值一和价值二

作为旁注,请参见。在Ansible中,变量优先级相当复杂,例如

shell>ansible playbook playbook.yml-i inventory.ini-e one=X
...
任务[调试]************************************************************
确定:[本地主机]=>
一:X
任务[调试]************************************************************
确定:[本地主机]=>
2:X和值2

因此,除了模块调试之外,没有其他方法可以做到这一点:(,我很失望。感谢您的回答。不仅仅是调试。任何评估都可以。请确保您理解。