Operations 9 min read

Mastering Ansible: 8 Essential Modules to Automate Your Ops

This article explores eight of the most commonly used Ansible modules—including setup, command, shell, and script—detailing how they collect host information, filter data, execute commands, and run scripts, with practical code examples and tips to streamline everyday automation tasks.

Linux Ops Smart Journey
Linux Ops Smart Journey
Linux Ops Smart Journey
Mastering Ansible: 8 Essential Modules to Automate Your Ops

Collecting Host Information Module

This module is automatically invoked by a playbook to gather useful variables about remote hosts, which can be used within the playbook. It can also be executed directly via

/usr/bin/ansible

to inspect available host variables.

1. setup subset information

<code># Default: gather all subsets
$ ansible -i hosts 172.139.20.121 -m setup

# Gather only the network subset
$ ansible -i hosts 172.139.20.170 -m setup -a 'gather_subset=!all,!min,network'
</code>

Tip: Subsets include all, min, hardware, network, virtual, ohai, and facter.

2. setup filter information

<code># Filter all IP addresses of the host
$ ansible -i hosts 172.139.20.170 -m setup -a 'filter=ansible_all_ipv4_addresses'

# Filter host CPU architecture
$ ansible -i hosts 172.139.20.121 -m setup -a 'filter=ansible_architecture'
</code>

Tip: Common fields:

ansible_all_ipv4_addresses – all IP addresses

ansible_default_ipv4 – default IPv4 interface

ansible_architecture – CPU architecture

ansible_distribution – OS name

ansible_distribution_major_version – OS major version

ansible_distribution_version – OS version

ansible_kernel – kernel version

ansible_memtotal_mb – total memory

ansible_memfree_mb – free memory

ansible_processor_vcpus – total vCPUs

ansible_processor_count – CPU count

ansible_processor_cores – cores per CPU

ansible_hostname – short host name

ansible_fqdn – fully qualified domain name

Command‑Related Modules

command module

<code>$ ansible -i hosts k8sMaster -m command -a 'hostname'
172.139.20.151 | CHANGED | rc=0 >>
 k8s-master03
172.139.20.121 | CHANGED | rc=0 >>
 k8s-master01
172.139.20.176 | CHANGED | rc=0 >>
 k8s-master02

$ ansible -i hosts k8sMaster -a ' ls /etc/hosts '
172.139.20.176 | CHANGED | rc=0 >>
 /etc/hosts
172.139.20.121 | CHANGED | rc=0 >>
 /etc/hosts
172.139.20.151 | CHANGED | rc=0 >>
 /etc/hosts
</code>

Tip:

When no module is specified, the default is

command

.

The command is not processed by a shell, so variables like

$HOME

and characters such as

&lt;

,

&gt;

,

|

,

;

,

&amp;

are not interpreted. Use the

shell

module for those features.

shell module

<code>$ ansible -i hosts k8sMaster -m shell -a 'cat /etc/hosts | grep core.jiaxzeng.com'
172.139.20.151 | CHANGED | rc=0 >>
172.139.20.100  core.jiaxzeng.com
172.139.20.121 | CHANGED | rc=0 >>
172.139.20.100  core.jiaxzeng.com simple.jiaxzeng.com ops.jiaxzeng.com
172.139.20.176 | CHANGED | rc=0 >>
172.139.20.100  core.jiaxzeng.com

$ ansible -i hosts k8sMaster -m shell -a "awk '/core.jiaxzeng.com/ {print $0}' /etc/hosts"
172.139.20.151 | CHANGED | rc=0 >>
172.139.20.100  core.jiaxzeng.com
172.139.20.121 | CHANGED | rc=0 >>
172.139.20.100  core.jiaxzeng.com simple.jiaxzeng.com ops.jiaxzeng.com
172.139.20.176 | CHANGED | rc=0 >>
172.139.20.100  core.jiaxzeng.com
</code>

script module

Local scripts are transferred to remote nodes, executed, and then the script files are removed.

<code># Test shell script
cat <<'EOF' | sudo tee /opt/test.sh > /dev/null
#!/bin/bash

echo "This hostname is $HOSTNAME"
echo "This hostname is $(hostname -I)"
echo "The first parameter: $1"
echo "The all parameter: $*"
EOF

$ ansible -i hosts 172.139.20.170 -m script -a "/opt/test.sh fds fs"
172.139.20.170 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 172.139.20.170 closed.\r\n",
    "stderr_lines": ["Shared connection to 172.139.20.170 closed."],
    "stdout": "\r\nThis hostname is net_proxy.ecloud.com\r\nThis hostname is 192.168.122.13 172.139.20.101 172.139.20.170 172.17.0.1 2001:db8:dead:beef:fe::12e8 \r\nThe first parameter: fds\r\nThe all parameter: fds fs\r\n",
    "stdout_lines": ["", "This hostname is net_proxy.ecloud.com", "This hostname is 192.168.122.13 172.139.20.101 172.139.20.170 172.17.0.1 2001:db8:dead:beef:fe::12e8 ", "The first parameter: fds", "The all parameter: fds fs"]
}
</code>

Reference Documentation

https://docs.ansible.com/ansible/2.9/modules/setup_module.html

https://docs.ansible.com/ansible/2.9/modules/command_module.html

https://docs.ansible.com/ansible/2.9/modules/shell_module.html

https://docs.ansible.com/ansible/2.9/modules/script_module.html

https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html

Conclusion

By studying these modules, you should now have a deeper understanding of Ansible's basic operations. These modules can be used individually or combined to solve more complex automation challenges, whether for routine maintenance or large‑scale deployments, helping you work more efficiently and reliably as an operations engineer.

automationConfiguration ManagementDevOpsModulesAnsible
Linux Ops Smart Journey
Written by

Linux Ops Smart Journey

The operations journey never stops—pursuing excellence endlessly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.