Create a symbolic link (also symlink or soft link) in Linux - Ansible module file
How to create an "example" symbolic/soft link to "/proc/cpuinfo" on Linux filesystem using Ansible


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 a hardlink use see the following parameters of Ansible file module.
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.
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
demo
Let’s jump into a real-life playbook on how to create a symbolic link with Ansible.
code
- create_symlink.yml
---
- 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
$ ansible-playbook -i demo/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
$ ssh [email protected]
[[email protected] ~]$ ls -al
total 16
drwx------. 4 devops wheel 111 Nov 14 14:26 .
drwxr-xr-x. 5 root root 50 Nov 8 17:07 ..
drwx------. 3 devops wheel 17 Sep 6 05:53 .ansible
-rw-------. 1 devops wheel 3055 Nov 14 14:26 .bash_history
-rw-r--r--. 1 devops wheel 18 Dec 4 2020 .bash_logout
-rw-r--r--. 1 devops wheel 141 Dec 4 2020 .bash_profile
-rw-r--r--. 1 devops wheel 376 Dec 4 2020 .bashrc
drwx------. 2 devops wheel 94 Sep 16 10:09 .ssh
[[email protected] ~]$
after execution
$ ssh [email protected]
[[email protected] ~]$ ls -al
total 16
drwx------. 4 devops wheel 126 Nov 14 14:29 .
drwxr-xr-x. 5 root root 50 Nov 8 17:07 ..
drwx------. 3 devops wheel 17 Sep 6 05:53 .ansible
-rw-------. 1 devops wheel 3067 Nov 14 14:28 .bash_history
-rw-r--r--. 1 devops wheel 18 Dec 4 2020 .bash_logout
-rw-r--r--. 1 devops wheel 141 Dec 4 2020 .bash_profile
-rw-r--r--. 1 devops wheel 376 Dec 4 2020 .bashrc
drwx------. 2 devops wheel 94 Sep 16 10:09 .ssh
lrwxrwxrwx. 1 devops wheel 13 Nov 14 14:29 example -> /proc/cpuinfo
[[email protected] ~]$ cat example | less
[[email protected] ~]$
Recap
Now you know how to create a symbolic link or soft link in Linux with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate