cancel
Showing results for 
Search instead for 
Did you mean: 
Trevor
Commander Commander
Commander
  • 430 Views

YAML Syntax - Inventory Definition

Calling all YAML juggernauts!!! 


I'm looking at some YAML syntax, that I need help unpacking.
Here's the synax:

--- prod: children: eastus: hosts: srv3: srv[1:2]: python_var: /usr/bin/python3.6 centralus: hosts: srv4


The syntax is shown on a single line.  However, I know that it should be broken
up into multiple lines, and indented accordingly.  My challenge is, which pieces 
go on separate lines, and how to indent.

This information is supposed to represent an inventory definition.  The inventory
definition is supposed to set a variable named "python_var", only for hosts
"srv1" and "srv2", in the "eastus" group.

In addition to my inability to enter the syntax in the correct format, I'm clueless
on what to do with the host names "srv3" and "srv4".  Where do they come into
play?  

The final piece, that's causing me to throw up both of my hands is that name
"centralus".  It's not identified specifically in the question, as to what it is supposed
to represent.  Is it supposed to represent a group name?  I'll stop with that guess

Can you solve this puzzle? 

Whew! Now, where did I put that bottle of Excedrin

 

 

 

 

 

Trevor "Red Hat Evangelist" Chandler
Labels (3)
9 Replies
Chetan_Tiwary_
Community Manager
Community Manager
  • 394 Views

@Trevor Before Travis arrives with his ansible cavalry, let me quickly try to handle this immediate challenge and hold the ground !

Quick glance at this inventory definition, I can deduce that  prod is the parent group and children the sub groups with obvious US geo regions east and central as children sub groups under prod. 

Also given your condition of placing the python_var for only srv1 & srv2 in eastus group I can make this yaml formatted like this : 

---
prod:
  children:
    eastus:
      hosts:
        srv1:
          python_var: /usr/bin/python3.6
        srv2:
          python_var: /usr/bin/python3.6
        srv3: {}
    centralus:
      hosts:
        srv4: {}

 

Refer the official doc for more info on this : https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#organizing-host-and-gro... 

 

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yaml_inventory.html 

Trevor
Commander Commander
Commander
  • 379 Views

Chetan, from what I can see, I'd be surprised if the Godfather of Ansible (aka Travis) makes any changes to your formatting.  If I had been given what you produced, I would have never posted the query.

I'm eager to see what the Ansible Authority has to say about your work!

Thanks Chetan!!!

Trevor "Red Hat Evangelist" Chandler
Chetan_Tiwary_
Community Manager
Community Manager
  • 340 Views

ok @Trevor will wait for their approval - in the meantime does it work for you ?

bonnevil
Starfighter Starfighter
Starfighter
  • 336 Views

So @Chetan_Tiwary_, your work looks good to me.  Running your YAML through ansible-inventory -i your-inventory --list --yaml gives me this YAML:

all:
children:
prod:
children:
centralus:
hosts:
srv4: {}
eastus:
hosts:
srv1:
python_var: /usr/bin/python3.6
srv2:
python_var: /usr/bin/python3.6
srv3: {}

The empty curly braces weren't in the original question, but they will get inserted by ansible-inventory if they're missing, as needed. I also get the same YAML output from the following equivalent INI inventory:

[prod:children]
eastus
centralus

[eastus]
srv3
srv[1:2] python_var=/usr/bin/python3.6

[centralus]
srv4

 

Chetan_Tiwary_
Community Manager
Community Manager
  • 325 Views

ok wonderful @bonnevil Thanks !

Trevor
Commander Commander
Commander
  • 305 Views

bonnevil, 2 questions:

1) Do you have any idea why the "ansible-inventory" command changed - in its output - the vertical position of the "centralus" child, from the bottom of the YAML code (where it originally appeared as the 2nd child), to the top (to appear as the first child)?   This is not critical to know.  If there's something significant about this, I'd like to be able to add to my knowledgebase - that's all.

2) Did you run a command to get that equivalent INI inventory format?

 

 

Trevor "Red Hat Evangelist" Chandler
Chetan_Tiwary_
Community Manager
Community Manager
  • 266 Views

@Trevor good question. 

1.) I think it formatted the child subgroups alphabetically.

2.) Ansible inventory tool supports output formatting in JSON or YAML only, You need to manually write that INI format. 

@bonnevil can confirm if the above points are right or not. 

bonnevil
Starfighter Starfighter
Starfighter
  • 257 Views

@Trevor / @Chetan_Tiwary_ Yes, the output sorts the child groups and hosts alphabetically when it does the conversion, as well as adding the optional YAML that indicates that prod is a child group of all (which is otherwise assumed).  The nice thing about this is that it means that equivalent inventory that happens to have the groups ordered differently have the exact same output from ansible-inventory, which is great for testing.

Yeah, I saw that it doesn't convert to INI, so I just manually wrote the INI based on the YAML and tested the INI with ansible-inventory to convert it to YAML and make sure I was correct and got what I expected.

I'll admit that I left things out of alphabetical order in the INI to match the order in the original question and to subtly highlight the whole alphebetizing / consistent output behavior of ansible-inventory.

Trevor
Commander Commander
Commander
  • 252 Views

bonnevil, your lesson was too excellent for me to be concerned about the alphabetical
order in the INI format.

Thank you for a wonderful explanation!!!!

Trevor "Red Hat Evangelist" Chandler
Join the discussion
You must log in to join this conversation.