Ansible - Group (Host Properties)

Card Puncher Data Processing

About

A host can have one or more group (tag). A group may have also have a group. See Ansible - Child Group (Children|Group of Group)

Groups don’t really survive outside of inventory and host matching because variables are defined to a specific host before a play is run.

Built-in

There are two built-in groups:

  • all: all contains every host
  • ungrouped: ungrouped contains all hosts that don’t have a group

All

The all group contains all host

ungrouped

The ungrouped group contains all hosts that don’t have a group

Management

Definition

In an inventory file

[group1]
host1
host2

Task Conditional

Running a task if a server is or not in a group.

group_names is a list

when: 'webserver' not in group_names
# or
when: 'webserver' in group_names

Built-in groups variable

During execution, you can access the groups information with the built-in groups variable

Example: the first host

{{ groups['groupname'][0] }}
"all": [
	"alias_host"
],
"group1": [
	"alias_host"
],
"group2": [
	"alias_host"
],
"ungrouped": []

  • and with hostvars, you can access the variable. Example
# all variable
{{ hostvars[groups['groupname'][0]] }}
# the ansible of the first host of the group groupname
{{ hostvars[ groups['groupname'][0] ].ansible_host }}

Variable

Variable that are defined on the group level can be defined:

Inventory Variable

Example:

[group1:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

Variable file (group_vars directory)

Variables for a group may be defined in one file or in multiple file under a directory.

The location of this file is relative to the inventory file path or playbook file path.

Order of precedence if both paths exist:

  • playbook directory (higher priority - chosen first )
  • inventory directory

Syntax:

($INVENTORY_DIR|$PLAYBOOK_DIR)/group_vars/group1.yml # can optionally end in '.yaml', or '.json' or no file extension
# or in a directory structure
($INVENTORY_DIR|$PLAYBOOK_DIR)/group_vars/group1/db_settings.yml
($INVENTORY_DIR|$PLAYBOOK_DIR)/group_vars/group1/cluster_settings

Example with the default inventory location: /etc/ansible/hosts, the structure would be

/etc/ansible/group_vars/group1.yml
# or in a directory structure
/etc/ansible/group_vars/group1/db_settings.yml
/etc/ansible/group_vars/group1/cluster_settings

with the file

---
ntp_server: acme.example.org
database_server: storage.example.org





Discover More
Card Puncher Data Processing
Ansible - Ad-hoc command

Adhoc command are command executed as in the shell via ansible. You can therefore execute command on a whole cluster of server. This is because the default module of the ansible command line is command...
Card Puncher Data Processing
Ansible - Ansible-vault

ansible-vault is a command line utility that permits to add/get sensitive data (file or property value) into an encrypted format called a vault Example of sensitive data: password private keys ...
Card Puncher Data Processing
Ansible - Child Group (Children|Group of Group)

A group of a group is called a child group. The relation is defined through the use of the children key word. Any host that is member of a child group is automatically a member of the parent group....
Card Puncher Data Processing
Ansible - Host (system)

A host may have zero or more than one group (ie webserver and a dbserver). In a inventory file as playbook variable. Example Variable that are defined on the group level can be defined:...
Card Puncher Data Processing
Ansible - Inventory

inventory is a file that define the following entity: the hosts the group of host the child relationship between group and variables (connection variable,...). The preferred practice in Ansible...
Card Puncher Data Processing
Ansible - Playbook

Playbook is the language of Ansible. Ansible modules are the function Playbooks are declarative instruction written in Yaml that run module functions A playbook is a list of play. Playbooks are:...
Card Puncher Data Processing
Playbook Variable

This article is variable inside a playbook, task, ... Single value Boolean Block of text Order...



Share this page:
Follow us:
Task Runner