Create a symbolic link in Linux — Ansible module file

How to create an ”example” symbolic/soft link to “/proc/cpuinfo” on Linux filesystem using Ansible

Ansible Pilot
4 min readNov 15, 2021

--

How to create a symbolic link with Ansible?

I’m going to show you a live demo and some simple Ansible code.

I’m Luca Berton and welcome to today’s episode of Ansible Pilot

Ansible create a symbolic link

  • ansible.builtin.file
  • Manage files and file properties

Today we’re talking about the Ansible module file.
The full name is ansible.builtin.file, which means that is part of the collection of modules “builtin” with ansible and shipped with it.
It’s a module pretty stable and out for years.
It works in a different variety of operating systems.
It manages files and file properties.
For Windows targets, use the `ansible.windows.win_file` module instead.

Parameters

  • src string — symlink path
  • dest string — destination file path
  • state string — file/absent/symbolic link/hard/link/touch
  • mode/owner/group — permission
  • setype/seuser/selevel — SELinux

This module has some parameters to perform any tasks.
The two required fields are "src" and “dest” which specify the filesystem paths of the link and the target file.

The state defines the type of object we are modifying, the default is “file” but for our use case, we need the “link” option.
Let me highlight also the permission and SELinux parameters.

demo

Let’s jump into a real-life playbook on how to create a symbolic link with Ansible.

  • create_symlink.yml
---
- name: file module demo
hosts: all
vars:
mylink: "~/example"
mysrc: "/proc/cpuinfo"
tasks:
- name: Creating a…

--

--

Ansible Pilot

I help creative Automation DevOps, Cloud Engineer, System Administrator, and IT Professional to succeed with Ansible Technology to automate more things everyday