【问题标题】:testinfra - ansible variables not interpreted correctlytestinfra - ansible 变量未正确解释
【发布时间】: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


    【解决方案1】:

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

    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:

    延迟评估:一般来说,Ansible 在可能的最后一秒评估剧本内容中的任何变量,这意味着如果您定义一个数据结构,该数据结构本身可以在其中定义变量值,并且一切都“正常工作” ” 如你所料。这也意味着变量字符串可以在这些字符串中包含其他变量。

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

    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
    

    【讨论】:

    • 所以,除了模块调试之外没有其他方法可以做到这一点:(,我很失望。谢谢你的回答。
    • 不仅仅是调试。任何评估都可以。确保你理解lazy evaluation
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 2018-01-18
    • 2014-11-07
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多