【问题标题】:Unable to load and fetch variables from group_vars file from /etc/ansible无法从 /etc/ansible 的 group_vars 文件加载和获取变量
【发布时间】: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 位置。

【问题讨论】:

    标签: ansible ansible-inventory


    【解决方案1】:

    “默认位置”是 ansible 查找清单的位置,如果您没有使用 -i /path/to/inventory.yaml 指定一个,但您使用 include_vars 包含的文件不是清单,而仅包含裸变量。
    如果您将include_vars 与相对路径一起使用,ansible 将在相对于剧本/角色的位置查找文件(请参阅documentation)。
    您也可以在include_vars 中指定绝对路径。例如。 /etc/ansible/variables.yaml(文件必须存在)。

    但实际上,我认为这不是你想要的。如果你的变量在/etc/ansible/group_vars 中,如果你正确地构建了你的目录,它们将默认加载而没有include_vars。检查docs

    /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 加载您库存中组 stack 的变量并显示正确的消息。

    【讨论】:

    • 感谢@toydarian .. 是的,如果我将变量文件放在剧本当前目录中它可以工作,并且没有 include_vars 的 group_vars 工作正常。但唯一让我感到困惑的是以下文档 - docs.ansible.com/ansible/latest/user_guide/…
    • 究竟是哪一部分?
    • 文档 - docs.ansible.com/ansible/latest/user_guide/…。最后一部分“这会从 group_vars/os_CentOS.yml 文件中提取变量”。我假设它来自 /etc/ansible 默认位置。但我现在明白你的意思了……谢谢@toydarian
    • 这个文档实际上有点混乱,可以用错误的方式理解。摘要:库存将在 group_vars 中查找相对于库存的组变量,而如果路径是相对的,include_vars 总是相对于角色/剧本。我的回答能回答你的问题吗?如果是这样,请随时接受它:)
    • 是的..现在很清楚..已经赞成你的答案..:)
    猜你喜欢
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 2022-10-06
    • 1970-01-01
    相关资源
    最近更新 更多