Ansible lets you automate IT tasks like installing software, configuring servers, and managing infrastructure—all from the command line. With just a few lines of YAML or CLI commands, you can deploy applications, patch systems, and orchestrate complex workflows without scripting languages. This guide shows you exactly how to run Ansible directly on the command line, from installing the tool to executing playbooks and troubleshooting errors.
What You'll Need
- Ansible installed on your Linux, macOS, or Windows machine (via pip or direct download).
- A local or remote server (Linux/Unix preferred) or a Windows host with SSH access enabled.
- A text editor or IDE to write playbooks (like Vi, Emacs, VS Code, or Sublime Text).
- Either an Ansible control machine (your local PC) or a dedicated management server with known credentials.
Step-by-Step
-
Install Ansible.
Use pip to install the latest version:
pip install ansibleOn macOS, run:brew install ansibleFor Windows (WSL recommended), download from Ansible’s official site or use the Python package manager. -
Verify installation.
Run:
ansible --versionCheck the output for the installed version (e.g.,ansible 2.15.7). -
Create an inventory file.
This maps your hosts (servers) to Ansible. Create a file (e.g.,
hosts.ini) with:[servers] server1 ansible_host=192.168.1.100 ansible_user=admin server2 ansible_host=192.168.1.101 ansible_user=rootReplace192.168.1.100with your server’s IP andadmin/rootwith credentials. -
Write a basic playbook.
Save a YAML file (e.g.,
deploy_webapp.yml--- - name: Install and start a web server hosts: servers tasks: - name: Install Apache ansible.builtin.apt: name: apache2 state: present when: ansible_os_family == 'Debian' - name: Start Apache ansible.builtin.service: name: apache2 state: started -
Test connectivity to hosts.
Run:
ansible all -i hosts.ini -m pingThis checks if Ansible can reach each server. If errors appear, verify SSH access or fix host credentials. -
Execute the playbook.
Run:
ansible-playbook -i hosts.ini deploy_webapp.ymlFor verbose output, add-vvv.
Common Mistakes
-
Missing SSH credentials.
Ensure SSH is enabled on remote servers (run
ssh-keygenlocally, then copy~/.ssh/id_rsa.pubto the server under~/.ssh/authorized_keys). -
Incorrect inventory file.
Double-check hostnames/IPs and username/passwords in
hosts.ini. Useansible all -i hosts.ini --list-hoststo validate. -
Ansible not in PATH.
On Windows, ensure Ansible is added to PATH during installation. On Unix, use full paths like
/usr/local/bin/ansible-playbook.
Tips
-
Use variables for flexibility.
Define variables in
host_varsor inline withvars:in the playbook:--- - name: Deploy with a custom port hosts: servers vars: http_port: 8080 tasks: - name: Configure Apache ansible.builtin.template: src: apache.conf.j2 dest: /etc/apache2/ports.conf -
Leverage ad-hoc commands.
Run single tasks without playbooks:
ansible all -i hosts.ini -m apt -a "name=nginx state=present" --become(Use--becomefor sudo/passwordless access.) -
Monitor progress.
Redirect output to a log file:
ansible-playbook deploy_webapp.yml -i hosts.ini > deploy.log
Troubleshooting
-
Connection refused.
Verify SSH is running on the remote server (check with
sudo systemctl status sshon Linux). If SSH is disabled, enable it viasudo ufw allow 22. -
Playbook fails on task X.
Isolate the issue by running a single task:
ansible-playbook -i hosts.ini deploy_webapp.yml -t "task_name" --limit servers -
Timeout errors.
Increase the connection timeout in the inventory:
[servers] server1 ansible_host=192.168.1.100 ansible_connection=ssh ansible_ssh_private_key_file=~/.ssh/id_rsa ansible_ssh_common_args="-o ConnectTimeout=30"
Frequently Asked Questions
How do I run Ansible on Windows?
Ansible works best on Linux/macOS via WSL2. On native Windows: 1. Install Python and thepywinrm package: pip install pywinrm.
2. Configure the inventory to use WinRM:
[windows_servers] pc1 ansible_host=192.168.1.100 ansible_connection=winrm ansible_user=admin ansible_winrm_transport=ntlmEnable WinRM on the target:
Enable-PSRemoting -Force.
Can I run Ansible without SSH?
No. SSH (or WinRM on Windows) is required for remote execution. Local playbooks (hosts: localhost) run viaansible localhost -m ping.
How do I debug a failed playbook?
Useansible-playbook -i hosts.ini deploy_webapp.yml -vv to get detailed logs. Check modules with ansible-doc module_name for usage examples.
What if my server doesn’t support apt?
Replace the Debian task with YUM/DNF for RHEL/CentOS:ansible.builtin.yum:
name: httpd
state: present
How do I encrypt sensitive data (passwords, keys)?
Use Ansible Vault: 1. Encrypt a file:ansible-vault encrypt secret.yml
2. Run with decryption: ansible-playbook -i hosts.ini deploy_webapp.yml (password prompt required).
Can I run Ansible on a cloud provider (AWS, Azure) without SSH?
AWS/Azure SSH keys can be automated via temporary credentials or IAM roles. For minimal setup, use AWS EC2’s default SSH keys or Azure’saz login to manage credentials securely.