Ansible — Agentless Server Automation
Practical guide to Ansible — agentless IT automation over SSH, provisioning, deploying and configuring with YAML playbooks plus Vault for secrets.
Ansible is an agentless automation tool from Red Hat that lets you provision servers, configure them and roll out software – without running any agent on the managed hosts. Everything happens over plain SSH, and your desired state lives as code in declarative YAML playbooks. The name actually covers several binaries: ansible for ad-hoc commands, ansible-playbook for full workflows, plus ansible-galaxy, ansible-vault and ansible-inventory. This guide takes you from a quick ping through structured playbooks all the way to encrypted secrets management.
Ad-Hoc Commands
ansible <hosts> -m ping — Test connectivity to hosts.
ansible all -m pingansible <hosts> -a '<command>' — Run a shell command on remote hosts.
ansible webservers -a 'uptime'ansible <hosts> -m <module> -a '<args>' — Run a module with arguments.
ansible webservers -m apt -a 'name=nginx state=present' -bansible <hosts> -m copy -a 'src=<src> dest=<dest>' — Copy a file to remote hosts.
ansible all -m copy -a 'src=config.conf dest=/etc/app/config.conf' -bansible <hosts> -m service -a 'name=<svc> state=restarted' — Restart a service on remote hosts.
ansible webservers -m service -a 'name=nginx state=restarted' -bansible <hosts> -b -a '<command>' — Run command with sudo (become).
ansible all -b -a 'apt update'Playbooks
ansible-playbook <playbook> — Run a playbook.
ansible-playbook site.ymlansible-playbook <playbook> -i <inventory> — Run with a specific inventory file.
ansible-playbook deploy.yml -i production/hostsansible-playbook <playbook> --check — Dry run: show what would change without applying.
ansible-playbook site.yml --checkansible-playbook <playbook> --diff — Show file differences when making changes.
ansible-playbook site.yml --diffansible-playbook <playbook> -l <hosts> — Limit execution to specific hosts.
ansible-playbook site.yml -l webserver01ansible-playbook <playbook> -t <tags> — Run only tasks with specific tags.
ansible-playbook site.yml -t nginx,deployansible-playbook <playbook> -e '<key>=<value>' — Pass extra variables.
ansible-playbook deploy.yml -e 'version=2.0 env=production'Inventory
ansible-inventory --list — Show the full inventory in JSON format.
ansible-inventory --list -i hosts.ymlansible-inventory --graph — Show inventory as a tree graph.
ansible-inventory --graphansible <hosts> --list-hosts — List which hosts match a pattern.
ansible webservers --list-hostsansible-playbook <playbook> --list-tasks — List all tasks in a playbook.
ansible-playbook site.yml --list-tasksansible-playbook <playbook> --list-tags — List all tags in a playbook.
ansible-playbook site.yml --list-tagsGalaxy & Roles
ansible-galaxy init <role> — Create a new role directory structure.
ansible-galaxy init my-roleansible-galaxy install <role> — Install a role from Ansible Galaxy.
ansible-galaxy install geerlingguy.dockeransible-galaxy install -r requirements.yml — Install roles from a requirements file.
ansible-galaxy install -r requirements.ymlansible-galaxy collection install <collection> — Install an Ansible collection.
ansible-galaxy collection install community.dockeransible-galaxy list — List installed roles.
ansible-galaxy listVault (Secrets)
ansible-vault create <file> — Create a new encrypted file.
ansible-vault create secrets.ymlansible-vault edit <file> — Edit an encrypted file.
ansible-vault edit secrets.ymlansible-vault encrypt <file> — Encrypt an existing file.
ansible-vault encrypt vars/passwords.ymlansible-vault decrypt <file> — Decrypt an encrypted file.
ansible-vault decrypt secrets.ymlansible-vault view <file> — View an encrypted file without decrypting.
ansible-vault view secrets.ymlansible-playbook <playbook> --ask-vault-pass — Run playbook and prompt for vault password.
ansible-playbook site.yml --ask-vault-passansible-vault encrypt_string '<string>' --name '<var>' — Encrypt a single string for use in YAML.
ansible-vault encrypt_string 'mysecret' --name 'db_password'Debugging & Config
ansible-playbook <playbook> -v — Run with verbose output (-v, -vv, -vvv, -vvvv).
ansible-playbook site.yml -vvvansible-config dump — Show all configuration settings.
ansible-config dump --only-changedansible-doc <module> — Show documentation for a module.
ansible-doc ansible.builtin.aptansible-doc -l — List all available modules.
ansible-doc -l | grep dockeransible-playbook <playbook> --syntax-check — Check playbook syntax without running.
ansible-playbook site.yml --syntax-checkansible-playbook <playbook> --step — Run playbook step by step with confirmation.
ansible-playbook site.yml --step Conclusion
The same toolbox covers both worlds: quick ad-hoc fixes and reproducible playbooks versioned in YAML – idempotent, so running them repeatedly converges on the same target state. For risky changes, dry-run with --check and --diff before you apply for real, because ad-hoc shell or command calls and state=absent take effect immediately and irreversibly. Keep secrets encrypted in ansible-vault only – never in plaintext in a Git repository – and reach for -b/--become solely where you genuinely need root privileges.
Further Reading
- Ansible documentation – official reference for playbooks, modules and inventory
- Ansible Vault – encrypting secrets – official guide to managing secrets securely
- Ansible (software) – background and context on the English Wikipedia