Ansible Commands
This page is a practical command reference for day-to-day Ansible work.
Install and Remove
Configuration
Common config locations:
~/.ansible.cfg/etc/ansible/ansible.cfg
Inspect the effective configuration:
Inventory and Ad Hoc Commands
ansible all -i inventory.ini -m ping
ansible web -i inventory.ini -a hostname
ansible all -i host.example.com, --user=ubuntu -m ping
Inline inventory with a private key:
ansible all -i host.example.com, --user=ubuntu \
-e ansible_ssh_private_key_file=my_ssh_key.pem \
-m ping
Playbooks
ansible-playbook -i inventory.ini site.yml
ansible-playbook site.yml --limit worker
ansible-playbook site.yml --start-at-task="Install nginx"
ansible-playbook site.yml --extra-vars="env=prod"
Variables
Use extra vars for runtime input:
ansible-playbook site.yml --extra-vars="remote_folder=/opt/app"
ansible-playbook site.yml --extra-vars=@vars/prod.yml
Access environment values inside templates:
Roles and Includes
Example Task Patterns
- hosts: all
tasks:
- name: Create directory
ansible.builtin.file:
path: ~/spark-submit/trafficsigns
state: directory
mode: "0775"
- name: Copy files
ansible.builtin.copy:
src: /home/projects/ubs/current-task/nodes/ansible/files/
dest: ~/spark-submit/trafficsigns
mode: "0775"
Practical Notes
- Use
gather_facts: nowhen the target host does not have Python available. - Prefer idempotent modules over shell commands.
- Keep secrets out of plain playbooks and move them to vault or encrypted vars.