如何根据变量的值选择Ansible中的主机?
我想使用用户提供的答案在Ansible剧本中选择主机,例如以下伪代码:
---
- name: feed the machines
vars_prompt:
- name: food
prompt: "Which food do you have? b:Banana, g:grass, m:meat."
default: "b"
private: no
hosts: 'monkeys' if food=='b' else {'cows' if food=='g' else 'lions'}
我知道使用字典也是一种选择。
如何实施它?如果 s或词典有的答案,那将是很高兴的。
I want to use the answer provided by the user to select the hosts in an Ansible playbook, like the following pseudocode:
---
- name: feed the machines
vars_prompt:
- name: food
prompt: "Which food do you have? b:Banana, g:grass, m:meat."
default: "b"
private: no
hosts: 'monkeys' if food=='b' else {'cows' if food=='g' else 'lions'}
I am aware that using a dictionary would also be an option.
How do I implement it? It would be nice to have answers with if
s or with a dictionary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要使用正确的jinja表达式模板
hosts
参数,因此 inline-if if :哪个会产生:
与使食物相对应的词典相同:
You just need to template the
hosts
parameter with the correct Jinja expression, so an inline-if:Which would yield:
The same with a dictionary that would make a food correspond to an animal: