testinfra - ansible 变量未正确解释
testinfra - ansible variables not interpreted correctly
对于 testinfra,我正在使用
来自 testinfra.modules.ansible.Ansible 的 get_variables()
方法,但似乎 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
中使用时未被解释。
那么,是否可以这样做以及如何做到这一点?
感谢您的反馈! :)
您正在测试库存。结果正确
shell> ansible-inventory -i inventory.ini --graph --vars
@all:
|--@ungrouped:
| |--localhost
| | |--{one = value one}
| | |--{two = {{ one }} and value two}
|--{one = value one}
|--{two = {{ one }} and value two}
引用自Glossary:
Lazy Evaluation: In general, Ansible evaluates any variables in playbook content at the last possible second, which means that if you define a data structure that data structure itself can define variable values within it, and everything “just works” as you would expect. This also means variable strings can include other variables inside of those strings.
要评估剧本内容中的变量,请写一个,例如
shell> cat playbook
- hosts: localhost
tasks:
- debug:
var: one
- debug:
var: two
给予
shell> ansible-playbook playbook.yml -i inventory.ini
...
TASK [debug] ************************************************************
ok: [localhost] =>
one: value one
TASK [debug] ************************************************************
ok: [localhost] =>
two: value one and value two
作为旁注,请参阅 Understanding variable precedence。在 Ansible 中,变量优先级相当复杂,例如
shell> ansible-playbook playbook.yml -i inventory.ini -e one=X
...
TASK [debug] ************************************************************
ok: [localhost] =>
one: X
TASK [debug] ************************************************************
ok: [localhost] =>
two: X and value two
对于 testinfra,我正在使用
来自 testinfra.modules.ansible.Ansible 的 get_variables()
方法,但似乎 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
中使用时未被解释。
那么,是否可以这样做以及如何做到这一点?
感谢您的反馈! :)
您正在测试库存。结果正确
shell> ansible-inventory -i inventory.ini --graph --vars
@all:
|--@ungrouped:
| |--localhost
| | |--{one = value one}
| | |--{two = {{ one }} and value two}
|--{one = value one}
|--{two = {{ one }} and value two}
引用自Glossary:
Lazy Evaluation: In general, Ansible evaluates any variables in playbook content at the last possible second, which means that if you define a data structure that data structure itself can define variable values within it, and everything “just works” as you would expect. This also means variable strings can include other variables inside of those strings.
要评估剧本内容中的变量,请写一个,例如
shell> cat playbook
- hosts: localhost
tasks:
- debug:
var: one
- debug:
var: two
给予
shell> ansible-playbook playbook.yml -i inventory.ini
...
TASK [debug] ************************************************************
ok: [localhost] =>
one: value one
TASK [debug] ************************************************************
ok: [localhost] =>
two: value one and value two
作为旁注,请参阅 Understanding variable precedence。在 Ansible 中,变量优先级相当复杂,例如
shell> ansible-playbook playbook.yml -i inventory.ini -e one=X
...
TASK [debug] ************************************************************
ok: [localhost] =>
one: X
TASK [debug] ************************************************************
ok: [localhost] =>
two: X and value two