Master Ansible Inventory: Define Hosts, Groups, and Configurations Step‑by‑Step
This guide walks you through creating and organizing Ansible inventory files, defining hosts and host groups, using nested groups and wildcards, understanding configuration file priority, and setting up a devops user with password‑less sudo and SSH keys to verify the setup.
Define Hosts and Groups
When using Ansible to manage hosts in bulk, you first define the hosts or host groups in an inventory file, typically /etc/ansible/hosts.
Host definition
Hosts can be defined by hostname, domain name, or IP address.
<code>node1
node2
node1.example.com
node2.example.com
192.168.200.100
192.168.200.200</code>You can also define ranges, e.g.
<code>192.168.200.[1:10]</code>Use
ansible all --listto view defined hosts.
Host group definition
Groups are defined with a name in brackets.
<code>[webserver]
node1
node2</code>List group members with
ansible webserver --list.
Nested groups
Groups can contain other groups using the
:childrensuffix.
<code>[MachineRoom:children]
webserver
sqlserver</code>Now
ansible MachineRoom --listreturns all hosts in both groups.
Select Hosts and Groups
Match specific hosts or groups, use commas to combine, or use wildcards.
<code>ansible 192.168.1.1 --list
ansible webserver --list
ansible 192.168.1.1,webserver --list
ansible *.example.com --list
ansible '*.example.com,!node2*' --list</code>Configuration file priority
Ansible reads configuration from four locations in order: ANSIBLE_CONFIG environment variable, ./ansible.cfg, ~/.ansible.cfg, /etc/ansible/ansible.cfg.
Configuration file details
Key sections include
[defaults],
[inventory],
[privilege_escalation], etc. Important parameters:
inventory = ./hosts
remote_user = root
ask_pass = false
become = true
become_method = sudo
become_user = root
become_ask_pass = false
Example setup
Create a regular user
devopswith password
123and configure password‑less sudo:
<code># useradd devops
# echo 123|passwd --stdin devops
# echo "devops ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/devops</code>Generate SSH keys for
devopsand copy them to managed nodes:
<code># su - devops
$ ssh-keygen
$ ssh-copy-id node1
$ ssh-copy-id node2</code>Copy the Ansible configuration to the user’s home, adjust
ansible.cfgto use the local
hostsfile and the
devopssudo user:
<code># cp -r /etc/ansible/ .
# edit ansible.cfg:
[defaults]
inventory = ./hosts
sudo_user = devops
ask_sudo_pass = False
ask_pass = False
host_key_checking = False
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False</code>Write the host inventory:
<code>node1
node2</code>Verify the setup with
ansible all --listand run a test command:
<code>ansible all -m shell -a 'whoami'</code>The output shows both nodes returning
root, confirming that the configuration and privilege escalation work correctly.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.