Ansible Hosts and Groups


Ansible uses host files to define the hosts to manage with optional their variables to be used in playbooks. To create a hierarchy host can be organized into groups. This page will discuss host and group with variable definitions.

Host and Groups

  • File stored in /etc/ansible/hosts
  • It is a simple format for storing host and their associated groups.


  • Brackets define a group.
  • Hosts can be member of multiple groups.
  • Hosts can be without a group.
  • Host running non standard SSH port, can define port such as :2200 in the example above.
  • For a large number of hosts that has a structured hostname ranges can be added to the host file. These ranges can be numeric or alphabetic for example:


  • Variable can be used in playbooks……NEED MORE.

Host Variables

  • Ansible has some special variables. For example to define a host resolution only to be used by ansible you can add variables such as:
SECRETHOST ansible_ssh_port=2200 ansible_ssh_host=
  • In the example above the SECRETHOST will be mapped to using ssh port 2200. SECRETHOST is not in DNS or the OS's host file.
  • In general adding variables to hosts in the host file is as simple as adding them after the hostname.

Group Variables

  • Not only host can have variables, but groups can as well.
  • For example to create variables such as ntp_server or tacacs_server to the group ChicagoEdges use the keyword :vars:


Groups of Groups

  • Ansible has the capability to create nested groups. For example having two groups ChicagoEdges and MadisonEdges can be combined into another group called MidwestEdges. To configured use the keyword children:


  • The aggregates group (MidwestEdges) can also have variables just like any other group. [MidwestEdges:vars].
  • Groups can be aggregated multiple levels. For example have a group MidwestEdges group with UnitedStatesEdges.


rating: 0+x
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License