Ansible Commands
Ansible has multiple commands, each serving different functions. Below are some commonly used commands and their parameters.
Common Commands
# Set the ansible configuration file
ansible-config
# View ansible related modules and module documentation
ansible-doc
# View ansible's inventory
ansible-inventory
# Execute ad-hoc tasks
ansible
# Execute ansible playbooks
ansible-playbook
# Set ansible roles
ansible-galaxy
# Encrypt or decrypt ansible related files
ansible-vault
Common Parameters for Ansible-config
# Generate an ansible configuration file with all configurations commented out
ansible-config init --disabled -t all > ./ansible.cfg
Common Parameters for Ansible-doc
# List all supported modules
ansible-doc -l
# View module parameters
ansible-doc -s <module_name>
# View module examples
ansible-doc <module_name>
# This can be quickly viewed by searching for EXAMPLE
# List modules by type
ansible-doc -t cache -l # List all cache modules
Common Parameters for Ansible-inventory
# List all hosts
ansible-inventory -i inventory --list
-i # Specify the inventory file
--list # List all hosts
# View a specific host
ansible-inventory -i inventory --host server1
# Display in a tree format and show variables
ansible-inventory -i inventory --graph --vars
# Output inventory in toml format
ansible-inventory -i inventory --list --toml
# Output inventory in yaml format
ansible-inventory -i inventory --list --yaml
# Output content to a file
ansible-inventory -i inventory --list --output file
Common Parameters for Ansible
ansible all -u root -k -m command -a 'pwd chdir=/opt removes=/root/anaconda-ks.cfg'
all # Indicates the hosts or host groups to execute tasks on
-m # Followed by the module to use
command # The module being used
-u root # Specify the connection user
-k # Manually input the password
-a # Followed by the module parameters
Sometimes you may come across the term Ansible Ad-Hoc, which refers to the
<span>ansible</span>
command, used to execute simple tasks in an ad-hoc manner (using one module to perform one task at a time).
Common Parameters for Ansible-playbook
ansible-playbook --become --become-method sudo \
--become-user root -i inventory \
--ask-pass --ask-become-pass playbook.yaml
--become # Enable privilege escalation
--become-method # Set the method for privilege escalation
--become-user # Specify the user to escalate to
--ask-pass # Input the remote user password
--ask-become-pass # Set the privilege escalation password
# Check if the playbook has syntax errors
ansible-playbook --syntax-check playbook.yaml
# Execute the playbook without making changes
ansible-playbook --check playbook.yaml
Common Options for Ansible-galaxy
# List all roles
ansible-galaxy role list
# Initialize a role named role1
ansible-galaxy role init role1
# Download collections
ansible-galaxy collection download
Common Options for Ansible-vault
# Create an encrypted file
ansible-vault create test1
# Encrypt an existing file
ansible-vault encrypt test
# Encrypt the string username=root
ansible-vault encrypt_string "username=root"
# Edit an encrypted file
ansible-vault edit test
# View an encrypted file
ansible-vault view test
# Change the file password
ansible-vault rekey test
# Decrypt a file
ansible-vault decrypt test2