如何从 ansible 库存中获取特定组并将其用于剧本?
How to grab specific group from ansible inventory and use it in playbook?
我需要从我的清单文件中获取特定的子组并在 ansible playbook 中正确使用它。
all:
children:
development:
children:
ntp_servers:
hosts:
ntp01:
ntp02:
services:
children:
chatbot:
children:
app:
hosts:
chatbot-app01:
chatbot-app02:
db:
hosts:
chatbot-db01:
chatbot-db02:
dice:
children:
app:
hosts:
dice-app01:
dice-app02:
db:
hosts:
dice-db01:
dice-db02:
dice-db03:
redis:
hosts:
dice-redis01:
dice-redis02:
例如,我想抓取聊天机器人组的应用子组。
谁能举例说明如何引用 chatbot.app 组?
我是这样使用的,但这似乎是错误的说法。
- hosts: chatbot:app
roles:
- chatbot
不可能直接实现您想要实现的目标。您当前遇到的问题是主机组名称 app
和 db
重复,这会影响您的结果。
首先,您需要确保使用唯一的组名。例如:
all:
children:
development:
children:
ntp_servers:
hosts:
ntp01
ntp02
services:
children:
chatbot:
children:
chatbot_app:
hosts:
chatbot-app01:
chatbot-app02:
chatbot_db:
hosts:
chatbot-db01:
chatbot-db02:
dice:
children:
dice_app:
hosts:
dice-app01:
dice-app02:
dice_db:
hosts:
dice-db01:
dice-db02:
dice-db03:
dice_redis:
hosts:
dice-redis01:
dice-redis02:
然后您可以考虑引用 parent/child 个您认为合适的组。使用问题中的示例,您可以执行以下操作:
- hosts: chatbot_app
roles:
- chatbot
您的所有组(开发、ntp_servers、服务、聊天机器人、骰子等)都可以这样做
我需要从我的清单文件中获取特定的子组并在 ansible playbook 中正确使用它。
all:
children:
development:
children:
ntp_servers:
hosts:
ntp01:
ntp02:
services:
children:
chatbot:
children:
app:
hosts:
chatbot-app01:
chatbot-app02:
db:
hosts:
chatbot-db01:
chatbot-db02:
dice:
children:
app:
hosts:
dice-app01:
dice-app02:
db:
hosts:
dice-db01:
dice-db02:
dice-db03:
redis:
hosts:
dice-redis01:
dice-redis02:
例如,我想抓取聊天机器人组的应用子组。 谁能举例说明如何引用 chatbot.app 组?
我是这样使用的,但这似乎是错误的说法。
- hosts: chatbot:app
roles:
- chatbot
不可能直接实现您想要实现的目标。您当前遇到的问题是主机组名称 app
和 db
重复,这会影响您的结果。
首先,您需要确保使用唯一的组名。例如:
all:
children:
development:
children:
ntp_servers:
hosts:
ntp01
ntp02
services:
children:
chatbot:
children:
chatbot_app:
hosts:
chatbot-app01:
chatbot-app02:
chatbot_db:
hosts:
chatbot-db01:
chatbot-db02:
dice:
children:
dice_app:
hosts:
dice-app01:
dice-app02:
dice_db:
hosts:
dice-db01:
dice-db02:
dice-db03:
dice_redis:
hosts:
dice-redis01:
dice-redis02:
然后您可以考虑引用 parent/child 个您认为合适的组。使用问题中的示例,您可以执行以下操作:
- hosts: chatbot_app
roles:
- chatbot
您的所有组(开发、ntp_servers、服务、聊天机器人、骰子等)都可以这样做