[英]Ansible playbook doesnt get values from inventory group_vars
I have got a simple playbook:我有一个简单的剧本:
- hosts: all
serial: 1
order: "{{ run_order }}"
gather_facts: false
tasks:
- ping:
register: resultt
My inventory tree:我的库存树:
.
├── group_vars
│ └── g1
│ └── values.yml
├── host_vars
└── inv
inv file: .inv 文件:
[g1:children]
m1
m2
[g2:children]
m3
m4
[m1]
192.168.0.60
[m2]
192.168.0.61
[m3]
192.168.0.62
[m4]
192.168.0.63
group_vars/g1/values.yml: group_vars/g1/values.yml:
---
run_order: 'reverse_sorted'
When I try to run playbook:当我尝试运行剧本时:
ansible-playbook -i inv-test/inv --limit='g1' my-playbook.yml
I get the error:我得到错误:
ERROR! The field 'order' has an invalid value, which includes an undefined variable. The error was: 'run_order' is undefined
The error appears to be in '/home/mk/throttle': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- hosts: all
^ here
Although the output from ansible-inventory -i inv --graph --vars
is correct虽然
ansible-inventory -i inv --graph --vars
的输出是正确的
Why ansible doesn't parse vars from inventory groups_vars?为什么ansible不从库存groups_vars解析vars? Ansible version 2.10.15
Ansible 版本 2.10.15
The problem is that you want to use a host variable in the scope of a play.问题是您想在播放范围内使用主机变量。 See Scoping variables .
请参阅范围变量。 It's possible if you create such a host variable first.
如果您先创建这样的主机变量,则可以。 Then you can use hostvars to reference the host variable.
然后您可以使用hostvars来引用主机变量。 For example, given the simplified inventory
例如,给定简化清单
shell> cat hosts
[g1]
host_01
host_02
and the group_vars和group_vars
shell> cat group_vars/g1.yml
run_order: 'reverse_sorted'
The playbook剧本
shell> cat pb.yml
- hosts: all
gather_facts: false
tasks:
- debug:
var: run_order
gives (abridged)给出(删节)
shell> ansible-playbook -i hosts pb.yml
ok: [host_01] =>
run_order: reverse_sorted
ok: [host_02] =>
run_order: reverse_sorted
But, the playbook below但是,下面的剧本
shell> cat pb.yml
- hosts: all
gather_facts: false
order: "{{ run_order }}"
tasks:
- debug:
var: run_order
fails because the variable run_order is not defined in the scope of the play失败是因为变量run_order没有定义在 play 的范围内
ERROR!
错误! The field 'order' has an invalid value, which includes an undefined variable.
字段“订单”的值无效,其中包括未定义的变量。 The error was: 'run_order' is undefined
错误是:“run_order”未定义
The (awkward) solution is to create the hostvars in the first play and use it in the second play. (尴尬的)解决方案是在第一次播放中创建主机变量并在第二次播放中使用它。 For example, the second play in the playbook below is executed in the reverse order
比如下面playbook中的第二个play是按相反的顺序执行的
shell> cat pb.yml
- hosts: all
gather_facts: false
tasks:
- debug:
var: run_order
- hosts: all
gather_facts: false
serial: 1
order: "{{ hostvars.host_01.run_order }}"
tasks:
- debug:
var: run_order
gives给
shell> ansible-playbook -i hosts pb.yml
PLAY [all] ***********************************************************************************
TASK [debug] *********************************************************************************
ok: [host_01] =>
run_order: reverse_sorted
ok: [host_02] =>
run_order: reverse_sorted
PLAY [all] ***********************************************************************************
TASK [debug] *********************************************************************************
ok: [host_02] =>
run_order: reverse_sorted
PLAY [all] ***********************************************************************************
TASK [debug] *********************************************************************************
ok: [host_01] =>
run_order: reverse_sorted
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.