【发布时间】:2021-04-12 04:13:01
【问题描述】:
我正在尝试从 group_vars 中获取变量值,但 ansible 无法从默认位置 /etc/ansible 中找到它。以下是详细信息。
Ansible 版本
[admin@ansicontrol newvarsexample]$ ansible --version
ansible 2.9.18
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/admin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.8/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.2 (default, Feb 28 2020, 00:00:00) [GCC 10.0.1 20200216 (Red Hat 10.0.1-0.8)]
Ansible 库存文件/etc/ansible/hosts:
[stack]
192.168.47.130
Ansible 剧本fedoragroupvarfile.yaml:
- hosts: all
remote_user: admin
tasks:
- name: Set OS distribution dependent variables
include_vars: "os_{{ ansible_facts['distribution'] }}.yaml"
- debug:
var: fedcharm
Ansible group_vars 文件/etc/ansible/group_vars/os_Fedora.yaml:
fedcharm: "This is Fedora 32 OS variable file"
错误
TASK [Set OS distribution dependent variables] *******************************************************************************
fatal: [192.168.47.130]: FAILED! => {"ansible_facts": {}, "ansible_included_var_files": [], "changed": false, "message": "Could not find or access 'os_Fedora.yaml'\nSearched in:\n\t/home/admin/practiceansible/newvarsexample/vars/os_Fedora.yaml\n\t/home/admin/practiceansible/newvarsexample/os_Fedora.yaml\n\t/home/admin/practiceansible/newvarsexample/vars/os_Fedora.yaml\n\t/home/admin/practiceansible/newvarsexample/os_Fedora.yaml on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}
为什么include_vars 会尝试在当前playbook 的目录中查找变量文件,而不是文件所在的默认/etc/ansible/group_vars 位置。
【问题讨论】: