Ansible vars inventory

问题描述 投票:0回答:1

我正在学习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"

而在这两种情况下,我都得到了一个空白的响应。我不知道为什么。

如果有人能解释为什么或告诉我在哪里读它将是巨大的。谢谢大家!我正在学习Ansible。

server ansible ansible-inventory
1个回答
1
投票

双括号 {{ }} 需要评估这个变量。试试这个

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_countdown 已定义的 test_01 只是。

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

[pruebascomandos:vars]
example=true
© www.soinside.com 2019 - 2024. All rights reserved.