我使用可见模板来生成配置文件。问题是我的模板中有很多条件语句,我想通过使用一个简单的条件选择正确的变量文件来摆脱它们。
我怎样才能做到这一点?例如:而不是-
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_facts这个嵌套变量在您的条件中使用
查看这个页面,他们有一些有用的例子,我认为它可以帮助你处理这个问题"发现变量:事实和神奇的变量- Ansible Documentation"https://docs.ansible.com/ansible/latest/user_guide/playbooks_vars_facts.html