【问题标题】:Ansible is not picking the group_vars and host_varsAnsible 没有选择 group_vars 和 host_vars
【发布时间】:2020-06-21 15:49:34
【问题描述】:

我是 Ansible 的新手,无法弄清楚为什么剧本没有选择我定义的 group_vars/ 和 host_vars。根据文件:

您还可以将 group_vars/ 和 host_vars/ 目录添加到您的 playbook 目录。 ansible-playbook 命令默认在当前工作目录中查找这些目录。

我的剧本、库存和其他文件结构非常简单。它应该与默认值匹配。

库存文件:

dummy

[spider]
s0ra
s0ra_slave

剧本:

- name: base mix release upgrade Prod.
  hosts: spider
  gather_facts: false

  # vars_files:
  # - vars/s0ra_sup.yaml

  tasks:
    - name: check release bin
      stat:
        path: "{{ sh_lastrel }}"
      register: rel_bin

当我尝试运行 ansible-playbook -i inventory.ini mix_upgrade.yaml 的剧本时,它抱怨:

PLAY [base mix release upgrade Prod.] **********************************************************************************

TASK [check release bin] ***********************************************************************************************
fatal: [s0ra]: FAILED! => {"msg": "The task includes an option with an undefined variable. 
The error was: 'sh_lastrel' is undefined\n\n
The error appears to be in 'xxx/ansible/mix_upgrade.yaml': line 19, column 7, but may\n
be elsewhere in the file depending on the exact syntax problem.\n\n
The offending line appears to be:\n\n\n
    - name: check release bin\n      ^ here\n"}

PLAY RECAP *************************************************************************************************************
s0ra                       : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

sh_lastrel 实际上是在spider.yaml 中定义的。我不知道为什么它没有加载。我试图打开 -v 模式,但它似乎没有更多的调试信息。非常感谢任何有关原因或如何进一步调试的提示。

我的ansible版本如下:

╰─ ansible --version                                                                                   ✔  22:01:57 
ansible 2.9.9
  config file = None
  configured module search path = ['/Users/kenchen/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/kenchen/.pyenv/versions/3.8.2/lib/python3.8/site-packages/ansible
  executable location = /Users/kenchen/.pyenv/versions/3.8.2/bin/ansible
  python version = 3.8.2 (default, May 18 2020, 00:02:00) [Clang 10.0.1 (clang-1001.0.46.4)]

【问题讨论】:

  • 这应该可以工作。确保运行命令的用户可以读取group_vars/spider.yml。发布文件。一般设为mcve
  • 从您的屏幕截图中可以看出,您在group_ vars 中有一个空格——这是一个奇妙的原因,为什么人们不应该在问题中使用代码屏幕截图,正如在how to ask 页面
  • @mdaniel 很抱歉,我认为着色会帮助人们更容易地看到结构,而不是一些自生成的文件夹树。你是对的,额外的空间打破了它。非常感谢。

标签: ansible


【解决方案1】:

确保group_vars/spider.yml 可用。例如,

shell> cat group_vars/spider.yml 
sh_lastrel: value of sh_lastrel defined in group_vars/spider.yml
shell> cat inventory.ini
dummy
[spider]
s0ra
s0ra_slave
shell> cat mix_upgrade.yaml 
- hosts: spider
  gather_facts: false
  tasks:
    - debug:
        var: sh_lastrel
shell> ansible-playbook -i inventory.ini mix_upgrade.yaml 

ok: [s0ra] => 
  sh_lastrel: value of sh_lastrel defined in group_vars/spider.yml
ok: [s0ra_slave] => 
  sh_lastrel: value of sh_lastrel defined in group_vars/spider.yml

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多