Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
ansible web -i inventory.ini -m ansible.builtin.command -a 'df -h /'
ansible all -i inventory.ini -b -m ansible.builtin.shell -a 'uname -r; uptime'
ansible web -i inventory.ini -b -m ansible.builtin.service -a 'name=chronyd state=restarted' --limit web1.example.com-b = become root; --limit = only these hosts (test on one first).
Step by step
- An inventory with groups (see the Ansible how-to) and working SSH keys. Test:
ansible all -i inventory.ini -m ansible.builtin.ping -o-o = one line per host — easy to read for many servers.
- Read-only questions —
command(no shell features) orshell(pipes,;,>):
ansible all -i inventory.ini -o -m ansible.builtin.command -a 'cat /etc/redhat-release'
ansible all -i inventory.ini -b -m ansible.builtin.shell -a "ss -ltn | grep -c LISTEN"
ansible all -i inventory.ini -m ansible.builtin.setup -a 'filter=ansible_kernel' -o- Changes — with a module, not a shell command (modules are idempotent and show
changed):
ansible web -i inventory.ini -b -m ansible.builtin.service -a 'name=nginx state=reloaded'
ansible all -i inventory.ini -b -m ansible.builtin.copy -a 'src=./motd dest=/etc/motd mode=0644'- Safety rules: first
--limitone host, then the group;--checkwhere the module supports it; for risky changes use--forks 5(5 at a time) or a playbook withserial:. - No Ansible? A plain SSH loop —
BatchMode(never hang on a password prompt), a timeout, and the host name in front of each line:
for h in web1 web2 web3; do ssh -o BatchMode=yes -o ConnectTimeout=5 "$h" 'uptime' | sed "s/^/$h: /"; done- In parallel with
xargs -P:
Check that it worked
Every host answers (no UNREACHABLE or FAILED in the summary), and for changes a second run shows changed=0 / SUCCESS everywhere.
Common problems
| Problem | Cause | Fix |
|---|---|---|
UNREACHABLE … Permission denied (publickey) | No key on that host or wrong user. | Fix SSH keys first; ansible_user in the inventory. |
A pipe or > does nothing with -m command | command has no shell. | Use -m ansible.builtin.shell. |
| Output is hard to read for 50 hosts | Multi-line output per host. | -o (one line), or write results to files (--tree). |
| SSH loop hangs | A host asks for a password or a host key. | -o BatchMode=yes, ConnectTimeout; accept host keys first. |
| You restarted a service everywhere at once | No --limit, no rolling. | Test on one; --forks small, or a playbook with serial. |
sudo: a password is required | become needs a password. | -K (ask for it) or passwordless sudo for the automation user. |
Go further
- Command pages: ansible, ssh, xargs.
- Next how-tos: Install a package on many servers with Ansible, Log in with SSH keys, Write a safe Bash script.
- Full guide: Install Ansible. At work: Ansible in daily DevOps.
- Practise: Lab: automate a web server with Ansible.
1When do you use -m shell instead of -m command?
When you need shell features like pipes, ; or redirects.
2Why use -o BatchMode=yes in an SSH loop?
SSH never waits for a password — a host without key access fails fast instead of hanging the loop.