AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible Create Symlink: file Module with state=link (Guide) — Video Tutorial
How to create symbolic links (symlinks) in Ansible with the file module state=link. Create, manage, remove symlinks and hard links.
What You'll Learn
- Ansible create a symbolic link
- Parameters
- code
- execution
- before execution
- after execution
- Conclusion
- Related Articles
Full Tutorial Content
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 devops@demo.example.com
[devops@demo ~]$ 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
[devops@demo ~]$
```
after execution
```bash
$ ssh devops@demo.example.com
[devop
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 3 min
- Category: troubleshooting
Read the full written article: Ansible Create Symlink: file Module with state=link (Guide)
Related Video Tutorials
- Ansible Create Hard Link & Symlink: file Module Guide — How to create hard links and symbolic links with Ansible file module. Use state=hard and state=link with permissions, force, and practical examples.
- Ansible Create File with Content: copy Module content Parameter — How to create files with content in Ansible using the copy module content parameter. Write text, YAML, JSON to files without templates.
- Ansible Create Empty File: Touch Files with file Module (Guide) — How to create empty files in Ansible with the file module state=touch. Create files, set permissions and ownership on new files.
- Ansible Rename File: Move & Rename Files with copy + file Modules — How to rename files and directories in Ansible using copy and file modules. Move files, rename with register, use command module.
- Ansible Check If Directory Exists: stat Module Guide — How to check if files and directories exist with Ansible stat module. Use stat results in conditionals, check permissions, size, and timestamps.
- Ansible Create Directory: file Module with state=directory (Guide) — How to create directories in Ansible with the file module state=directory. Set permissions, owner, group, recursive creation.