1. Basic Syntax
ansible <target host or group> -m <module name> -a "<module parameters>"
•<span><target host or group></span>
: Can be a host, host group in Inventory, or <span>all</span>
to indicate all hosts.•<span>-m <module name></span>
: Specifies the module to use (e.g., <span>ping</span>
, <span>shell</span>
, <span>yum</span>
, etc.).•<span>-a "<module parameters>"</span>
: Parameters passed to the module.
2. Common Options
|
|
<span>-i <Inventory file></span> |
<span>/etc/ansible/hosts</span> ). |
<span>-u <username></span> |
|
<span>-k</span> |
|
<span>--become</span> |
<span>sudo</span> ). |
<span>--become-user</span> |
<span>root</span> ). |
<span>-K</span> |
<span>sudo</span> password). |
<span>-f <concurrency></span> |
|
<span>-v</span> |
<span>-vvv</span> shows more detailed information). |
<span>--check</span> |
|
<span>--diff</span> |
<span>--check</span> ). |
3. Common Ad-hoc Command Examples
3.1 Check Host Connectivity
ansible all -m ping
3.2 Execute Commands on All Hosts
# Execute uptime command
ansible all -a "uptime"
# Execute shell command
ansible all -m shell -a "df -h"
3.3 Manage Packages
# Install package (using yum module)
ansible webservers -m yum -a "name=httpd state=present"
# Uninstall package
ansible webservers -m yum -a "name=httpd state=absent"
3.4 Manage Services
# Start service
ansible webservers -m service -a "name=httpd state=started"
# Stop service
ansible webservers -m service -a "name=httpd state=stopped"
# Restart service
ansible webservers -m service -a "name=httpd state=restarted"
3.5 File Operations
# Copy file
ansible webservers -m copy -a "src=/local/path/file.txt dest=/remote/path/file.txt"
# Change file permissions
ansible webservers -m file -a "path=/remote/path/file.txt mode=644 owner=root group=root"
# Create directory
ansible webservers -m file -a "path=/remote/path/new_dir state=directory"
3.6 User Management
# Create user
ansible all -m user -a "name=testuser password=123456"
# Delete user
ansible all -m user -a "name=testuser state=absent"
3.7 Cron Job Management
# Add cron job
ansible all -m cron -a "name='backup' minute=0 hour=2 job='/path/to/backup.sh'"
# Delete cron job
ansible all -m cron -a "name='backup' state=absent"
3.8 Privilege Escalation to Execute Tasks
# Execute command using sudo
ansible all --become -a "yum update -y"
# Specify privilege escalation user
ansible all --become --become-user=admin -a "whoami"
3.9 Check Host Information
# Gather host information (using setup module)
ansible all -m setup
# View specific information (e.g., IP address)
ansible all -m setup -a "filter=ansible_default_ipv4"
3.10 Dry Run
# Simulate executing a task
ansible all -m yum -a "name=httpd state=present" --check
4. Advanced Usage
4.1 Using Variables
ansible all -m ping debug -a "msg='Hello, {{ ansible_hostname }}'"
4.2 Limiting Executing Hosts
# Execute only on specific host
ansible web1.example.com -m ping
# Use pattern matching
ansible 'web*' -m ping
4.3 Concurrent Execution
# Specify concurrency to 10
ansible all -m ping -f 10
4.4 Using Tags
# Run tasks with specific tags only
ansible-playbook playbook.yml --tags "apache"
4.5 Using Ansible Vault to Encrypt Data
# Run tasks that require Vault password
ansible all -m debug -a "msg='{{ vault_var }}'" --ask-vault-pass
5. Common Modules
|
|
<span>ping</span> |
|
<span>shell</span> |
|
<span>command</span> |
|
<span>yum</span> |
|
<span>apt</span> |
|
<span>service</span> |
|
<span>copy</span> |
|
<span>file</span> |
|
<span>user</span> |
|
<span>cron</span> |
|
<span>setup</span> |
|
<span>debug</span> |
|
6. Notes
1.Module Selection: Prefer using Ansible modules instead of executing commands directly, as modules have idempotency (consistent results across multiple executions).2.Privilege Operations: If privilege escalation is needed, remember to use <span>--become</span>
or <span>-b</span>
options.3.Concurrency Control: Control concurrency with <span>-f</span>
parameter to avoid overwhelming target hosts.4.Security: Avoid passing sensitive information (like passwords) directly in Ad-hoc commands; use Ansible Vault for encryption.