首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从/etc/ansible中加载和从group_vars文件中获取变量

无法从/etc/ansible中加载和从group_vars文件中获取变量
EN

Stack Overflow用户
提问于 2021-04-12 04:13:01
回答 1查看 1.6K关注 0票数 2

我试图从group_vars中获取变量值,但是ansible无法从默认位置/etc/ansible中找到它。这是细节。

不可信版本

代码语言:javascript
复制
[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)]

不可测库存文件/etc/ansible/hosts

代码语言:javascript
复制
[stack]
192.168.47.130

Ansible Playbook fedoragroupvarfile.yaml

代码语言:javascript
复制
- hosts: all
  remote_user: admin
  tasks:
    - name: Set OS distribution dependent variables
      include_vars: "os_{{ ansible_facts['distribution'] }}.yaml"
    - debug:
        var: fedcharm

group_vars文件/etc/ansible/group_vars/os_Fedora.yaml

代码语言:javascript
复制
fedcharm: "This is Fedora 32 OS variable file"

错误

代码语言:javascript
复制
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试图在当前剧本的目录中找到变量文件,而不是文件所在的默认/etc/ansible/group_vars位置。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-12 04:52:50

如果您不使用-i /path/to/inventory.yaml指定库存,那么“默认位置”就是ansible查找库存的位置,但是include_vars中包含的文件不是库存,而是只包含裸变量。

如果使用带有相对路径的include_vars,ansible将在相对于剧本/角色的位置查找文件(参见文档)。

您也可以在include_vars中指定绝对路径。例如/etc/ansible/variables.yaml (文件必须存在)。

但实际上,我不认为这是你想要的。如果您在/etc/ansible/group_vars中有您的变量,如果您正确地构造您的目录,它们将在默认情况下不使用include_vars加载。检查一下文档

/etc/ansible/group_vars/stack.yaml中放置包含此行的文件

代码语言:javascript
复制
fedcharm: "This is Fedora 32 OS variable file"

fedoragroupvarsfile.yaml应该包含以下内容:

代码语言:javascript
复制
- hosts: all
  remote_user: admin
  tasks:
    - debug:
        var: fedcharm

那就像以前一样运行你的剧本。如果您正确地完成了所有操作,ansible将从stack中加载组/etc/ansible/group_vars/stack.yaml中的变量,并显示正确的消息。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67052578

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档