In simple words
- Inventory: the list of servers Ansible manages, sorted into groups such as web and db.
- Group: a named set of hosts, so you can target many at once.
- Ad-hoc command: a quick one-off task from the command line, such as checking uptime on every server.
- Pattern: a way to pick which hosts to target, such as web:!web3.
Big picture
Before Ansible can work, it needs to know which servers to talk to. That list is the inventory. Once you have one, you can run one-off commands on many servers at once. These are called ad-hoc commands. Think of a group chat: the inventory is the member list, and an ad-hoc command is one message sent to everyone.
A simple inventory
[web]web1.example.comweb2.example.com[db]db1.example.com ansible_user=ubuntu[prod:children]webdb| Part | Meaning |
|---|---|
[web] | A group of servers |
ansible_user=ubuntu | A variable just for that server |
[prod:children] | A group made of other groups |
You can also write inventory in YAML. Both work. INI is shorter for beginners.
all: children: web: hosts: web1.example.com: web2.example.com: db: hosts: db1.example.com: ansible_user: ubuntuSpecial groups
allis every server.ungroupedis every server not in a group.
Check your inventory
ansible-inventory -i inventory.ini --graph@all: |--@prod: | |--@db: | | |--db1.example.com | |--@web: | | |--web1.example.com | | |--web2.example.comAd-hoc commands
The shape is: ansible WHO -m MODULE -a "ARGUMENTS".
ansible web -i inventory.ini -m pingansible all -i inventory.ini -m command -a "uptime"ansible web -i inventory.ini -m apt -a "name=nginx state=present" --becomeweb1.example.com | SUCCESS => { "changed": false, "ping": "pong" }web2.example.com | SUCCESS => { "changed": false, "ping": "pong" }| Flag | Meaning |
|---|---|
-i | which inventory file |
-m | which module |
-a | arguments for the module |
--become | run as root (using sudo) |
-u | which SSH user |
--limit web1 | only some of the servers |
Pick servers with patterns
The first word after ansible is a pattern. You can combine groups.
| Pattern | Meaning |
|---|---|
web | the web group |
web:db | web or db |
prod:&web | in prod and in web |
all:!db | everyone except db |
web1* | names that start with web1 |
ansible 'all:!db' -i inventory.ini -m ping --list-hosts hosts (2): web1.example.com web2.example.com--list-hosts shows who would be picked, without running anything.
When to use ad-hoc commands
Good for quick jobs: check disk space, restart a service, see if servers answer. For anything you will repeat, write a playbook instead.
Common mistakes
- Forgetting
--becomefor jobs that need root. - Thinking
-m pingis a network ping. It is not. It checks that Ansible can log in over SSH and run Python. - SSH key not set up. Test with plain
ssh user@hostfirst.
Summary
- The inventory lists servers and groups them.
- Ad-hoc commands run one module on many servers.
ansible-inventory --graphshows what Ansible sees.
CHECK YOUR UNDERSTANDING
3 quick questions
1. What does an inventory contain?
2. Which command checks that Ansible can reach the web group?
3. What does --become do?
You've finished the free preview of Ansible
Upgrade to Pro to unlock the remaining 15 lessons, every practice lab and a certificate when you finish.
See plans