Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOpsSchool!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Ansible: Command line tools with example in Ansible

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

ExampleDescription
ansible all -m pingPing 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 setupGather 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 pingRun ping module as user ‘admin’.
ansible all --list-hostsList hosts targeted by pattern.
ansible all --limit dbservers -m pingLimit 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" -vRun 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

ExampleDescription
ansible-playbook site.ymlRun ‘site.yml’ playbook.
ansible-playbook -i inventory.yml site.ymlSpecify a custom inventory.
ansible-playbook -l webservers site.ymlLimit run to webservers group.
ansible-playbook -u deployer site.ymlRun playbook as ‘deployer’ user.
ansible-playbook -b site.ymlRun with privilege escalation (sudo/become).
ansible-playbook -k site.ymlAsk for SSH password.
ansible-playbook -K site.ymlAsk for privilege escalation password.
ansible-playbook --check site.ymlDry run; don’t change anything.
ansible-playbook --diff site.ymlShow diff when files change.
ansible-playbook --start-at-task="Install packages" site.ymlStart at named task.
ansible-playbook -e var=value site.ymlSet extra variable.
ansible-playbook -e @vars.yml site.ymlLoad extra vars from YAML file.
ansible-playbook --tags "setup,install" site.ymlRun only tasks with these tags.
ansible-playbook --skip-tags "test" site.ymlSkip tasks with these tags.
ansible-playbook --step site.ymlStep through each task interactively.
ansible-playbook -v site.ymlVerbose mode (single -v, -vv, -vvv for more).
ansible-playbook --syntax-check site.ymlCheck playbook syntax only.
ansible-playbook --list-hosts site.ymlShow which hosts would be affected.
ansible-playbook --list-tasks site.ymlList all tasks.
ansible-playbook --list-tags site.ymlList all tags.
ansible-playbook --flush-cache site.ymlFlush fact cache before run.
ansible-playbook --forks 20 site.ymlSet 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

ExampleDescription
ansible-galaxy install geerlingguy.nginxInstall a role from Ansible Galaxy.
ansible-galaxy install -r requirements.ymlInstall multiple roles from requirements file.
ansible-galaxy listList installed roles.
ansible-galaxy remove geerlingguy.nginxRemove an installed role.
ansible-galaxy init myroleScaffold a new role called myrole.
ansible-galaxy collection install community.generalInstall a collection from Galaxy.
ansible-galaxy collection install -r collections/requirements.ymlInstall collections from requirements.
ansible-galaxy collection listList installed collections.
ansible-galaxy collection init my_namespace.my_collectionScaffold a new collection.
ansible-galaxy role search nginxSearch roles by keyword.
ansible-galaxy collection search kubernetesSearch collections by keyword.
ansible-galaxy role info geerlingguy.nginxShow details for a role.
ansible-galaxy collection info community.generalShow 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

ExampleDescription
ansible-vault create secrets.ymlCreate a new encrypted file.
ansible-vault edit secrets.ymlEdit encrypted file in-place.
ansible-vault view secrets.ymlView contents of an encrypted file.
ansible-vault encrypt file.ymlEncrypt a plaintext file.
ansible-vault decrypt secrets.ymlDecrypt file to plaintext.
ansible-vault rekey secrets.ymlChange 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.ymlEncrypt with a password file.
ansible-vault decrypt --vault-password-file ~/.vault_pass.txt secrets.ymlDecrypt with a password file.
ansible-playbook --ask-vault-pass site.ymlPrompt for vault password when running a playbook.
ansible-playbook --vault-password-file ~/.vault_pass.txt site.ymlUse 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

ExampleDescription
ansible-doc pingShow documentation for ping module.
ansible-doc -lList all available modules.
ansible-doc -s fileShow minimal argument spec for file module.
ansible-doc -M ./library custom_moduleShow doc for module in custom library dir.
ansible-doc -t lookup fileShow lookup plugin documentation.
ansible-doc --playbook-dir=./roles/Specify playbook directory context.
ansible-doc --jsonOutput 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

ExampleDescription
ansible-inventory --listList all hosts and groups in JSON.
ansible-inventory --graphShow inventory as a graph.
ansible-inventory --host <hostname>Show variables for a specific host.
ansible-inventory -i hosts.ini --listList inventory from a specific file.
ansible-inventory -i inventory.yml --graphGraph from YAML inventory.
ansible-inventory -i aws_ec2.yaml --listUse dynamic inventory.
ansible-inventory --yamlOutput inventory as YAML.
ansible-inventory --varsInclude vars in output.
ansible-inventory --exportExport inventory for external use.
ansible-inventory --helpShow 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

ExampleDescription
ansible-pull -U https://github.com/example/repo.gitPull and run playbook from git repo.
ansible-pull -U https://github.com/example/repo.git playbook.ymlPull 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/checkoutSpecify checkout directory.
ansible-pull -U https://github.com/example/repo.git -C devCheckout 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-keyAuto-accept git SSH keys.
ansible-pull -U https://github.com/example/repo.git -vVerbose mode.

8. ansible-config

View and manage Ansible config settings.

General Syntax

ansible-config <subcommand> [options]
Code language: HTML, XML (xml)

All Example Combinations

ExampleDescription
ansible-config viewShow current config settings.
ansible-config listList all config options and their defaults.
ansible-config dumpDump config settings (shows source of each).
ansible-config init --disabled > ansible.cfgGenerate 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

ExampleDescription
ansible-consoleStart interactive console (all hosts).
ansible-console webserversLimit to webservers group.
ansible-console --inventory inventory.ymlUse a custom inventory.
ansible-console --becomeRun 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

ExampleDescription
ansible-connection -hShow help and available subcommands.
ansible-connection passwordShow 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/ModuleExample Syntax/Usage
ansibleansible all -m ping
ansible-playbookansible-playbook -i hosts site.yml
ansible-galaxyansible-galaxy install geerlingguy.nginx
ansible-vaultansible-vault encrypt secrets.yml
ansible-docansible-doc file
ansible-inventoryansible-inventory --list
ansible-pullansible-pull -U https://github.com/example/repo.git
ansible-configansible-config view
ansible-consoleansible-console webservers
ansible-connectionansible-connection -h
Common modulescommand, shell, copy, template, file, service, user, apt, yum, git, etc.

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x