Learn Ansible CLI Commands
Contents
- 1 1. Ansible Inventory Management
- 2 2. Ansible Ad-Hoc Commands
- 3 3. Ansible Playbook Execution
- 4 4. Ansible Variables and Ansible Vault
- 5 5. Ansible Configuration and Debugging
- 6 6. Managing Ansible Roles and Ansible Collections
- 7 7. Advanced Usage
- 8 8. Ansible Miscellaneous Commands
- 9 9. Ansible Role Management
- 10 10. Automation and CI/CD Integration
1. Ansible Inventory Management
ansible-inventory --list
Purpose: Display the complete inventory in JSON format.ansible-inventory --graph
Purpose: Visualize the inventory as a graph.ansible-inventory -i inventory.ini --host hostname
Purpose: Show details for a specific host.ansible-inventory -i inventory.ini --yaml
Purpose: Display inventory in YAML format.ansible-inventory --inventory-file=inventory.ini
Purpose: Specify a custom inventory file for commands.ansible-inventory -i my_inventory.ini --host all
Purpose: List all hosts in a custom inventory.ansible-inventory --export
Purpose: Export the inventory to a specified format.ansible-inventory --list --vars
Purpose: Show the inventory with variable details.ansible-inventory --list --yaml --output-file=output.yml
Purpose: Save the inventory list to a YAML file.ansible-inventory --list --output-format=json
Purpose: Display the inventory in JSON format.
2. Ansible Ad-Hoc Commands
ansible all -m ping
Purpose: Ping all hosts to check connectivity.ansible all -m shell -a "ls -l"
Purpose: List files in the directory on all hosts.ansible dbservers -m command -a "df -h"
Purpose: Check disk usage on database servers.ansible webservers -m user -a "name=admin state=present"
Purpose: Ensure theadmin
user is present on web servers.ansible all -m copy -a "src=/local/path dest=/remote/path"
Purpose: Copy a file from the local to the remote system.ansible all -m file -a "path=/tmp/testfile state=absent"
Purpose: Remove a file from all hosts.ansible all -m yum -a "name=httpd state=latest"
Purpose: Ensure thehttpd
package is updated to the latest version.ansible all -m service -a "name=nginx state=started"
Purpose: Start thenginx
service on all hosts.ansible all -m cron -a "name='Backup' minute='0' hour='2' job='/usr/bin/backup.sh'"
Purpose: Schedule a cron job to run a backup script.ansible all -m debug -a "msg='This is a test message'"
Purpose: Display a debug message on all hosts.
3. Ansible Playbook Execution
ansible-playbook playbook.yml
Purpose: Execute the specified playbook.ansible-playbook -i inventory.ini playbook.yml
Purpose: Run a playbook using a custom inventory file.ansible-playbook playbook.yml --check
Purpose: Perform a dry run to see what changes would be applied.ansible-playbook playbook.yml --diff
Purpose: Show the differences between the current and desired states.ansible-playbook playbook.yml -e "var=value"
Purpose: Pass additional variables to the playbook.ansible-playbook playbook.yml --tags "tag_name"
Purpose: Execute only tasks with the specified tag.ansible-playbook playbook.yml --skip-tags "tag_name"
Purpose: Skip tasks with the specified tag.ansible-playbook playbook.yml --start-at-task "task_name"
Purpose: Begin execution at a specific task.ansible-playbook playbook.yml --limit "group_name"
Purpose: Restrict playbook execution to a specific group or host.ansible-playbook playbook.yml --vault-password-file=/path/to/password_file
Purpose: Use a file to decrypt Ansible Vault-encrypted data.
4. Ansible Variables and Ansible Vault
ansible-playbook playbook.yml -e "@vars.yml"
Purpose: Load variables from an external file.ansible-vault create secrets.yml
Purpose: Create a new encrypted file using Ansible Vault.ansible-vault edit secrets.yml
Purpose: Edit an existing encrypted file.ansible-vault decrypt secrets.yml
Purpose: Decrypt a file manually.ansible-vault encrypt secrets.yml
Purpose: Encrypt a file using Ansible Vault.ansible-vault rekey secrets.yml
Purpose: Change the encryption key of a file.ansible-vault view secrets.yml
Purpose: View the content of an encrypted file.ansible-vault password-file /path/to/file
Purpose: Specify a password file for Vault operations.ansible-vault encrypt_string 'value' --name 'variable_name'
Purpose: Encrypt a single variable.ansible-vault encrypt_string 'value' --name 'variable_name' --vault-id my_vault
Purpose: Encrypt a variable with a specific Vault ID.
5. Ansible Configuration and Debugging
ansible-config view
Purpose: Display current Ansible configuration settings.ansible-config dump
Purpose: Dump all configuration settings for review.ansible-playbook playbook.yml -v
Purpose: Increase verbosity of playbook execution output.ansible-playbook playbook.yml -vv
Purpose: Further increase verbosity of output.ansible-playbook playbook.yml -vvv
Purpose: Show detailed debug output.ansible-playbook playbook.yml --step
Purpose: Execute playbook tasks one by one with user confirmation.ansible-playbook playbook.yml --start-at-task="task_name"
Purpose: Begin playbook execution at a specific task.ansible-playbook playbook.yml --list-tasks
Purpose: List all tasks in a playbook without running them.ansible-playbook playbook.yml --syntax-check
Purpose: Validate the syntax of a playbook.ansible-playbook playbook.yml --extra-vars '{"key": "value"}'
Purpose: Pass extra variables in JSON format.
6. Managing Ansible Roles and Ansible Collections
ansible-galaxy init role_name
Purpose: Create a new role structure.ansible-galaxy install role_name
Purpose: Install a role from Ansible Galaxy.ansible-galaxy install -r requirements.yml
Purpose: Install roles defined in a requirements file.ansible-galaxy list
Purpose: List installed roles and collections.ansible-galaxy remove role_name
Purpose: Uninstall a role from the system.ansible-galaxy collection install collection_name
Purpose: Install a collection from Ansible Galaxy.ansible-galaxy collection list
Purpose: List installed collections.ansible-galaxy collection remove collection_name
Purpose: Uninstall a collection.ansible-galaxy role init my_role
Purpose: Initialize a new role namedmy_role
.ansible-galaxy collection init my_collection
Purpose: Initialize a new collection namedmy_collection
.
7. Advanced Usage
ansible-playbook playbook.yml --role-swap old_role:new_role
Purpose: Swap roles in a playbook dynamically.ansible-playbook playbook.yml --diff
Purpose: Show changes between current and desired states.ansible-playbook playbook.yml --extra-vars "env=production" --tags "deploy"
Purpose: Pass variables and limit execution to specific tags.ansible-playbook playbook.yml --start-at-task="Setup" --limit "webserver1"
Purpose: Start at a specific task and limit execution to one host.ansible-playbook playbook.yml --extra-vars "@/path/to/vars.yml"
Purpose: Load variables from an external file.ansible-playbook playbook.yml --vault-id vault_password_file
Purpose: Use a Vault password file for encrypted data.ansible-playbook playbook.yml --vault-id my_vault
Purpose: Use a specific Vault ID for decryption.ansible-playbook playbook.yml --start-at-task="Install" --diff
Purpose: Start at a specific task and show differences.ansible-playbook playbook.yml --list-tasks --list-tags
Purpose: List tasks and tags without running the playbook.ansible-playbook playbook.yml --ask-become-pass
Purpose: Prompt for a password to escalate privileges.
8. Ansible Miscellaneous Commands
ansible-playbook playbook.yml --ask-vault-pass
Purpose: Prompt for a Vault password interactively.ansible-playbook playbook.yml --check --diff
Purpose: Perform a dry run and show differences.ansible-playbook playbook.yml --start-at-task "Install App" --extra-vars "version=2.0"
Purpose: Start execution at a task and set extra variables.ansible-playbook playbook.yml --extra-vars "key=value" --limit "group_name"
Purpose: Pass extra variables and restrict to a specific group.ansible-playbook playbook.yml --syntax-check --list-tasks
Purpose: Validate syntax and list all tasks.ansible-playbook playbook.yml --verbose
Purpose: Increase the verbosity of the output.ansible-playbook playbook.yml --step --diff
Purpose: Run playbook step-by-step with differences shown.ansible-playbook playbook.yml --ask-become-pass --tags "web"
Purpose: Prompt for escalation password and run tasks with a specific tag.ansible-playbook playbook.yml --extra-vars "var1=value1 var2=value2"
Purpose: Pass multiple variables at runtime.ansible-playbook playbook.yml --vault-id my_vault@prompt
Purpose: Prompt for Vault password interactively with a specific Vault ID.
9. Ansible Role Management
ansible-galaxy init my_role
Purpose: Create a new Ansible role namedmy_role
.ansible-galaxy install -r requirements.yml
Purpose: Install roles listed in arequirements.yml
file.ansible-galaxy role list
Purpose: List all installed roles.ansible-galaxy role remove role_name
Purpose: Uninstall a specified role.ansible-galaxy role import role_name
Purpose: Import a role from a repository.ansible-galaxy role search role_name
Purpose: Search for roles by name.ansible-galaxy collection init my_collection
Purpose: Create a new collection namedmy_collection
.ansible-galaxy collection install collection_name
Purpose: Install a specified collection.ansible-galaxy collection remove collection_name
Purpose: Remove a collection from the system.ansible-galaxy collection search collection_name
Purpose: Search for collections by name.
10. Automation and CI/CD Integration
ansible-playbook playbook.yml --extra-vars "@secrets.yml"
Purpose: Load variables from a file in CI/CD pipelines.ansible-playbook playbook.yml --vault-id @prompt
Purpose: Prompt for Vault password during automated runs.ansible-playbook playbook.yml --diff --check
Purpose: Check what changes would be made and show differences.ansible-playbook playbook.yml --list-tasks
Purpose: List all tasks in a playbook without executing them.ansible-playbook playbook.yml --tags "update" --extra-vars "version=3.0"
Purpose: Run tasks tagged withupdate
and set version to3.0
.ansible-playbook playbook.yml --limit "production"
Purpose: Restrict playbook execution to hosts in theproduction
group.ansible-playbook playbook.yml --verbose
Purpose: Provide detailed output for debugging purposes.ansible-playbook playbook.yml --ask-pass
Purpose: Prompt for SSH password for remote connections.ansible-playbook playbook.yml --ask-become-pass
Purpose: Request password for privilege escalation.ansible-playbook playbook.yml --start-at-task="Setup"
Purpose: Begin execution at a specific task in the playbook.