ansible yml nested vars

Hi, I am looking to see if I can or for an example of a nested vars using yml format. I was able to nest the inventory files using all, children and pass host specific vars.  With this idea I was trying to do that with my vars needed to run a play. When take the inventory format or create a new vars file and I do not follow more of the ini method. I get an var not found error.
Inventory works for yml
all:
  children:
     linuxhost1:
        children:
           host1:
               ip1: "(ip address)"

the above works.
When I do the same for say a default gateway entry. i am getting an error of not found
all:
  children:
     linuxhost1:
      children:
         defaultgateway
If I move the defaultgateway all the way to the left it works fine.

I saw the yml vars example of the fruits from ansibles.

Is it possible to do what I would like to do? If not I will continue to go with the ini file.  It is just the vars required is getting large and complicated for an ini format.





  • post-author-pic
    Stosh O
    11-16-2018

    Hi Jason,

    If you are looking to nest variables, I believe yaml will be the best choice.  I do not believe the functionality exists for INI variable files parsed by Ansible and, even if it does, it is not very well documented and / or publicized.

  • post-author-pic
    Jason V
    11-16-2018

    Thanks for the help. I was looking there already but it was in how the using dynamic vars. format I needed to get the tasks I was loolking for done.Incase anyone else needs this info. I found a way to do what I wanted. For the second part I removed the children.  The for the var I wanted like defaulltgateway.  {{['all']['linuxhost1']['defaultgateway']}}   This allowed me to target vars way down a list.  Mainly to make it easier for me /others to edit from a large list of vars.    No moving to the fun part of converting two very large ini vars files into this format.

Looking For Team Training?

Learn More