Run Quick Tasks Without Playbooks
Beginner5 min
Execute one-off commands across multiple hosts using ansible ad-hoc commands for quick checks, updates, and troubleshooting.
Prerequisites
- -Ansible installed
- -Inventory file configured
- -SSH access to hosts
Steps
1
Ping all hosts
Verifies SSH connectivity and Python availability on all managed hosts.
$ ansible all -m ping
2
Run a shell command
Checks disk usage on the root filesystem across all webservers.
$ ansible webservers -m shell -a "df -h / | tail -1"
Use the 'shell' module for commands with pipes or redirects. Use 'command' for simple commands.
3
Copy a file to all hosts
Distributes a file to all hosts with root privileges.
$ ansible all -m copy -a "src=./motd.txt dest=/etc/motd" --become
4
Install a package
Installs htop on all webservers using the apt module.
$ ansible webservers -m apt -a "name=htop state=present update_cache=true" --become
5
Gather system facts
Retrieves distribution-related facts (OS name, version, release) from a single host.
$ ansible web01 -m setup -a "filter=ansible_distribution*"
Full Script
FAQ
Discussion
Loading comments...