Summary of Common Ansible Commands: Essential Collection for Automation Operations!

Today, let’s share some common commands of Ansible

1 Basic Commands

Command Description
<span>ansible --version</span> Check Ansible version
<span>ansible all -m ping</span> Test connectivity to all hosts
<span>ansible <host group> -m ping</span> Ping specified host group
<span>ansible webservers -m command -a "uptime"</span> Execute shell command on target host
<span>ansible all -i <ip>, -m ping</span> Temporarily execute command (comma cannot be omitted)

2 Inventory

Command Description
<span>ansible-inventory --list</span> Display the list of hosts in the current inventory (in JSON format)
<span>ansible-inventory --graph</span> Graphically display the structure of host groups
<span>ansible-inventory -i <inventory file> --list</span> Display host list using specified inventory file

3 Module Usage (Common Modules)

Command Description
<span>-m ping</span> Test connectivity
<span>-m shell -a "<command>"</span> Execute command using shell module
<span>-m command -a "<command>"</span> Use command module (does not support pipes, redirection, etc.)
<span>-m copy -a "src=/etc/hosts dest=/tmp/hosts"</span> Copy file to remote host
<span>-m fetch -a "src=/var/log/messages dest=/tmp"</span> Pull file from remote host
<span>-m file -a "path=/tmp/test state=touch"</span> Create an empty file
<span>-m file -a "path=/tmp/test state=absent"</span> Delete file
<span>-m yum -a "name=httpd state=present"</span> Install package
<span>-m service -a "name=httpd state=started enabled=true"</span> Start and enable service to start on boot
<span>-m user -a "name=testuser state=present"</span> Create user
<span>-m group -a "name=testgroup state=present"</span> Create user group

4 Playbook Related

Command Description
<span>ansible-playbook site.yml</span> Execute playbook
<span>ansible-playbook -i hosts site.yml</span> Execute with specified hosts file
<span>ansible-playbook --check site.yml</span> Simulate execution (will not actually change anything)
<span>ansible-playbook --syntax-check site.yml</span> Syntax check
<span>ansible-playbook -v/-vv/-vvv</span> Increase verbosity of log output
<span>ansible-playbook site.yml --start-at-task="task name"</span> Start execution from specified task

5 Keys and Permissions

Command Description
<span>ansible all -m ping -u root</span> Execute as specified user
<span>ansible all -m ping -u root --ask-pass</span> Prompt for SSH password
<span>ansible-playbook site.yml -K</span> Prompt for sudo password
<span>ansible-playbook site.yml --ask-vault-pass</span> Prompt for encrypted variable password

6 Vault (Sensitive Information Encryption)

Command Description
<span>ansible-vault create secret.yml</span> Create encrypted file
<span>ansible-vault edit secret.yml</span> Edit encrypted file
<span>ansible-vault view secret.yml</span> View encrypted file
<span>ansible-vault encrypt file.yml</span> Encrypt file
<span>ansible-vault decrypt file.yml</span> Decrypt file
<span>ansible-playbook playbook.yml --vault-password-file ~/.vault_pass.txt</span> Read password from file
<span>ansible-playbook playbook.yml --ask-vault-pass</span> Prompt for password when running playbook

7 Useful Tips

Command Description
<span>ansible-doc -l</span> View all modules
<span>ansible-doc <module name></span> View description of specified module
<span>ansible-doc -s copy</span> Display module parameter structure (recommended)
<span>ansible-playbook --tags "install"</span> Execute tasks with specified tags
<span>ansible-playbook --skip-tags "install"</span> Skip tasks with specified tags
<span>ansible-playbook -e "var1=value1"</span> -e: Temporarily pass variables

If you find it useful, remember to like and follow!If you want to add to the discussion group or need technical support, add WeChat: lige_linux

Previous Excellent Articles:

Detailed Summary of K8S Commands [Personal Collection]| K8S Cluster Deployment | K8S Storage Practical Cases |K8S Certificate Renewal for Ten Years | K8S Deployment of Prometheus | Rancher Deployment and Management of K8S |Jenkins Installation and Deployment | Gitlab Installation and Deployment | Service Mesh Istio Installation and Practice |Building an Enterprise-Level Harbor Repository | K8S Integration with Harbor Repository | Summary of Common Docker Commands |Solutions for Docker Image Download Issues | Three Methods to Install Docker | Summary of Basic Docker Concepts |Oracle 19C RAC Cluster Setup | Summary of Oracle Cluster Management Commands | MySQL Cluster Installation and Deployment | MySQL One-Click Backup Script | MySQL Cluster Directory Migration | Redis Three Masters and Three Slaves Cluster Deployment |150 Common Linux Commands | 8 Interesting Linux Commands | Summary of Network Card Configuration Methods for Various Versions of Mainstream Linux Operating Systems, Recommended for Collection! |Detailed Explanation of Firewalld Firewall | Building Internal Yum Repository | Comprehensive Disk Expansion Methods | Out-of-Band Management Knowledge for Servers

Leave a Comment