1. ansible
Run a single task (module) against hosts in your inventory. For quick, ad-hoc commands.
General Syntax
ansible <host-pattern> -m <module_name> -a "<module arguments>" [options]
Code language: HTML, XML (xml)
Common Examples and All Key Combinations
| Example | Description |
|---|---|
ansible all -m ping | Ping all hosts using the ‘ping’ module. |
ansible webservers -m shell -a "uptime" | Run ‘uptime’ shell command on ‘webservers’ group. |
ansible dbservers -m command -a "df -h" | Run ‘df -h’ using command module (no shell features) on ‘dbservers’. |
ansible localhost -m setup | Gather facts on localhost. |
ansible all -m user -a "name=john state=present" | Create user ‘john’ on all hosts. |
ansible all -m copy -a "src=foo.conf dest=/etc/foo.conf" | Copy file to all hosts. |
ansible all -m yum -a "name=httpd state=latest" | Install latest httpd via yum on all hosts. |
ansible all -m service -a "name=httpd state=restarted" | Restart httpd service on all hosts. |
ansible all -m file -a "path=/tmp/testfile state=touch" | Create empty file on all hosts. |
ansible all -m get_url -a "url=https://example.com/file.tar.gz dest=/tmp/file.tar.gz" | Download file from URL. |
ansible all -m debug -a "msg='Hello World'" | Print debug message on all hosts. |
ansible all -a "uname -a" | Run shell command directly (shell module by default if -m is omitted). |
ansible all -b -m apt -a "name=nginx state=present" | Install nginx with privilege escalation (sudo). |
ansible all -u admin -m ping | Run ping module as user ‘admin’. |
ansible all --list-hosts | List hosts targeted by pattern. |
ansible all --limit dbservers -m ping | Limit run to only dbservers. |
ansible all --check -m yum -a "name=git state=present" | Dry run (check mode). |
ansible all -m command -a "ls -l /tmp" -v | Run command with verbose output. |
ansible all -m raw -a "uptime" | Run raw command (no Python required on remote). |
2. ansible-playbook
Run complex multi-step playbooks written in YAML.
General Syntax
ansible-playbook <playbook.yml> [options]
Code language: CSS (css)
All Example Combinations
| Example | Description |
|---|---|
ansible-playbook site.yml | Run ‘site.yml’ playbook. |
ansible-playbook -i inventory.yml site.yml | Specify a custom inventory. |
ansible-playbook -l webservers site.yml | Limit run to webservers group. |
ansible-playbook -u deployer site.yml | Run playbook as ‘deployer’ user. |
ansible-playbook -b site.yml | Run with privilege escalation (sudo/become). |
ansible-playbook -k site.yml | Ask for SSH password. |
ansible-playbook -K site.yml | Ask for privilege escalation password. |
ansible-playbook --check site.yml | Dry run; don’t change anything. |
ansible-playbook --diff site.yml | Show diff when files change. |
ansible-playbook --start-at-task="Install packages" site.yml | Start at named task. |
ansible-playbook -e var=value site.yml | Set extra variable. |
ansible-playbook -e @vars.yml site.yml | Load extra vars from YAML file. |
ansible-playbook --tags "setup,install" site.yml | Run only tasks with these tags. |
ansible-playbook --skip-tags "test" site.yml | Skip tasks with these tags. |
ansible-playbook --step site.yml | Step through each task interactively. |
ansible-playbook -v site.yml | Verbose mode (single -v, -vv, -vvv for more). |
ansible-playbook --syntax-check site.yml | Check playbook syntax only. |
ansible-playbook --list-hosts site.yml | Show which hosts would be affected. |
ansible-playbook --list-tasks site.yml | List all tasks. |
ansible-playbook --list-tags site.yml | List all tags. |
ansible-playbook --flush-cache site.yml | Flush fact cache before run. |
ansible-playbook --forks 20 site.yml | Set number of parallel processes. |
3. ansible-galaxy
Install, manage, and create roles and collections.
General Syntax
ansible-galaxy <subcommand> [options]
Code language: HTML, XML (xml)
All Example Combinations
| Example | Description |
|---|---|
ansible-galaxy install geerlingguy.nginx | Install a role from Ansible Galaxy. |
ansible-galaxy install -r requirements.yml | Install multiple roles from requirements file. |
ansible-galaxy list | List installed roles. |
ansible-galaxy remove geerlingguy.nginx | Remove an installed role. |
ansible-galaxy init myrole | Scaffold a new role called myrole. |
ansible-galaxy collection install community.general | Install a collection from Galaxy. |
ansible-galaxy collection install -r collections/requirements.yml | Install collections from requirements. |
ansible-galaxy collection list | List installed collections. |
ansible-galaxy collection init my_namespace.my_collection | Scaffold a new collection. |
ansible-galaxy role search nginx | Search roles by keyword. |
ansible-galaxy collection search kubernetes | Search collections by keyword. |
ansible-galaxy role info geerlingguy.nginx | Show details for a role. |
ansible-galaxy collection info community.general | Show details for a collection. |
4. ansible-vault
Encrypt, decrypt, edit, and manage secrets.
General Syntax
ansible-vault <command> [options] <file>
Code language: HTML, XML (xml)
All Example Combinations
| Example | Description |
|---|---|
ansible-vault create secrets.yml | Create a new encrypted file. |
ansible-vault edit secrets.yml | Edit encrypted file in-place. |
ansible-vault view secrets.yml | View contents of an encrypted file. |
ansible-vault encrypt file.yml | Encrypt a plaintext file. |
ansible-vault decrypt secrets.yml | Decrypt file to plaintext. |
ansible-vault rekey secrets.yml | Change the password on the encrypted file. |
ansible-vault encrypt_string 'supersecret' --name 'my_secret' | Output an encrypted string for use in playbooks. |
ansible-vault encrypt --vault-password-file ~/.vault_pass.txt secrets.yml | Encrypt with a password file. |
ansible-vault decrypt --vault-password-file ~/.vault_pass.txt secrets.yml | Decrypt with a password file. |
ansible-playbook --ask-vault-pass site.yml | Prompt for vault password when running a playbook. |
ansible-playbook --vault-password-file ~/.vault_pass.txt site.yml | Use password file for vault. |
5. ansible-doc
Display documentation and examples for modules and plugins.
General Syntax
ansible-doc <module_name> [options]
Code language: HTML, XML (xml)
All Example Combinations
| Example | Description |
|---|---|
ansible-doc ping | Show documentation for ping module. |
ansible-doc -l | List all available modules. |
ansible-doc -s file | Show minimal argument spec for file module. |
ansible-doc -M ./library custom_module | Show doc for module in custom library dir. |
ansible-doc -t lookup file | Show lookup plugin documentation. |
ansible-doc --playbook-dir=./roles/ | Specify playbook directory context. |
ansible-doc --json | Output module docs in JSON format. |
6. ansible-inventory
Manage, validate, and display inventory.
General Syntax
ansible-inventory [options]
Code language: CSS (css)
All Example Combinations
| Example | Description |
|---|---|
ansible-inventory --list | List all hosts and groups in JSON. |
ansible-inventory --graph | Show inventory as a graph. |
ansible-inventory --host <hostname> | Show variables for a specific host. |
ansible-inventory -i hosts.ini --list | List inventory from a specific file. |
ansible-inventory -i inventory.yml --graph | Graph from YAML inventory. |
ansible-inventory -i aws_ec2.yaml --list | Use dynamic inventory. |
ansible-inventory --yaml | Output inventory as YAML. |
ansible-inventory --vars | Include vars in output. |
ansible-inventory --export | Export inventory for external use. |
ansible-inventory --help | Show all options. |
7. ansible-pull
Run playbooks in “pull” mode (nodes pull playbooks instead of being pushed).
General Syntax
ansible-pull -U <repo_url> [playbook.yml] [options]
Code language: CSS (css)
All Example Combinations
| Example | Description |
|---|---|
ansible-pull -U https://github.com/example/repo.git | Pull and run playbook from git repo. |
ansible-pull -U https://github.com/example/repo.git playbook.yml | Pull and run specific playbook. |
ansible-pull -U https://github.com/example/repo.git -i localhost, | Specify inventory (e.g., localhost only). |
ansible-pull -U https://github.com/example/repo.git -d /tmp/checkout | Specify checkout directory. |
ansible-pull -U https://github.com/example/repo.git -C dev | Checkout a specific branch. |
ansible-pull -U https://github.com/example/repo.git -e "var=value" | Pass extra variables. |
ansible-pull -U https://github.com/example/repo.git --accept-host-key | Auto-accept git SSH keys. |
ansible-pull -U https://github.com/example/repo.git -v | Verbose mode. |
8. ansible-config
View and manage Ansible config settings.
General Syntax
ansible-config <subcommand> [options]
Code language: HTML, XML (xml)
All Example Combinations
| Example | Description |
|---|---|
ansible-config view | Show current config settings. |
ansible-config list | List all config options and their defaults. |
ansible-config dump | Dump config settings (shows source of each). |
ansible-config init --disabled > ansible.cfg | Generate a commented ansible.cfg template. |
9. ansible-console
An interactive shell for running Ansible ad-hoc commands.
General Syntax
ansible-console [host-pattern] [options]
Code language: CSS (css)
All Example Combinations
| Example | Description |
|---|---|
ansible-console | Start interactive console (all hosts). |
ansible-console webservers | Limit to webservers group. |
ansible-console --inventory inventory.yml | Use a custom inventory. |
ansible-console --become | Run with privilege escalation. |
Once inside the console:
> ping> shell uname -a> apt name=git state=present> exit
10. ansible-connection
Internal, used to manage connection plugins (rarely used directly).
General Syntax
ansible-connection <subcommand> [options]
Code language: HTML, XML (xml)
All Example Combinations
| Example | Description |
|---|---|
ansible-connection -h | Show help and available subcommands. |
ansible-connection password | Show password subcommand (used by connection plugins). |
BONUS: Module Examples (Most Common Patterns)
1. Command module
- name: List directory
ansible.builtin.command:
cmd: ls -l /tmp
Code language: PHP (php)
2. Shell module
- name: Run shell with pipe
ansible.builtin.shell: "cat /etc/passwd | grep root"
Code language: JavaScript (javascript)
3. Copy module
- name: Copy config file
ansible.builtin.copy:
src: ./nginx.conf
dest: /etc/nginx/nginx.conf
Code language: JavaScript (javascript)
4. Template module
- name: Deploy config from template
ansible.builtin.template:
src: ./app.conf.j2
dest: /etc/myapp/app.conf
Code language: JavaScript (javascript)
5. File module
- name: Set permissions
ansible.builtin.file:
path: /opt/foo
state: directory
mode: '0755'
Code language: JavaScript (javascript)
6. Service module
- name: Ensure nginx is running
ansible.builtin.service:
name: nginx
state: started
Code language: CSS (css)
7. User module
- name: Add user
ansible.builtin.user:
name: deploy
groups: sudo
state: present
Code language: CSS (css)
8. Package modules (apt/yum/dnf)
- name: Install nginx (Debian)
ansible.builtin.apt:
name: nginx
state: latest
- name: Install nginx (RHEL)
ansible.builtin.yum:
name: nginx
state: latest
Code language: CSS (css)
9. Git module
- name: Clone a repo
ansible.builtin.git:
repo: 'https://github.com/example/project.git'
dest: /opt/app
Code language: JavaScript (javascript)
Summary Table
| Tool/Module | Example Syntax/Usage |
|---|---|
| ansible | ansible all -m ping |
| ansible-playbook | ansible-playbook -i hosts site.yml |
| ansible-galaxy | ansible-galaxy install geerlingguy.nginx |
| ansible-vault | ansible-vault encrypt secrets.yml |
| ansible-doc | ansible-doc file |
| ansible-inventory | ansible-inventory --list |
| ansible-pull | ansible-pull -U https://github.com/example/repo.git |
| ansible-config | ansible-config view |
| ansible-console | ansible-console webservers |
| ansible-connection | ansible-connection -h |
| Common modules | command, shell, copy, template, file, service, user, apt, yum, git, etc. |
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals