我正在使用ansible模板来生成配置文件。问题是我在我的模板中有很多条件语句,我想通过使用一个简单的条件选择正确的变量文件来删除这些语句。
我怎样才能做到这一点..例如:代替-
if os is defined in input variable file and os == 'centos'
if osver =7
username='centos7'
password='centos7passwd'
else if sver =8
username='centos8'
password='centos8passwd'
else if os is defined in input variable file and os == 'ubuntu'
username='ubuntu'
password='ubuntupasswd'
endif还有更多这样的东西,我宁愿让模板保持整洁,只需要选择正确的变量文件
if os is defined in input variable file and os == 'centos7' pick variables from file centos7_var.yaml
else if os == 'centos8' pick variables from file centos8_var.yaml
else if os == 'ubuntu' pick variables from file centos8_var.yaml
and generate the configuration from template我在这里做的是不是有什么根本的错误。不是ansible的专家。
发布于 2021-02-05 04:56:32
您可以在您的条件中使用ansible_facts嵌套变量
查看这个页面他们有一些有用的例子我认为它可以帮助你解决这个问题“发现变量:事实和神奇的变量--可接受的文档”https://docs.ansible.com/ansible/latest/user_guide/playbooks_vars_facts.html
https://stackoverflow.com/questions/66053590
复制相似问题