Operations 11 min read

Master 8 Essential Ansible Modules for Efficient Automation

This article introduces eight essential Ansible modules—file, copy, template, fetch, and get_url—explaining their parameters, usage examples, and how they simplify automation tasks in operations, with code snippets and reference links for deeper learning.

Linux Ops Smart Journey
Linux Ops Smart Journey
Linux Ops Smart Journey
Master 8 Essential Ansible Modules for Efficient Automation

Introduction

With the rise of cloud computing and container technology, automation in operations has become increasingly important. Ansible, a agent‑less configuration management tool, is popular for its simplicity and efficiency.

File‑related modules

file module

The

file

module creates files or directories on the target host.

path (required) : path of the managed file or directory

state : absent, directory, file, link, touch

owner : owner of the file/directory

group : group owner

mode : permissions

src : source path for links

<code># create /tmp/test file
ansible -i hosts 172.139.20.121 -m file -a "path=/tmp/test state=touch"
# set permissions
ansible -i hosts 172.139.20.121 -m file -a "path=/tmp/test state=file mode=0600"
# create directory with owner
ansible -i hosts 172.139.20.121 -m file -a "path=/tmp/test state=directory owner=ops group=ops"
# create symbolic link
ansible -i hosts 172.139.20.121 -m file -a "state=link src=/tmp/test path=/tmp/link_test"</code>

copy module

The

copy

module copies files or directories to the target host.

src : local path of the file/directory to copy

dest (required) : remote absolute path

owner/group/mode : same as file module

backup : create timestamped backup before overwriting

remote_src : whether the source is on the remote host (default no)

<code># copy directory
ansible -i hosts 172.139.20.121 -m copy -a "src=/tmp/testDir dest=/tmp"
# copy with ownership and mode
ansible -i hosts 172.139.20.121 -m copy -a "src=/tmp/testDir dest=/tmp owner=ceph group=ceph mode=777"
# copy with backup
ansible -i hosts 172.139.20.121 -m copy -a "src=/tmp/testDir dest=/tmp backup=yes"</code>

template module

The

template

module renders a Jinja2 template on the control node and copies the result to the target host.

src (required) : path to the Jinja2 template on the control node

dest (required) : destination path on the remote host

owner/group/mode : same as file module

backup : create timestamped backup of the destination file

<code># using extra variables
ansible -i hosts 172.139.20.121 -e db_host=127.0.0.1 -e db_port=5432 -e db_password=123456 -m template -a "src=/tmp/config.j2 dest=/tmp/config backup=yes"
# using inventory variables
ansible -i hosts 172.139.20.121 -m template -a "src=/tmp/config.j2 dest=/tmp/config backup=yes"</code>

Download modules

fetch module

The

fetch

module copies a file from the remote host back to the Ansible control machine.

src (required) : file on the remote host

dest (required) : directory on the control machine where the file will be saved

<code>ansible -i hosts 172.139.20.121 -m fetch -a "src=/tmp/config dest=/tmp"</code>

get_url module

The

get_url

module downloads a file from an HTTP/HTTPS/FTP URL to the target node.

url (required) : source URL

dest (required) : absolute path on the remote host

owner/group/mode/backup: same as file module

client_cert, client_key, url_username, url_password: optional authentication parameters

<code>ansible -i hosts 172.139.20.75 -m get_url -a "url=https://core.jiaxzeng.com/api/v2.0/systeminfo/getcert dest=/etc/docker/certs.d/core.jiaxzeng.com/ca.crt"
ansible -i hosts local -m get_url -a "url=https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/vim-common-7.4.629-7.el7.x86_64.rpm dest=/tmp/"</code>

Reference documentation

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

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

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

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

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

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

Conclusion

After reviewing these modules, you should have a deeper understanding of basic Ansible operations. They can be used individually or combined to tackle complex automation tasks, making daily maintenance and large‑scale deployments more efficient and reliable.

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