Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

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?

question from:https://stackoverflow.com/questions/41094864/is-it-possible-to-write-ansible-hosts-inventory-files-in-yaml

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...