Loading...

Watch: Ansible File Module Demo: Create Symlinks Easily

Discover how to create symlinks with Ansible's file module in this easy-to-follow Playbook. Master Ansible automation with practical examples.

How to create a symbolic link with Ansible?

I'm going to show you a live Playbook 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 a hardlink use see the following parameters of [Ansible file module](/articles/create-a-hard-link-ansible-module-file).

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.

## Playbook

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

code

  • create_symlink.yml

``yaml

---

  • name: file module demo

hosts: all

vars:

mylink: "~/example"

mysrc: "/proc/cpuinfo"

tasks:

- name: Creating a symlink

ansible.builtin.file:

src: "{{ mysrc }}"

dest: "{{ mylink }}"

state: link

`

execution

`bash

$ ansible-playbook -i Playbook/inventory create\ symlink/file.yml

PLAY [file module demo] *

TASK [Gathering Facts]

ok: [demo.example.com]

TASK [Creating a symlink] *

changed: [demo.example.com]

PLAY RECAP **

demo.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

`

before execution

``bash

$ ssh [email protected]

[devops@demo ~]$ ls -al

total

Read the full tutorial: Ansible File Module Demo: Create Symlinks Easily