From c3550b58ede1fcfaf84da2a81bab394e040802bd Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 6 Sep 2017 23:47:27 -0400 Subject: [PATCH] introducing ALSO YAML inventory (#28596) * introducing ALSO YAML inventory * Copy edits --- docs/docsite/rst/intro_inventory.rst | 131 +++++++++++++++++++++------ 1 file changed, 104 insertions(+), 27 deletions(-) diff --git a/docs/docsite/rst/intro_inventory.rst b/docs/docsite/rst/intro_inventory.rst index 4632504ab3e..70a18b6409f 100644 --- a/docs/docsite/rst/intro_inventory.rst +++ b/docs/docsite/rst/intro_inventory.rst @@ -11,7 +11,7 @@ which defaults to being saved in the location ``/etc/ansible/hosts``. You can specify a different inventory file using the ``-i `` option on the command line. Not only is this inventory configurable, but you can also use multiple inventory files at the same time and -pull inventory from dynamic or cloud sources, as described in :doc:`intro_dynamic_inventory`. +pull inventory from dynamic or cloud sources or different formats (YAML, ini, etc), as described in :doc:`intro_dynamic_inventory`. Introduced in version 2.4, Ansible has inventory plugins to make this flexible and customizable. .. _inventoryformat: @@ -38,12 +38,30 @@ For this example, the format for ``/etc/ansible/hosts`` is an INI-like (one of A The headings in brackets are group names, which are used in classifying systems and deciding what systems you are controlling at what times and for what purpose. +A YAML version would look like: + +.. code-block:: yaml + + all: + hosts: + mail.example.com + children: + webservers: + hosts: + foo.example.com: + bar.example.com: + dbservers: + hosts: + one.example.com: + two.example.com: + three.example.com: + + It is ok to put systems in more than one group, for instance a server could be both a webserver and a dbserver. If you do, note that variables will come from all of the groups they are a member of. Variable precedence is detailed in a later chapter. -If you have hosts that run on non-standard SSH ports you can put the port number -after the hostname with a colon. Ports listed in your SSH config file won't be used with the `paramiko` -connection but will be used with the `openssh` connection. +If you have hosts that run on non-standard SSH ports you can put the port number after the hostname with a colon. +Ports listed in your SSH config file won't be used with the `paramiko` connection but will be used with the `openssh` connection. To make things explicit, it is suggested that you set them if things are not running on the default port: @@ -51,20 +69,33 @@ To make things explicit, it is suggested that you set them if things are not run badwolf.example.com:5309 -Suppose you have just static IPs and want to set up some aliases that live in your host file, or you are connecting through tunnels. You can also describe hosts like this: +Suppose you have just static IPs and want to set up some aliases that live in your host file, or you are connecting through tunnels. +You can also describe hosts via variables: + +In INI: .. code-block:: ini jumper ansible_port=5555 ansible_host=192.0.2.50 -In the above example, trying to ansible against the host alias "jumper" (which may not even be a real hostname) will contact 192.0.2.50 on port 5555. Note that this is using a feature of the inventory file to define some special variables. Generally speaking this is not the best -way to define variables that describe your system policy, but we'll share suggestions on doing this later. We're just getting started. +In YAML: -.. note:: Values passed in using the ``key=value`` syntax are interpreted as Python literal structure (strings, numbers, tuples, lists, dicts, - booleans, None), alternatively as string. For example ``var=FALSE`` would create a string equal to 'FALSE'. Do not rely on types set - during definition, always make sure you specify type with a filter when needed when consuming the variable. +.. code-block:: yaml + + hosts: + jumper: + ansible_port: 5555 + ansible_host: 192.0.2.50 + +In the above example, trying to ansible against the host alias "jumper" (which may not even be a real hostname) will contact 192.0.2.50 on port 5555. +Note that this is using a feature of the inventory file to define some special variables. +Generally speaking, this is not the best way to define variables that describe your system policy, but we'll share suggestions on doing this later. -Adding a lot of hosts? If you have a lot of hosts following similar patterns you can do this rather than listing each hostname: +.. note:: Values passed in the INI format using the ``key=value`` syntax are not interpreted as Python literal structure + (strings, numbers, tuples, lists, dicts, booleans, None), but as a string. For example ``var=FALSE`` would create a string equal to 'FALSE'. + Do not rely on types set during definition, always make sure you specify type with a filter when needed when consuming the variable. + +If you are adding a lot of hosts following similar patterns, you can do this rather than listing each hostname: .. code-block:: ini @@ -91,15 +122,14 @@ You can also select the connection type and user on a per host basis: other1.example.com ansible_connection=ssh ansible_user=mpdehaan other2.example.com ansible_connection=ssh ansible_user=mdehaan -As mentioned above, setting these in the inventory file is only a shorthand, and we'll discuss how to store them in individual files -in the 'host_vars' directory a bit later on. +As mentioned above, setting these in the inventory file is only a shorthand, and we'll discuss how to store them in individual files in the 'host_vars' directory a bit later on. .. _host_variables: Host Variables ++++++++++++++ -As alluded to above, it is easy to assign variables to hosts that will be used later in playbooks: +As described above, it is easy to assign variables to hosts that will be used later in playbooks: .. code-block:: ini @@ -112,7 +142,11 @@ As alluded to above, it is easy to assign variables to hosts that will be used l Group Variables +++++++++++++++ -Variables can also be applied to an entire group at once:: +Variables can also be applied to an entire group at once: + +The INI way: + +.. code-block:: ini [atlanta] host1 @@ -122,14 +156,30 @@ Variables can also be applied to an entire group at once:: ntp_server=ntp.atlanta.example.com proxy=proxy.atlanta.example.com -Be aware that this is only a convenient way to apply variables to multiple hosts at once; even though you can target hosts by group, variables are always flattened to the host level before a play is executed. +The YAML version: + +.. code-block:: yaml + + atlanta: + hosts: + host1: + host2: + vars: + ntp_server: ntp.atlanta.example.com + proxy: proxy.atlanta.example.com + +Be aware that this is only a convenient way to apply variables to multiple hosts at once; even though you can target hosts by group, **variables are always flattened to the host level** before a play is executed. .. _subgroups: Groups of Groups, and Group Variables +++++++++++++++++++++++++++++++++++++ -It is also possible to make groups of groups using the ``:children`` suffix. Just like above, you can apply variables using ``:vars``:: +It is also possible to make groups of groups using the ``:children`` suffix in INI or the ``children:`` entry in YAML. +You can apply variables using ``:vars`` or ``vars:``: + + +.. code-block:: ini [atlanta] host1 @@ -155,11 +205,38 @@ It is also possible to make groups of groups using the ``:children`` suffix. Jus southwest northwest +.. code-block:: yaml + + all: + children: + usa: + children: + southeast: + children: + atlanta: + hosts: + host1: + host2: + raleigh: + hosts: + host2: + host3: + vars: + some_server: foo.southeast.example.com + halon_system_timeout: 30 + self_destruct_countdown: 60 + escape_pods: 2 + northeast: + northwest: + southwest: + If you need to store lists or hash data, or prefer to keep host and group specific variables separate from the inventory file, see the next section. Child groups have a couple of properties to note: - - First, any host that is member of a child group is automatically a member of the parent group. - - Second, a child group's variables will have higher precedence (override) a parent group's variables. + - Any host that is member of a child group is automatically a member of the parent group. + - A child group's variables will have higher precedence (override) a parent group's variables. + - Groups can have multiple parents and children, but not circular relationships. + - Hosts can also be in multiple groups, but there will only be **one** instance of a host, merging the data from the multiple groups. .. _default_groups: @@ -168,20 +245,20 @@ Default groups There are two default groups: ``all`` and ``ungrouped``. ``all`` contains every host. ``ungrouped`` contains all hosts that don't have another group aside from ``all``. +Every host will always belong to at least 2 groups. +Though ``all`` and ``ungrouped`` are always present, they can be implicit and not appear in group listings like ``group_names``. .. _splitting_out_vars: Splitting Out Host and Group Specific Data ++++++++++++++++++++++++++++++++++++++++++ -The preferred practice in Ansible is actually not to store variables in the main inventory file. +The preferred practice in Ansible is to not store variables in the main inventory file. -In addition to storing variables directly in the INI file, host -and group variables can be stored in individual files relative to the -inventory file. +In addition to storing variables directly in the inventory file, host and group variables can be stored in individual files relative to the inventory file (not directory, it is always the file). -These variable files are in YAML format. Valid file extensions include '.yml', '.yaml', '.json', -or no file extension. See :doc:`YAMLSyntax` if you are new to YAML. +These variable files are in YAML format. Valid file extensions include '.yml', '.yaml', '.json', or no file extension. +See :doc:`YAMLSyntax` if you are new to YAML. Assuming the inventory file path is:: @@ -202,9 +279,9 @@ the 'raleigh' group might look like:: ntp_server: acme.example.org database_server: storage.example.org -It is ok if these files do not exist, as this is an optional feature. +It is okay if these files do not exist, as this is an optional feature. -As an advanced use-case, you can create *directories* named after your groups or hosts, and +As an advanced use case, you can create *directories* named after your groups or hosts, and Ansible will read all the files in these directories. An example with the 'raleigh' group:: /etc/ansible/group_vars/raleigh/db_settings