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

  1. Verify Ansible is installed and functional by running ansible --version in your terminal. You should see version info and the Python path. If not, install Ansible via pip install ansible or your OS package manager (e.g., sudo apt install ansible on Ubuntu).
  2. 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.
  3. Test SSH access manually. Run ssh username@host (e.g., ssh admin@192.168.1.50). If this fails, configure SSH keys using ssh-copy-id username@host or ensure password authentication is enabled on the target.
  4. Decide whether to use an inventory file or run ad-hoc without one. For simple cases, you can avoid a file by using the -i flag 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.
  5. 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, webserver is the pattern matching the host alias (you can also use all or 192.168.1.50 directly as the pattern).
  6. 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 --limit flag ensures only the given host runs, even if the playbook targets multiple hosts.
  7. Optionally, override connection parameters inline. Add -u for user, -k to prompt for password (if SSH keys fail), or -e ansible_port=2222 for 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 server1 fails, Ansible won’t connect either. Add entries to /etc/hosts or fix DNS.
  • Assuming SSH keys are already set up. Ansible defaults to key-based auth; if keys aren’t configured, use -k to 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 ping before complex tasks wastes time debugging deeper issues.

Tips

  • Use the -v flag (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 run ansible 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=false to skip SSH host key prompts (add to ~/.ansible.cfg for persistence).
  • Use -C (check mode) first: ansible host -i "host," -m shell -a "command" -C to simulate changes without applying them.

Troubleshooting

If your command fails to connect or execute, follow these steps in order:

  1. Check the error message. Common errors include "UNREACHABLE" (network/SSH issue), "FAILED" (command execution error), or "no hosts matched" (inventory/pattern mismatch).
  2. Verify SSH access. Run ssh -v user@host to see verbose debug output. Look for authentication failures, permission denied, or connection refused.
  3. 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.
  4. 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.
  5. Confirm user permissions. Does the remote user have sudo rights if the task requires elevated privileges? Use -b with -K to prompt for sudo password if needed.
  6. Review firewall and SELinux. If SSH works manually but Ansible fails, check if SELinux is blocking connections (temporarily disable with setenforce 0 for testing) or if a firewall blocks port 22.
  7. Test with a simple module. Replace shell with debug or setup to 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.

Do I need to define the host in an inventory file to use it ad-hoc?

No. The whole point of command-line specification is to avoid maintaining inventory files for quick tasks. However, for repeatable, complex setups, inventory files are recommended for consistency.