Programster's Blog

Tutorials focusing on Linux, programming, and open-source

The Ansible Inventory/Hosts File

  1. File Location
  2. Groups
  3. Specify SSH User
  4. Specify SSH Keys
  5. Specify SSH Ports

File Location

The file may be found in different places depending on how you installed ansible. If you installed through the in-built package management system or a PPA, then the location is probably:

/etc/ansible/hosts

On Debian 12 after having installed the ansible package, the /etc/ansible folder did not exist, so I had to manually create that first.

However, I have found that when I installed through PIP on Debian a long time ago, it was at:

$HOME/ansible/ansible_hosts

Formats

This file can be in various formats, with the tutorial below first covering how it can be setup using ini format, and then the yaml equivalent. No matter which format you use the file must be named the same. E.g. do not stick a .ini or .yaml extension on it.

Groups

The hosts file can categorize servers into groups for our playbooks. For example, it may be useful to create a group for all of the apt based servers (ubuntu/debian etc), called apt-servers for a playbook that wishes to update apt-based servers. A server can belong to any number of groups and a group is simply defined by putting square brackets around the name for the group, before then listing the various servers' IP addresses or hostnames on subsequent lines like so:

[apt_servers]
server1.programster.org
server2.programster.org
server3.programster.org

[group2]
server2.programster.org
server4.programster.org

Ansible doesn't like the user of - in your group names, so use camelCase or snake_case when creating group names.

YAML Format

Alternatively, if you wish to use the YAML format, the above would be:

apt_servers:
  hosts:
    server1.programster.org:
    server2.programster.org:
    server3.programster.org:

group2:
  hosts:
    server2.programster.org:
    server4.programster.org:

Specify SSH User

By default, the user will be the same as the user that is calling the ansible script. To set the user to something different, then you can specify it like so:

apt_servers:
  hosts:
    server1.programster.org:
      ansible_user: myRemoteUser

Specify SSH Keys

If your ansible server needs to use different SSH keys to connect to the various servers, then you will also need to specify the key on the relevant line:

[amazon_linux_servers]
site1.programster.org ansible_ssh_private_key_file=/home/admin/key1.pem
site2.programster.org ansible_ssh_private_key_file=/home/admin/key2.pem
site3.programster.org ansible_ssh_private_key_file=/home/admin/key3.pem

YAML Format

The above in YAML format would be:

amazon_linux_servers:
  hosts:
    site1.programster.org:
        ansible_ssh_private_key_file: /home/admin/key1.pem
    site2.programster.org:
        ansible_ssh_private_key_file: /home/admin/key2.pem
    site3.programster.org:
        ansible_ssh_private_key_file: /home/admin/key3.pem

Specify SSH PORTs

If the remote server needs to be connected on a port other than 22, you can specify it after the hostname with a colon. The example below connects on port 2222.

[amazon_linux_servers]
site1.programster.org:2222

Alternatively, you can use ansible_port=2222 like so:

[amazon_linux_servers]
site1.programster.org    ansible_port=2222

YAML Format

The above in YAML format would be:

amazon_linux_servers:
  hosts:
    site1.programster.org:
        ansible_port: 2222
Last updated: 19th January 2025
First published: 16th August 2018

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch