Learn Ansible CLI Commands

1. Ansible Inventory Management

  1. ansible-inventory --list
    Purpose: Display the complete inventory in JSON format.
  2. ansible-inventory --graph
    Purpose: Visualize the inventory as a graph.
  3. ansible-inventory -i inventory.ini --host hostname
    Purpose: Show details for a specific host.
  4. ansible-inventory -i inventory.ini --yaml
    Purpose: Display inventory in YAML format.
  5. ansible-inventory --inventory-file=inventory.ini
    Purpose: Specify a custom inventory file for commands.
  6. ansible-inventory -i my_inventory.ini --host all
    Purpose: List all hosts in a custom inventory.
  7. ansible-inventory --export
    Purpose: Export the inventory to a specified format.
  8. ansible-inventory --list --vars
    Purpose: Show the inventory with variable details.
  9. ansible-inventory --list --yaml --output-file=output.yml
    Purpose: Save the inventory list to a YAML file.
  10. ansible-inventory --list --output-format=json
    Purpose: Display the inventory in JSON format.

2. Ansible Ad-Hoc Commands

  1. ansible all -m ping
    Purpose: Ping all hosts to check connectivity.
  2. ansible all -m shell -a "ls -l"
    Purpose: List files in the directory on all hosts.
  3. ansible dbservers -m command -a "df -h"
    Purpose: Check disk usage on database servers.
  4. ansible webservers -m user -a "name=admin state=present"
    Purpose: Ensure the admin user is present on web servers.
  5. ansible all -m copy -a "src=/local/path dest=/remote/path"
    Purpose: Copy a file from the local to the remote system.
  6. ansible all -m file -a "path=/tmp/testfile state=absent"
    Purpose: Remove a file from all hosts.
  7. ansible all -m yum -a "name=httpd state=latest"
    Purpose: Ensure the httpd package is updated to the latest version.
  8. ansible all -m service -a "name=nginx state=started"
    Purpose: Start the nginx service on all hosts.
  9. 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.
  10. ansible all -m debug -a "msg='This is a test message'"
    Purpose: Display a debug message on all hosts.

3. Ansible Playbook Execution

  1. ansible-playbook playbook.yml
    Purpose: Execute the specified playbook.
  2. ansible-playbook -i inventory.ini playbook.yml
    Purpose: Run a playbook using a custom inventory file.
  3. ansible-playbook playbook.yml --check
    Purpose: Perform a dry run to see what changes would be applied.
  4. ansible-playbook playbook.yml --diff
    Purpose: Show the differences between the current and desired states.
  5. ansible-playbook playbook.yml -e "var=value"
    Purpose: Pass additional variables to the playbook.
  6. ansible-playbook playbook.yml --tags "tag_name"
    Purpose: Execute only tasks with the specified tag.
  7. ansible-playbook playbook.yml --skip-tags "tag_name"
    Purpose: Skip tasks with the specified tag.
  8. ansible-playbook playbook.yml --start-at-task "task_name"
    Purpose: Begin execution at a specific task.
  9. ansible-playbook playbook.yml --limit "group_name"
    Purpose: Restrict playbook execution to a specific group or host.
  10. 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

  1. ansible-playbook playbook.yml -e "@vars.yml"
    Purpose: Load variables from an external file.
  2. ansible-vault create secrets.yml
    Purpose: Create a new encrypted file using Ansible Vault.
  3. ansible-vault edit secrets.yml
    Purpose: Edit an existing encrypted file.
  4. ansible-vault decrypt secrets.yml
    Purpose: Decrypt a file manually.
  5. ansible-vault encrypt secrets.yml
    Purpose: Encrypt a file using Ansible Vault.
  6. ansible-vault rekey secrets.yml
    Purpose: Change the encryption key of a file.
  7. ansible-vault view secrets.yml
    Purpose: View the content of an encrypted file.
  8. ansible-vault password-file /path/to/file
    Purpose: Specify a password file for Vault operations.
  9. ansible-vault encrypt_string 'value' --name 'variable_name'
    Purpose: Encrypt a single variable.
  10. 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

  1. ansible-config view
    Purpose: Display current Ansible configuration settings.
  2. ansible-config dump
    Purpose: Dump all configuration settings for review.
  3. ansible-playbook playbook.yml -v
    Purpose: Increase verbosity of playbook execution output.
  4. ansible-playbook playbook.yml -vv
    Purpose: Further increase verbosity of output.
  5. ansible-playbook playbook.yml -vvv
    Purpose: Show detailed debug output.
  6. ansible-playbook playbook.yml --step
    Purpose: Execute playbook tasks one by one with user confirmation.
  7. ansible-playbook playbook.yml --start-at-task="task_name"
    Purpose: Begin playbook execution at a specific task.
  8. ansible-playbook playbook.yml --list-tasks
    Purpose: List all tasks in a playbook without running them.
  9. ansible-playbook playbook.yml --syntax-check
    Purpose: Validate the syntax of a playbook.
  10. ansible-playbook playbook.yml --extra-vars '{"key": "value"}'
    Purpose: Pass extra variables in JSON format.

