Ansible 变量清单

Ansible vars inventory

我正在学习 Ansible。我正在关注官方文档:

但我有一个小问题。如何使用库存中的变量?

我尝试使用一些默认参数,例如 self_destruct_countdown.

[pruebascomandos]
MY-SERVER-IP self_destruct_countdown=60
OTHER-MY-SERVER-IP

并将变量应用于所有组。有自己的变量。

[pruebascomandos:vars]
example=true

但我的问题是,在这两种情况下,我都尝试使用以下方法检查变量:

$ ansible pruebascomandos -m shell -a "echo $self_destruct_countdown"

$ ansible pruebascomandos -m shell -a "echo $example"

在这两种情况下,我都得到了空白回复。我不知道为什么。

如果有人能解释原因或告诉我在哪里阅读它,那就太好了。谢谢大家!

需要

Double braces {{ }} 来评估变量。试试这个

shell> ansible pruebascomandos -i hosts -m shell -a "echo {{ example }}"
test_01 | CHANGED | rc=0 >>
true
test_02 | CHANGED | rc=0 >>
true

shell> ansible pruebascomandos -i hosts -m shell -a "echo {{ self_destruct_countdown }}"
test_02 | FAILED | rc=-1 >>
The task includes an option with an undefined variable. The error was: self_destruct_countdown is undefined
test_01 | CHANGED | rc=0 >>
60

主机 test_02 失败,因为已为 定义了变量 self_destruct_countdowntest_01

shell> cat hosts
[pruebascomandos]
test_01 self_destruct_countdown=60
test_02

[pruebascomandos:vars]
example=true