Skip to content
$ lynuxpro
← Ansible

LESSON 2 OF 17 · BEGINNER · 10 MIN

Inventory and ad-hoc commands

Free previewLesson 2 of the 2 free lessons in this Pro course.Unlock all 17

HOW IT WORKSOne command to many servers

inventory.ini[web] web1 web2ansible web -m pingweb1web2db1not in group
1/5

The inventory groups your servers.

[web]
web1
web2

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
NORMALconfutf-8 · 10L
PartMeaning
[web]A group of servers
ansible_user=ubuntuA 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: ubuntu
NORMALyamlutf-8 · 10L

Special groups

  • all is every server.
  • ungrouped is every server not in a group.

Check your inventory

ansible-inventory -i inventory.ini --graph
SHELLbashutf-8 · 1L
@all:  |--@prod:  |  |--@db:  |  |  |--db1.example.com  |  |--@web:  |  |  |--web1.example.com  |  |  |--web2.example.com
OUTPUTterminalutf-8 · 7L

Ad-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" --become
SHELLbashutf-8 · 3L
web1.example.com | SUCCESS => { "changed": false, "ping": "pong" }web2.example.com | SUCCESS => { "changed": false, "ping": "pong" }
OUTPUTterminalutf-8 · 2L
FlagMeaning
-iwhich inventory file
-mwhich module
-aarguments for the module
--becomerun as root (using sudo)
-uwhich SSH user
--limit web1only some of the servers

Pick servers with patterns

The first word after ansible is a pattern. You can combine groups.

PatternMeaning
webthe web group
web:dbweb or db
prod:&webin prod and in web
all:!dbeveryone except db
web1*names that start with web1
ansible 'all:!db' -i inventory.ini -m ping --list-hosts
SHELLbashutf-8 · 1L
  hosts (2):    web1.example.com    web2.example.com
OUTPUTterminalutf-8 · 3L

--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 --become for jobs that need root.
  • Thinking -m ping is 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@host first.

Summary

  • The inventory lists servers and groups them.
  • Ad-hoc commands run one module on many servers.
  • ansible-inventory --graph shows what Ansible sees.

CHECK YOUR UNDERSTANDING

3 quick questions

  1. 1. What does an inventory contain?

  2. 2. Which command checks that Ansible can reach the web group?

  3. 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