【发布时间】:2021-10-28 09:16:06
【问题描述】:
我需要从清单文件中检索 bob_server 的 IP。我不清楚在filter、lookup 和when 中使用什么组合?根据库存文件bob_server 和alice_server 名称可以更改,但app_type 不会更改。我的剧本逻辑显然是错误的,有人可以指导我在app_type = bob时获取IP地址的正确方法
我当前的库存文件:
---
all:
hosts:
children:
bob_server:
hosts: 10.192.2.6
vars:
app_type: bob
alice_server:
hosts: 10.192.2.53
vars:
app_type: alice
我的剧本
---
- hosts: localhost
name: Retrive data
tasks:
- name: Set Ambari IP
set_fact:
ambariIP: "{{ lookup('hosts', children) }}"
when: "hostvars[app_type] == 'bob'"
【问题讨论】:
-
您的库存文件无效。请参考documentation 并修复它。此外,您似乎混淆了组和主机的概念。这也在文档中进行了解释。一旦所有这些都解决了,您将不需要任何花哨的查找:使用
inventory_hostname或ansible_host(取决于您如何命名主机)将为您提供预期的数据。 -
是的,组和主机的概念令人困惑。但是,不,库存不是无效的。见下文,
ansible-inventory -i hosts-01 --list工作正常。我认为没有理由关闭。这是一个很好的例子,说明如何(不)使用(仍然有效)YAML inventory format。
标签: ansible ansible-2.x ansible-inventory ansible-facts