Understanding ansible all -m ping
ansible all -m ping| Part | Meaning |
|---|---|
ansible | CLI |
all | Every host in inventory |
-m ping | Run the ping module |
ping does not use ICMP. It opens SSH and runs a tiny Python check — proof Ansible can automate that host.
Default inventory location
Historically:
/etc/ansible/hosts
Better for projects: a local inventory next to ansible.cfg (next lesson).
mkdir -p ~/ansible-project
cd ~/ansible-project# inventory.ini
[web]
54.179.68.157
54.254.115.55
[web:vars]
ansible_user=ubuntuansible-inventory -i inventory.ini --graph
ansible all -i inventory.ini -m ping
ansible web -i inventory.ini -m ping
ansible-inventory -i inventory.ini --listGroups
[web]
54.179.68.157
54.254.115.55
[db]
13.200.10.3Target only one group: ansible web -m ping.
Host variables vs group variables
Host variables — one host:
[web]
web1 ansible_host=54.179.68.157 app_name=frontend
web2 ansible_host=54.254.115.55 app_name=backendGroup variables — every host in the group:
[web:vars]
ansible_user=ubuntu
env=prodPriority
Host vars override group vars for the same key.
| Type | Scope | Priority |
|---|---|---|
| Host variables | Single host | Higher |
| Group variables | Whole group | Lower |
YAML inventory example
all:
children:
web:
hosts:
web1:
ansible_host: 54.179.68.157
app_name: frontend
web2:
ansible_host: 54.254.115.55
app_name: backend
vars:
ansible_user: ubuntuStatic vs dynamic inventory
| Static | Dynamic |
|---|---|
Hand-written inventory.ini | Queried from AWS/Azure/GCP APIs |
| Simple for labs / small fleets | Tracks autoscaling automatically |
| Manual IP updates | Needs cloud credentials + plugins |
Static: Ansible → inventory.ini → hosts
Dynamic: Ansible → AWS API → live instances
Startups with fixed servers often use static. Enterprises with autoscaling prefer dynamic (covered in Day 5).
Prefer project-local inventories over editing /etc/ansible/hosts so each repo carries its own host list.