Ansible配置文件无法识别Jinja变量,库存路径配置异常求助
Ansible 自定义清单路径配置失败排查
问题说明
我通过pip3 install ansible安装了Ansible,想要把默认清单路径从/etc/ansible/hosts修改为$HOME/.ansible/inventory_list,假设ANSIBLE_HOME环境变量指向$HOME/.ansible。
尝试的配置
在~/.ansible.cfg中添加了以下配置:
inventory={{ ANSIBLE_HOME ~ "/inventory_list" }}
实际执行结果
运行ansible-config dump --only-changed查看生效配置时,得到如下错误结果:
$ ansible-config dump --only-changed DEFAULT_HOST_LIST(/home/liso/.ansible.cfg) = ['/home/liso/{{ ANSIBLE_HOME }}']
期望的正确结果
希望配置生效后显示:
DEFAULT_HOST_LIST(/home/liso/.ansible.cfg) = ['/home/liso/.ansible/inventory_list']
解决方法
原因分析
Ansible的.cfg配置文件(INI格式)不支持Jinja2表达式语法(比如你用的{{ ... }}和~字符串拼接写法),只能直接使用环境变量或绝对路径。
可行配置方案
方案1:直接引用环境变量
确保ANSIBLE_HOME环境变量已正确设置(比如在~/.bashrc或~/.zshrc中添加export ANSIBLE_HOME="$HOME/.ansible",然后重启shell或执行source ~/.bashrc),然后修改配置为:
inventory = $ANSIBLE_HOME/inventory_list
方案2:使用绝对路径或家目录简写
如果不想依赖环境变量,直接写绝对路径:
inventory = /home/liso/.ansible/inventory_list
或者用~指代当前用户家目录(更通用):
inventory = ~/.ansible/inventory_list
内容的提问来源于stack exchange,提问作者Liso
相关产品推荐
相关产品推荐

