Ansible:'groups' 变量未定义

Ansible: 'groups' variable is undefined

我在尝试应用角色时收到一个奇怪的错误:显然 'groups'(内置变量)未定义。我已经在同一个剧本中测试过一个剧本并且变量有效。有什么想法吗?

- name: wtf?
  gather_facts: True
  hosts: tag_Name_Consul_server
  sudo: True
  user: username
  tasks:
    - name: thing
      debug: msg="-join {{ groups['tag_consul_server'][0] }}"

- name: blah
  gather_facts: True
  hosts: tag_Name_Consul_server
  sudo: True
  user: username
  vars:
    consul_command: "-join {{ groups['tag_consul_server'][0] }}"
  roles:
    - consul_server

第一个播放正常,第二个播放失败:

ERROR! the field 'vars' has an invalid value, which appears to include a variable that is undefined. The error was: 'groups' is undefined

内置变量以'ansible_'为前缀。所以你应该使用:

consul_command: "-join {{ ansible_groups['tag_consul_server'][0] }}"

更新 这不是您的解决方案,我在这里混淆了两件事。加载剧本静态变量时,尚未解析组成员身份。这显然发生在加载变量之后。

事实证明这是一个错误,并且已在最新版本的 Ansible 中进行了修补!