当前位置: 首页 > 知识库问答 >
问题:

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

辛才俊
2023-03-14
[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 Playbookfedoragroupvarfile.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"}

共有1个答案

徐嘉勋
2023-03-14

如果不使用-I/path/to/inventory.yaml指定目录,则“默认位置”是ansible查找目录的位置,但是使用include_vars包含的文件不是目录,只包含裸变量。
如果使用相对路径的include_vars,ansible将在相对于playbook/role的位置查找文件(请参阅文档)。
也可以在include_vars中指定绝对路径。例如。/etc/ansible/variables.yaml(文件必须存在)。

但实际上,我不认为那是你想要的。如果变量位于/etc/ansible/group_vars中,如果目录结构正确,则默认情况下将不加载include_vars。检查一下文件。

/etc/ansible/group_vars/stack.yaml:

fedcharm: "This is Fedora 32 OS variable file"

fedoragroupvarsfile.yaml应包含以下内容:

- hosts: all
  remote_user: admin
  tasks:
    - debug:
        var: fedcharm

然后像以前一样运行你的剧本。如果所有操作都正确,ansible将从/etc/ansible/group_vars/stack.yaml中加载目录中组堆栈的变量,并显示正确的消息。

 类似资料:
  • 我有一个假的怀疑让我卡住了很长时间。我有一个非常平庸的包含主机和变量的清单文件: 问题是我需要从group_vars/all文件访问清单文件中的所有这些主机和变量。 我尝试了以下方式来访问主机IP: 为了访问主机变量,我尝试了: 问题:在变量文件中使用清单文件中声明的主机,比方说:group_vars/all。 示例:我有一个DB主机,。 库存文件: 问题是group_vars/all文件中的变量

  • 我想看GMail的邮件正文。 我能够获得所有详细信息,例如发件人姓名,电子邮件和主题等,但不能获得电子邮件正文。 我知道有特定的Gmail的API可以做到这一点,但IMAP不应该工作吗? 从PHP的文档中可以看出: 我尝试了<code>$message=imap_fetchbody($inbox,$email_number,“1.2”)和

  • 问题内容: 我在 Java 应用程序中使用 JasperReports 。 __ 我有一个名为“ reports”的包,用于存储所有生成的报告。这是我在应用程序中调用jasper报告的方式。 可以了 我没有给出完整的路径,而是尝试了: 但这在运行程序时显示错误: 不适合提供 JRXmlLoader 的完整路径,因为如果要在其他计算机上运行此应用程序,则必须根据计算机路径更改所有编码。所以帮我 问题

  • 我们可以使用Autoloader来跟踪是否已从S3存储桶加载的文件。我关于Autoloader的问题:有没有办法读取Autoloader数据库以获取已加载文件的列表? 我可以在AWS Glue job书签中轻松做到这一点,但我不知道如何在Databricks Autoloader中做到这一点。

  • 我已经将我的项目上传到githttps://github.com/gitekiras/spring-boot-gradle-mvc 我不能得到正确的是,我不能加载css和js从资源文件夹。 当我把它放在webapp文件夹,我能够得到每件事情的工作,但根据教程,我应该把它们放在资源的静态文件夹。 但是当我这样做的时候,我不能加载页面上的css和js。 谁能说出我错过了什么? 另外:当我在浏览器日志中

  • /inventory/group_vars/directory有 我希望host_1引用/inventory/group_vars/host_1.yml中的变量