Is it possible to write Ansible hosts/inventory files in YAML?

Question:

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?

Answer:

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

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

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

Leave a Reply