6. Managing Ansible Roles and  Ansible Collections

  1. ansible-galaxy init role_name
    Purpose: Create a new role structure.
  2. ansible-galaxy install role_name
    Purpose: Install a role from Ansible Galaxy.
  3. ansible-galaxy install -r requirements.yml
    Purpose: Install roles defined in a requirements file.
  4. ansible-galaxy list
    Purpose: List installed roles and collections.
  5. ansible-galaxy remove role_name
    Purpose: Uninstall a role from the system.
  6. ansible-galaxy collection install collection_name
    Purpose: Install a collection from Ansible Galaxy.
  7. ansible-galaxy collection list
    Purpose: List installed collections.
  8. ansible-galaxy collection remove collection_name
    Purpose: Uninstall a collection.
  9. ansible-galaxy role init my_role
    Purpose: Initialize a new role named my_role.
  10. ansible-galaxy collection init my_collection
    Purpose: Initialize a new collection named my_collection.

7. Advanced Usage

  1. ansible-playbook playbook.yml --role-swap old_role:new_role
    Purpose: Swap roles in a playbook dynamically.
  2. ansible-playbook playbook.yml --diff
    Purpose: Show changes between current and desired states.
  3. ansible-playbook playbook.yml --extra-vars "env=production" --tags "deploy"
    Purpose: Pass variables and limit execution to specific tags.
  4. ansible-playbook playbook.yml --start-at-task="Setup" --limit "webserver1"
    Purpose: Start at a specific task and limit execution to one host.
  5. ansible-playbook playbook.yml --extra-vars "@/path/to/vars.yml"
    Purpose: Load variables from an external file.
  6. ansible-playbook playbook.yml --vault-id vault_password_file
    Purpose: Use a Vault password file for encrypted data.
  7. ansible-playbook playbook.yml --vault-id my_vault
    Purpose: Use a specific Vault ID for decryption.
  8. ansible-playbook playbook.yml --start-at-task="Install" --diff
    Purpose: Start at a specific task and show differences.
  9. ansible-playbook playbook.yml --list-tasks --list-tags
    Purpose: List tasks and tags without running the playbook.
  10. ansible-playbook playbook.yml --ask-become-pass
    Purpose: Prompt for a password to escalate privileges.

8. Ansible Miscellaneous Commands

  1. ansible-playbook playbook.yml --ask-vault-pass
    Purpose: Prompt for a Vault password interactively.
  2. ansible-playbook playbook.yml --check --diff
    Purpose: Perform a dry run and show differences.
  3. ansible-playbook playbook.yml --start-at-task "Install App" --extra-vars "version=2.0"
    Purpose: Start execution at a task and set extra variables.
  4. ansible-playbook playbook.yml --extra-vars "key=value" --limit "group_name"
    Purpose: Pass extra variables and restrict to a specific group.
  5. ansible-playbook playbook.yml --syntax-check --list-tasks
    Purpose: Validate syntax and list all tasks.
  6. ansible-playbook playbook.yml --verbose
    Purpose: Increase the verbosity of the output.
  7. ansible-playbook playbook.yml --step --diff
    Purpose: Run playbook step-by-step with differences shown.
  8. ansible-playbook playbook.yml --ask-become-pass --tags "web"
    Purpose: Prompt for escalation password and run tasks with a specific tag.
  9. ansible-playbook playbook.yml --extra-vars "var1=value1 var2=value2"
    Purpose: Pass multiple variables at runtime.
  10. ansible-playbook playbook.yml --vault-id my_vault@prompt
    Purpose: Prompt for Vault password interactively with a specific Vault ID.

9. Ansible Role Management

  1. ansible-galaxy init my_role
    Purpose: Create a new Ansible role named my_role.
  2. ansible-galaxy install -r requirements.yml
    Purpose: Install roles listed in a requirements.yml file.
  3. ansible-galaxy role list
    Purpose: List all installed roles.
  4. ansible-galaxy role remove role_name
    Purpose: Uninstall a specified role.
  5. ansible-galaxy role import role_name
    Purpose: Import a role from a repository.
  6. ansible-galaxy role search role_name
    Purpose: Search for roles by name.
  7. ansible-galaxy collection init my_collection
    Purpose: Create a new collection named my_collection.
  8. ansible-galaxy collection install collection_name
    Purpose: Install a specified collection.
  9. ansible-galaxy collection remove collection_name
    Purpose: Remove a collection from the system.
  10. ansible-galaxy collection search collection_name
    Purpose: Search for collections by name.

10. Automation and CI/CD Integration

  1. ansible-playbook playbook.yml --extra-vars "@secrets.yml"
    Purpose: Load variables from a file in CI/CD pipelines.
  2. ansible-playbook playbook.yml --vault-id @prompt
    Purpose: Prompt for Vault password during automated runs.
  3. ansible-playbook playbook.yml --diff --check
    Purpose: Check what changes would be made and show differences.
  4. ansible-playbook playbook.yml --list-tasks
    Purpose: List all tasks in a playbook without executing them.
  5. ansible-playbook playbook.yml --tags "update" --extra-vars "version=3.0"
    Purpose: Run tasks tagged with update and set version to 3.0.
  6. ansible-playbook playbook.yml --limit "production"
    Purpose: Restrict playbook execution to hosts in the production group.
  7. ansible-playbook playbook.yml --verbose
    Purpose: Provide detailed output for debugging purposes.
  8. ansible-playbook playbook.yml --ask-pass
    Purpose: Prompt for SSH password for remote connections.
  9. ansible-playbook playbook.yml --ask-become-pass
    Purpose: Request password for privilege escalation.
  10. ansible-playbook playbook.yml --start-at-task="Setup"
    Purpose: Begin execution at a specific task in the playbook.