首页 > 解决方案 > 一个主机有许多组的变量

问题描述

我有一个如下所示的库存文件:

all:
  children:
    win:

      children:
        hi:
          hosts:
            10.50.127.18:
            10.50.127.19:

        bye:
          hosts:
            10.50.127.18:
            10.50.127.19:
    linux:
      children:
        hi:
          hosts:
            10.50.127.20:
        bye:
          hosts:
            10.50.127.20:  

在组 vars 文件夹中,我有这个代码 hi.yml

services:
  - ls
  - pwd

再见.yml

services:
  - pwd

然后在剧本中我有这个:

- hosts: linux
  gather_facts: no
  tasks:
  - name: lalaland
    command: "{{ item }}"
    loop: "{{services}}"

但我收到如下错误:

fatal: [10.50.127.18]: FAILED! => {"msg": "Unexpected failure in finding the lookup named '{{services}}' in the available lookup plugins"}

你能告诉我我做错了什么吗?你也知道我是否有交叉变量含义,比如在这种情况下([ls] 和 [ls,pwd]),我会得到 vars [ls,ls,pwd] 或 [ls,pwd] 的结果列表吗?

标签: ansibleansible-inventory

解决方案


但我收到如下错误:

我通过从您的问题中复制粘贴它们的内容来制作相同的文件,并且它在 Ansible 2.8 上没有任何错误。

你也知道我是否有交叉变量含义,比如在这种情况下([ls] 和 [ls,pwd]),我会得到 vars [ls,ls,pwd] 或 [ls,pwd] 的结果列表吗?

解析库存时,组变量将应用于主机。并且替换同名的变量。因此,由于在您的库存中按字母顺序排列的最后一组是“hi”,因此您的主机将拥有service = [ls, pwd].


推荐阅读