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.
node1
node2
node1.example.com
node2.example.com
192.168.200.100
192.168.200.200You can also define ranges, e.g. 192.168.200.[1:10] Use ansible all --list to view defined hosts.
Host group definition
Groups are defined with a name in brackets.
[webserver]
node1
node2List group members with ansible webserver --list.
Nested groups
Groups can contain other groups using the :children suffix.
[MachineRoom:children]
webserver
sqlserverNow ansible MachineRoom --list returns all hosts in both groups.
Select Hosts and Groups
Match specific hosts or groups, use commas to combine, or use wildcards.
ansible 192.168.1.1 --list
ansible webserver --list
ansible 192.168.1.1,webserver --list
ansible *.example.com --list
ansible '*.example.com,!node2*' --listConfiguration 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 devops with password 123 and configure password‑less sudo:
# useradd devops
# echo 123|passwd --stdin devops
# echo "devops ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/devopsGenerate SSH keys for devops and copy them to managed nodes:
# su - devops
$ ssh-keygen
$ ssh-copy-id node1
$ ssh-copy-id node2Copy the Ansible configuration to the user’s home, adjust ansible.cfg to use the local hosts file and the devops sudo user:
# 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 = FalseWrite the host inventory:
node1
node2Verify the setup with ansible all --list and run a test command: ansible all -m shell -a 'whoami' The output shows both nodes returning root, confirming that the configuration and privilege escalation work correctly.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
