Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 96IaC · Ansible

How to run one command on many servers

"What is the free disk on all web servers?", "Which kernel runs everywhere?", "Restart chrony on all 30 servers." — logging in to each one does not scale. This how-to runs one command on many servers in parallel: with Ansible ad-hoc commands (the standard way), and with a plain SSH loop when Ansible is not there. Plus the safety rules: read-only first, one server first, then all.

  • 5 Lessen
  • 5 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

96.1

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

96.2

Step by step

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

  1. Read-only questions — command (no shell features) or shell (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
  1. 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'
  1. Safety rules: first --limit one host, then the group; --check where the module supports it; for risky changes use --forks 5 (5 at a time) or a playbook with serial:.
  2. 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
  1. In parallel with xargs -P:
printf '%s\n' web1 web2 web3 db1 | xargs -P 10 -I{} ssh -o BatchMode=yes -o ConnectTimeout=5 {} 'echo "$(hostname): $(df -h / | tail -1)"'

96.3

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.

96.4

Common problems

ProblemCauseFix
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 commandcommand has no shell.Use -m ansible.builtin.shell.
Output is hard to read for 50 hostsMulti-line output per host.-o (one line), or write results to files (--tree).
SSH loop hangsA host asks for a password or a host key.-o BatchMode=yes, ConnectTimeout; accept host keys first.
You restarted a service everywhere at onceNo --limit, no rolling.Test on one; --forks small, or a playbook with serial.
sudo: a password is requiredbecome needs a password.-K (ask for it) or passwordless sudo for the automation user.

96.5

Go further

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.

Alle commando's in dit hoofdstuk