Ansible - Host (system)

Card Puncher Data Processing

About

A host may have zero or more than one group (ie webserver and a dbserver).

Management

Definition

Inventory file

hostName

; With regexp, you can add a lot of hosts
www[01:50].example.com
db-[a:f].example.com

playbook

  • as playbook variable. Example
- name: Wait 300 seconds for port 22 to become open and contain "OpenSSH"
  wait_for:
    port: 22
    host: '{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}'
    search_regex: OpenSSH
    delay: 10
  connection: local

List

ansible-playbook playbook.yml --list-hosts

Variable

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

Inventory Variable

Example:

host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909

Variable file (host_vars directory)

Variables for a host 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)/host_vars/host1.yml # can optionally end in '.yaml', or '.json' or no file extension
# or in a directory structure
($INVENTORY_DIR|$PLAYBOOK_DIR)/host_vars/host1/db_settings.yml
($INVENTORY_DIR|$PLAYBOOK_DIR)/host_vars/host1/cluster_settings

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

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

with the file

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

Built-in hostvars variable

At runtime, you can access the variable with the hostvars variable

Example:

{{ hostvars.alias.ansible_host }}

Built-in variable

Inventory

  • inventory_hostname is the name of the hostname as configured in Ansible’s inventory host file
  • inventory_hostname_short - the part up to the first period, without the rest of the domain.
[group]
inventory_hostname blablabla

Connection

Connection:

  • ansible_host : the name to connect to

Fact

Fact

You can see them by running the setup module with the ansible command line

ansible -i inventories.yml -m setup all

Extract the domain

Example if you define the ansible_host with the following full qualified name hostname.example.com

{{ ansible_host.split('.', 1)[1] }}

With ansible

ansible -i inventory.yml -m debug -a "msg={{ ansible_host.split('.', 1)[1] }}" all
example.com

Extract the hostname

Example if you define the ansible_host with the following full qualified name hostname.example.com

{{ ansible_host.split('.', 1)[0] }}

With ansible

ansible -i inventory.yml -m debug -a "msg={{ ansible_host.split('.', 1)[0] }}" all
example.com





Discover More
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 - Connection

Connection parameters to hosts are given through variable. You can define the running user with the help of this two variable: ansible_user ( in a inventory file) remote_user in a Playbook ansible_userremote_userbecomeansible/ansible/issues/2004520045...
Card Puncher Data Processing
Ansible - Debug module

debug module diagnostic page With the ansible command line Example to get the ansible_host value where: ansible command line inventory is an inventory file -m set the module to debug -a...
Card Puncher Data Processing
Ansible - Group (Host Properties)

A host can have one or more group (tag). A group may have also have a group. See Groups don’t really survive outside of inventory and host matching because variables are defined to a specific host...
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
Playbook Variable

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



Share this page:
Follow us:
Task Runner