Search code examples
ansibleansible-inventory

One host has vars of many groups


I have an inventory file looking like this:

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:  

in group vars folder i've got this code hi.yml

services:
  - ls
  - pwd

bye.yml

services:
  - pwd

then in playbook i have this:

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

but i'm getting errors like:

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

Can you tell me what i'm doing wrong? also do you know if i have crossed variable meanings, like in this case([ls] and [ls, pwd]), will i get in result list of vars [ls,ls,pwd] or [ls,pwd]?


Solution

  • but i'm getting errors like:

    I made the same files by copy-pasting their content from your question, and it works without any errors on Ansible 2.8.

    also do you know if i have crossed variable meanings, like in this case([ls] and [ls, pwd]), will i get in result list of vars [ls,ls,pwd] or [ls,pwd]?

    Group variables would be applied to host when parsing your inventory. And the variables with the same name are substituted. So since in your inventory last group in alphabetical order is "hi", your hosts will have service = [ls, pwd].