Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

Introduction in one sentence: Ad-hoc commands are Ansible’s “quick knife”, allowing you to manage servers in bulk with a single command in the command line.

Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

1. What are Ad-hoc Commands?

In Ansible, there are two main execution methods:

1.Ad-hoc commands: Execute a single task directly in the command line, suitable for temporary bulk operations.

2.Playbook: Automation scripts written in YAML files, suitable for complex, multi-step tasks.

Characteristics of Ad-hoc commands:

·Fast

·One-time

·No need to write scripts

Common scenarios:

·Test server connectivity

·Bulk install software

·Bulk modify file permissions

·Bulk restart services

Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

2. Ad-hoc Command Syntax

ansible<host group> -m <module name> -a “<module parameters>”[options]

Common options:

Option

Function

-m

Specify module name (default is command module)

-a

Pass module parameters

-k

Interactive input of SSH password (recommended to configure passwordless login)

-b

Execute with sudo privileges (equivalent to root permissions)

–check

Dry run mode, does not actually execute

–limit

Limit the range of hosts to execute

Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

3. Detailed Explanation of Common Modules

1. ping — Test connectivity

ansible all -m ping

Returns pong indicating the connection is normal.

Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

2. command — Execute command (without using Shell)

ansible all -m command -a“uptime”

Cannot use Shell features (such as pipes, redirection).

Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

3. shell — Execute Shell command

ansible all -m shell -a“cat /etc/os-release | grep PRETTY_NAME”

Supports Shell features such as pipes and variables.

Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

4. copy — Copy files to managed nodes

ansible webservers -m copy -a“src=./index.html dest=/var/www/html/index.html owner=nginx group=nginx mode=0644”

·src: Source file path on the control node

·dest: Target path on the managed node

·owner/group/mode: File permission settings

Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

5. file — Manage files and directories

ansible webservers -m file -a“path=/var/www/html state=directory mode=0755”

·Create directory

ansible webservers -m file -a“path=/tmp/test.txt state=touch”

·Delete file

ansible webservers -m file -a“path=/tmp/test.txt state=absent”

Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

6. yum/apt — Install and uninstall software packages

·CentOS / RHEL:

ansible webservers -m yum -a“name=nginx state=present”

·Ubuntu / Debian:

ansible webservers -m apt -a“name=nginx state=present update_cache=yes”

Common state:

·present:Install

·absent:Uninstall

·latest:Install the latest version

Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

7. service — Manage services

ansible webservers -m service -a“name=nginx state=started enabled=yes”

·started:Start

·stopped:Stop

·restarted:Restart

·enabled:Enable on boot

Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

8. user — Manage users

ansible all -m user -a“name=dev password={{ ‘123456’ | password_hash(‘sha512’) }} state=present”

It is recommended to manage passwords with vars and not write them in plain text in the command.

Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

4. Practical Ad-hoc Examples

1. Batch view disk space

ansible all -m shell -a“df -h”

2. Batch update packages

ansible all -m yum -a“name=* state=latest”

3. Batch synchronize time

ansible all -m shell -a“ntpdate ntp.aliyun.com”

Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

5. Common Issues and Troubleshooting

1.SSH connection failed

oConfirm if passwordless login is configured successfully

oCheck if /etc/ansible/hosts is configured correctly

oIf the port is not 22, add -e ansible_port=xxx

2.Insufficient permissions

oAdd -b to use sudo privileges

oConfirm that the managed node’s sudo configuration allows passwordless execution

3.Command execution result garbled

oUnify the LANG encoding on the control node and managed node, for example:

exportLANG=en_US.UTF-8

Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

6. Summary and Next Article Preview

In this article, we learned:

·Using Ad-hoc commands to quickly execute tasks in bulk

·How to use common modules

·Practical batch management examples

The next article “Introduction to Playbooks and Basics of YAML” will take us into Ansible’s core capability—writing repeatable automation tasks with Playbooks, truly achieving “write once, run multiple times”.

Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

💡 Tip: In actual production, temporary operations can use Ad-hoc, but repetitive operations are recommended to be written as Playbooks for easier maintenance and version management.

Ansible Series Tutorial (Part 2): Ad-hoc Commands and Common Modules

Leave a Comment