Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 894
Behrang Avatar asked Dec 12 '16 05:12

Behrang


People also ask

What format are Ansible inventory files formatted it?

ansible. builtin. yaml inventory – Uses a specific YAML file as an inventory source.

How can I get a list of hosts from an Ansible inventory file?

You can use the option --list-hosts. It will show all the host IPs from your inventory file.

Is Ansible YAML based?

Ansible uses YAML because it is very easy for humans to understand, read and write when compared to other data formats like XML and JSON.


2 Answers

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:
like image 189
techraf Avatar answered Oct 19 '22 18:10

techraf


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

like image 5
grepit Avatar answered Oct 19 '22 19:10

grepit