40

In the best practices page, there is an example that uses hosts.yml for hosts files:

YAML-based hosts file

In the docs, however, I can only find the INI syntax for writing hosts files.

What is the syntax for the inventory files in YAML?

techraf
  • 64,883
  • 27
  • 193
  • 198
Behrang
  • 46,888
  • 25
  • 118
  • 160
  • 1
    ...although I still believe the reference on your screenshot is a leftover from the past. Interesting question, I wasn't aware of brining the feature back. – techraf Dec 12 '16 at 07:40
  • Ansible 2.4 (released in September 2017) has re-introduced support for this! – Ben S Sep 24 '17 at 17:06

3 Answers3

50

Yes.

It's been deprecated in version 0.6 in 2012 and reintroduced in a commit first included in version 2.1 in 2016.

The example file on GitHub contains the guidelines and examples:

  • Comments begin with the '#' character
  • Blank lines are ignored
  • Top level entries are assumed to be groups
  • Hosts must be specified in a group's hosts: and they must be a key (: terminated)
  • groups can have children, hosts and vars keys
  • Anything defined under a hosts is assumed to be a var
  • You can enter hostnames or ip addresses
  • A hostname/ip can be a member of multiple groups

Ex 1: Ungrouped hosts, put in 'ungrouped' group

ungrouped:
  hosts:
      green.example.com:
          ansible_ssh_host: 191.168.100.32
      blue.example.com:
      192.168.100.1:
      192.168.100.10:

Ex 2: A collection of hosts belonging to the 'webservers' group

webservers:
  hosts:
      alpha.example.org:
      beta.example.org:
      192.168.1.100:
      192.168.1.110:

Ex 3: You can create hosts using ranges and add children groups and vars to a group. The child group can define anything you would normally add to a group

testing:
  hosts:
      www[001:006].example.com:
  vars:
      testing1: value1
  children:
      webservers:
          hosts:
              beta.example.org:
techraf
  • 64,883
  • 27
  • 193
  • 198
  • Also, "These variable files are in YAML format. Valid file extensions include ‘.yml’, ‘.yaml’, ‘.json’, or no file extension." source: http://docs.ansible.com/ansible/intro_inventory.html – Elijah Lynn Mar 17 '17 at 16:42
  • 2
    @ElijahLynn I don't understand your comment. You cites a part referring to host and group variables. This is a different thing. – techraf Mar 17 '17 at 23:15
  • @techraf Do you know how to specify which format Ansible should expect? Somehow my version 2.2.1.0 tries to interpret the INI format as yaml. – TonyH Mar 29 '17 at 17:23
  • @TonyH Ansible figures it out automatically. Create a new question and include the relevant data, maybe someone would be able to help. – techraf Mar 29 '17 at 22:30
  • 1
    Viewing ansible's code, I learned it actually tries to load any inventory 3 ways regardless. As an INI, Yaml, and python script. My problem was actually a typo, but the Yaml 'error' was a false alarm which distracted me from the real error. – TonyH Mar 29 '17 at 22:34
  • The link @ElijahLynn suggests re. inventory filename extensions is now [here](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#organizing-host-and-group-variables) – Andrew Richards Oct 22 '21 at 14:51
6

Previous answers are correct but here is simple hosts.yaml and INI like side by side in the screenshot and I'm just copying the actual hosts.yaml here too so if you want copy and paste and edit it for yourself

--- 
all: 
  hosts:               
    xmp: 
      ansible_connection: ssh
      ansible_host: "192.1.0.1"
      ansible_port: 7822
      ansible_user: nanoseco

enter image description here

some more info:

https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

grepit
  • 21,260
  • 6
  • 105
  • 81
2

I just discovered Ansible INI to YAML inventory converter on github which worked fine for me:

This repository contains a Python script for converting Ansible inventories in INI format to YAML format.

For some reason, the conversion ended up with host ranges ([01:03]) being seperated by = instead of the shown and correct :.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
jitter
  • 434
  • 8
  • 16
  • @JoshUsre please report the issue you encountered on the github of the project mentioned. – jitter Jul 12 '19 at 21:40
  • 2
    You can use the ansible-inventory command to convert between formats. https://evrard.me/convert-ansible-inventories-with-ansible-inventory-cli – user1717259 Aug 30 '19 at 12:53