Operations 9 min read

Unlock Hidden Power of Ansible: Master unarchive, lineinfile, and synchronize Modules

This article explores three lesser‑known but highly useful Ansible modules—unarchive, lineinfile, and synchronize—detailing their key parameters, practical examples, and tips so you can streamline deployments, configuration edits, and file syncing in automated operations.

Linux Ops Smart Journey
Linux Ops Smart Journey
Linux Ops Smart Journey
Unlock Hidden Power of Ansible: Master unarchive, lineinfile, and synchronize Modules

In the fast‑moving IT industry, automation is essential for boosting efficiency and reducing errors. Ansible, a powerful configuration‑management tool, is celebrated for its simplicity and agent‑less operation, yet many of its useful modules remain under the radar.

unarchive Module

The unarchive module simplifies extracting compressed packages (e.g., .zip, .tar.gz) to a target directory during software deployment, allowing you to set ownership and permissions for the extracted files.

src (required) : Path to the archive file.

dest (required) : Destination directory for extraction.

owner / group / mode : Permissions and ownership for the extracted files.

remote_src : Set to

yes

if the archive resides on the remote host; default is

no

(local to the Ansible controller).

Example configuration:

<code># 1. Archive on the Ansible host, extract to remote host /opt
ansible -i hosts 172.139.20.121 -m unarchive -a "src=/home/ops/grafana-7.3.12.tgz dest=/opt owner=ops" -b

# 2. Archive already on the remote host, extract with group and remote_src
ansible -i hosts 172.139.20.121 -m unarchive -a "src=/home/ops/grafana-7.3.12.tgz dest=/opt owner=ops group=ops remote_src=yes" -b</code>

lineinfile Module

The lineinfile module provides a straightforward way to modify text files by searching for a specific line and replacing or adding it based on conditions—ideal for tweaking system configuration files.

path (required) : File to edit.

regexp : Regular expression to locate the target line.

line : Content to insert or replace.

backrefs : Enable back‑references in

line

when set to

yes

.

insertafter : Position for new line (EOF, BOF, regex, or string).

backup : Create a backup of the original file.

validate : Command to verify file syntax;

%s

is replaced with a temporary file name.

state :

present

ensures the line exists;

absent

ensures it is removed.

Example configurations:

<code># Replace SELINUX line or add it if missing
ansible -i hosts 172.139.20.121 -m lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=disabled' backup=yes state=present" -b

# Comment out http_proxy lines using backrefs
ansible -i hosts 172.139.20.121 -m lineinfile -a "path=/tmp/grafana-values.yaml regexp='^(\s*)(http_proxy:.*)$' line='\1# \2' backrefs=yes state=present"

# Insert a new line after a marker when regexp does not match
ansible -i hosts 172.139.20.121 -m lineinfile -a "path=/tmp/grafana-values.yaml regexp='^  http_proxy:.*$' line='  http_proxy=\"http://172.139.20.170:3888\"' insertafter='env:' state=present"

# Delete matching lines (sed‑like delete)
ansible -i hosts 172.139.20.121 -m lineinfile -a "path=/tmp/grafana-values.yaml regexp='^(\s*)(http_proxy:.*)$' state=absent"

# Validate sudoers file after modification
ansible -i hosts 172.139.20.121 -m lineinfile -a "path=/etc/sudoers.d/test regexp='^(.*)(ALL)$' line='\1 NOPASSWD: \2' backrefs=yes validate='visudo -cf %s' state=present" -b</code>

synchronize Module

The synchronize module wraps the

rsync

command to synchronize files between a control machine and remote hosts (or between two remote hosts). It transfers only changed data, making it fast and suitable for backups or large dataset updates.

src (required) : Source path.

dest (required) : Destination path.

owner / group / mode : Permissions for synchronized files.

Tip:

Both control and target machines must have

rsync

installed.

If Ansible runs with sudo (

-b

), configure password‑less sudo on the control host.

Example configuration:

<code># Sync /etc/hosts from the Ansible controller to the remote host
ansible -i hosts 172.139.20.121 -m synchronize -a "src=/etc/hosts dest=/opt" -b</code>

Conclusion

Although the unarchive , lineinfile , and synchronize modules may not receive as much attention as other Ansible features, each solves specific problems with great efficiency. Mastering these modules enriches your automation scripts, making daily operations smoother and more flexible.

automationConfiguration ManagementAnsiblelineinfilesynchronizeunarchive
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.