Ansible Hosts and Groups
Table of Contents
Introduction
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.
edge1.ord.test.net
[ChicagoEdges]
edge1.ord.test.net:2200
edge2.ord.test.net
[AllEdges]
edge1.ord.test.net:2200
edge2.ord.test.net
edge3.a.test.net
edge4.d.test.net
- 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:
edge[1:5].test.net
edge1.[a:d].test.net
Variables
- 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=192.168.1.50
- In the example above the SECRETHOST will be mapped to 192.168.1.50 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:
[ChicagoEdges]
edge1.ord.test.net:2200
edge2.ord.test.net
[ChicagoEdges:vars]
ntp_server=192.168.1.1
tacacs_server=192.168.1.2
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:
[ChicagoEdges]
edge1
[MadisonEdges]
edge2
[MidwestEdges:children]
ChicagoEdges
MadisionEdges
- 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.