Inventory and Ad-Hoc Commands
Organizing servers into groups, group and host variables, dynamic cloud inventories, quick ad-hoc commands, and ansible.cfg.
The inventory
The inventory is Ansible's list of servers, organized into groups such as "Oracle servers" or "production". Every Ansible command targets a group, a server, or a combination, so a well-organized inventory is what lets you say "patch the test Oracle servers" and have Ansible know exactly which machines that means.
Inventories can be written in INI or YAML. Here's Kofi's team's, in YAML, with servers grouped two ways: by database engine, and by environment.
# inventory/hosts.yml
all:
children:
oracle:
hosts:
db01.acme.local:
db02.acme.local:
db03.acme.local:
postgres:
hosts:
pg01.acme.local:
prod:
hosts:
db01.acme.local:
db02.acme.local:
test:
hosts:
db03.acme.local:
pg01.acme.local:
A server can be in many groups at once, and you target them with patterns:
| Pattern | Targets |
|---|---|
oracle | Every Oracle server |
oracle:&prod | Servers in both groups: production Oracle servers |
oracle:!prod | Oracle servers that aren't in production |
db01.acme.local | One server |
all | Everything in the inventory |
Group and host variables
Settings that differ between groups or servers go in folders next to the inventory, named after the group or host. Ansible loads them automatically:
inventory/
├── hosts.yml
├── group_vars/
│ ├── oracle.yml # oracle_base: /u01/app/oracle, oracle_version: 19c
│ ├── prod.yml # sga_target: 16G, backup_retention_days: 30
│ └── test.yml # sga_target: 4G, backup_retention_days: 7
└── host_vars/
└── db01.acme.local.yml # oracle_sid: SALES
The same playbook then configures each server with the right values for its groups: a production Oracle server gets the Oracle settings and the production ones.
Dynamic inventories
In the cloud, servers come and go, so a hand-written list goes out of date. A dynamic inventory asks the cloud which servers exist right now, and groups them by tags or other attributes. There are inventory plugins for OCI, AWS, Azure, VMware, and more:
# inventory/oci.yml: build the inventory from OCI, grouped by freeform tags
plugin: oracle.oci.oci
regions:
- us-ashburn-1
hostname_format_preferences:
- "private_ip"
keyed_groups:
- key: freeform_tags.role # a server tagged role=oracle joins group "oracle"
prefix: ""
separator: ""
This is how Terraform and Ansible fit together cleanly: Terraform creates servers with tags, such as role = oracle and environment = prod, and Ansible's dynamic inventory finds them by those tags, with no list to keep up to date.
Ad-hoc commands
For a quick, one-off task across many servers, you don't need a playbook. An ad-hoc command runs a single module from the command line:
# can Ansible reach and log in to every Oracle server?
$ ansible oracle -i inventory -m ansible.builtin.ping
db01.acme.local | SUCCESS => {
"changed": false,
"ping": "pong"
}
db02.acme.local | SUCCESS => ...
# free space on /u01, on every production database server
$ ansible prod -i inventory -m ansible.builtin.command -a "df -h /u01"
# is the listener running on the test Oracle servers? (as the oracle user)
$ ansible 'oracle:&test' -i inventory -b --become-user oracle \
-m ansible.builtin.shell -a ". ~/.bash_profile && lsnrctl status | grep -i uptime"
- -m and -a
- The module to run, and its arguments.
- -b (become)
- Run with higher privileges, using
sudoby default.--become-user oracleruns as theoracleuser instead. - Results
- Each server reports SUCCESS, CHANGED, FAILED, or UNREACHABLE, so problems on one server out of a hundred stand out.
ansible.cfg
An ansible.cfg file in the project folder sets defaults, so you don't repeat them on every command:
# ansible.cfg
[defaults]
inventory = inventory/
remote_user = ansible
forks = 20 # how many servers to work on at once
[privilege_escalation]
become_method = sudo
Key takeaways
- The inventory lists servers in groups, such as by engine and environment; a server can be in many groups.
- Patterns such as
oracle:&prodtarget exactly the servers you mean. group_varsandhost_varsgive each group and server its own settings.- Dynamic inventories ask the cloud which servers exist, grouping them by the tags Terraform set.
- Ad-hoc commands run one module across many servers: ideal for checks, while changes belong in playbooks.