Ansible lets you run ad-hoc commands or playbooks against specific hosts by specifying them directly on the command line—bypassing inventory groups. This is useful for targeting a single server during debugging, one-off tasks, or when you don’t want to modify your inventory file. You’ll need Ansible installed and a working SSH connection to the target host (with either passwordless keys or access to enter a password when prompted).
What You'll Need
- Ansible installed on your control node (local machine or jump host)
- Valid SSH credentials to the target host (password or SSH key)
- Target host reachable over the network (pingable, SSH port 22 open by default)
- Basic familiarity with the command line
Step-by-Step
- Verify Ansible is installed and functional by running
ansible --versionin your terminal. You should see version info and the Python path. If not, install Ansible viapip install ansibleor your OS package manager (e.g.,sudo apt install ansibleon Ubuntu). - Ensure your target host is reachable. From your control node, run
ping <hostname_or_ip>(e.g.,ping 192.168.1.50). If ping fails, check network connectivity, firewall rules, or DNS resolution. - Test SSH access manually. Run
ssh username@host(e.g.,ssh admin@192.168.1.50). If this fails, configure SSH keys usingssh-copy-id username@hostor ensure password authentication is enabled on the target. - Decide whether to use an inventory file or run ad-hoc without one. For simple cases, you can avoid a file by using the
-iflag with an inline host list. For example:ansible all -i "192.168.1.50," -m ping. Note the trailing comma after the IP—this tells Ansible the inventory source is a comma-separated list, not a file. - To run a command against a single host, use the pattern syntax after the inventory. For instance:
ansible webserver -i "192.168.1.50," -m shell -a "uptime". Here,webserveris the pattern matching the host alias (you can also useallor192.168.1.50directly as the pattern). - For playbook execution, specify the host pattern in the command. Example:
ansible-playbook deploy.yml -i "192.168.1.50," --limit "192.168.1.50". The--limitflag ensures only the given host runs, even if the playbook targets multiple hosts. - Optionally, override connection parameters inline. Add
-ufor user,-kto prompt for password (if SSH keys fail), or-e ansible_port=2222for non-standard ports. Example:ansible dbhost -i "10.0.0.25," -u deploy -k -m ping.
Common Mistakes
- Forgetting the trailing comma in inline inventories (e.g.,
-i "192.168.1.50"instead of-i "192.168.1.50,") causes Ansible to treat it as a file path, resulting in a "file not found" error. - Using hostnames that don’t resolve locally. If
ping server1fails, Ansible won’t connect either. Add entries to/etc/hostsor fix DNS. - Assuming SSH keys are already set up. Ansible defaults to key-based auth; if keys aren’t configured, use
-kto prompt for password authentication. - Typing the wrong pattern. If your playbook targets
[webservers]but you specify-i "192.168.1.50," --limit "webserver"(missing the 's'), Ansible won’t match any hosts and skip execution silently. - Running commands without verifying connectivity first. Skipping
ansible all -i "host," -m pingbefore complex tasks wastes time debugging deeper issues.
Tips
- Use the
-vflag (e.g.,ansible -v) to get verbose output and confirm exactly which hosts Ansible is targeting. - For repeated use of the same host, define a temporary inventory file:
echo "192.168.1.50" > /tmp/temp_inv, then runansible host -i /tmp/temp_inv .... - Combine multiple hosts inline:
-i "192.168.1.50,10.0.0.25,"—useful for quick multi-host tests. - Set environment variables like
ANSIBLE_HOST_KEY_CHECKING=falseto skip SSH host key prompts (add to~/.ansible.cfgfor persistence). - Use
-C(check mode) first:ansible host -i "host," -m shell -a "command" -Cto simulate changes without applying them.
Troubleshooting
If your command fails to connect or execute, follow these steps in order:
- Check the error message. Common errors include "UNREACHABLE" (network/SSH issue), "FAILED" (command execution error), or "no hosts matched" (inventory/pattern mismatch).
- Verify SSH access. Run
ssh -v user@hostto see verbose debug output. Look for authentication failures, permission denied, or connection refused. - Test with the ping module. Run
ansible host -i "host," -m ping. A successful response confirms basic connectivity; failure points to SSH or inventory issues. - Check inventory syntax. Ensure inline inventories use a comma after the last host. For files, confirm the file has no trailing spaces or invalid characters.
- Confirm user permissions. Does the remote user have sudo rights if the task requires elevated privileges? Use
-bwith-Kto prompt for sudo password if needed. - Review firewall and SELinux. If SSH works manually but Ansible fails, check if SELinux is blocking connections (temporarily disable with
setenforce 0for testing) or if a firewall blocks port 22. - Test with a simple module. Replace
shellwithdebugorsetupto isolate module-specific problems:ansible host -i "host," -m debug -a "var=ansible_hostname".
Frequently Asked Questions
Can I specify multiple hosts on the command line without an inventory file?
Yes. Use a comma-separated list in the-i flag, with a trailing comma. Example: ansible all -i "host1,host2,host3," -m ping. Each host can be an IP, hostname, or FQDN.
Does specifying a host on the command line override inventory groups?
Yes. When you use-i with an inline list or file, Ansible uses only that inventory source. Any groups defined in your default /etc/ansible/hosts are ignored for that command.
Can I use hostnames instead of IP addresses?
Absolutely. As long as the hostname resolves (via DNS or/etc/hosts), Ansible will connect. Example: ansible dbserver -i "dbserver.local," -m shell -a "df -h".
What if my target uses a non-standard SSH port?
Specify the port inline using an environment variable:ansible host -i "host," -e ansible_port=2222 -m ping. Alternatively, add the port in the host entry: -i "host:2222,".
Why does my command run but skip hosts?
This usually means the host pattern doesn’t match any entries in your inventory. Double-check spelling, case, and whether you’re using the correct pattern (e.g.,web* vs web1). Use -v to see which hosts were included/excluded.