Bootstrap FreeKB - Ansible - Create a symbolic link using the file module
Ansible - Create a symbolic link using the file module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

Let's say you want to create a symbolic link, so that /tmp/foo.txt is symbolically linked to /tmp/bar.txt, like this.

/tmp/foo.txt -> /tmp/bar.txt

 

The file module with state: link is used to create a symbolic link. In this example, a symbolic link from foo.txt to bar.txt is created. The src file must already exist and the dest file should not exist.

---
- hosts: localhost
  tasks:
  - name: create symbolic link /tmp/bar.txt -> /tmp/foo.txt
    file:
      src: /tmp/foo.txt
      dest: /tmp/bar.txt
      state: link
...

 

If the source file or directory does not exist, something like this should be returned.

TASK [create symbolic link /tmp/bar.txt -> /tmp/foo.txt]
ok: [server1.example.com]: FAILED! => {
  "changed": false,
  "msg": "src file does not exist, use \"force=yes\" if you really want to create the link: /tmp/foo",
  "path": "/tmp/bar",
  "src": "/tmp/foo" }

 

force: yes can be used to create the symbolic link when the source file or directory does not exist,

---
- hosts: localhost
  tasks:
  - name: create symbolic link /tmp/bar.txt -> /tmp/foo.txt
    file:
      src: /tmp/foo.txt
      dest: /tmp/bar.txt
      state: link
      force: yes
...

 

Or you could use the ignore_errors parameter.

---
- hosts: localhost
  tasks:
  - name: create symbolic link /tmp/bar.txt -> /tmp/foo.txt
    file:
      src: /tmp/foo.txt
      dest: /tmp/bar.txt
      state: link
    ignore_errors: true
...

 

Let's say the source file or directory (/tmp/foo.txt in this example) is owned by john.doe and the Ansible playbook is run by jane.doe. In this scenario, the dest symbolic link will be owned by jane.doe. follow: false can be used so that the dest symlink has the same owner as the source file or directory.

---
- hosts: localhost
  tasks:
  - name: create symbolic link /tmp/bar.txt -> /tmp/foo.txt
    file:
      src: /tmp/foo.txt
      dest: /tmp/bar.txt
      state: link
      follow: false
...

 

The stat module can be used to store the stats of the files in a variable. The debug module can be used to print the output.

---
- hosts: localhost
  tasks:
  - name: store the stats of /tmp/foo.txt in the 'foo' variable
    stat:
      path: /tmp/foo.txt
    register: foo

  - name: store the stats of /tmp/bar.txt in the 'bar' variable
    stat:
      path: /tmp/bar.txt
    register: bar

  - debug:
      var: item
    with_items:
    - foo
    - bar
...

 

Which should output something like this. With foo, islink is false.

ok: [server1.example.com] => {
    "foo": {
        "changed": false,
        "failed": false,
        "stat": {
            "atime": 1608987441.9865022,
            "attr_flags": "",
            "attributes": [],
            "block_size": 4096,
            "blocks": 8,
            "charset": "us-ascii",
            "checksum": "8efc1730910ea99be3c377aea9eb8ad20a2bdf0c",
            "ctime": 1608987441.9865022,
            "dev": 64768,
            "device_type": 0,
            "executable": false,
            "exists": true,
            "gid": 1000,
            "gr_name": "john.doe",
            "inode": 51009750,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mimetype": "text/plain",
            "mode": "0664",
            "mtime": 1608987441.9865022,
            "nlink": 1,
            "path": "/tmp/foo.txt",
            "pw_name": "john.doe",
            "readable": true,
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 82,
            "uid": 1000,
            "version": "18446744072593596519",
            "wgrp": true,
            "woth": false,
            "writeable": true,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        }
    }
}

 

With bar.txt, "islnk" is "true", lnk_source and lnk_target are /tmp/foo.txt, and path is /tmp/bar.txt.

TASK [output the contents of the 'out' variable] *******************************
ok: [server1.example.com] => {
    "msg": {
        "changed": false, 
        "failed": false, 
        "stat": {
            "atime": 1602836449.8337147, 
            "attr_flags": "", 
            "attributes": [], 
            "block_size": 4096, 
            "blocks": 0, 
            "charset": "binary", 
            "ctime": 1589524743.897752, 
            "dev": 64768, 
            "device_type": 0, 
            "executable": true, 
            "exists": true, 
            "gid": 0, 
            "gr_name": "root", 
            "inode": 4206809, 
            "isblk": false, 
            "ischr": false, 
            "isdir": false, 
            "isfifo": false, 
            "isgid": false, 
            "islnk": true, 
            "isreg": false, 
            "issock": false, 
            "isuid": false, 
            "lnk_source": "/tmp/foo.txt", 
            "lnk_target": "/tmp/foo.txt", 
            "mimetype": "inode/symlink", 
            "mode": "0777", 
            "mtime": 1589524743.897752, 
            "nlink": 1, 
            "path": "/tmp/bar.txt", 
            "pw_name": "root", 
            "readable": true, 
            "rgrp": true, 
            "roth": true, 
            "rusr": true, 
            "size": 27, 
            "uid": 0, 
            "version": null, 
            "wgrp": true, 
            "woth": true, 
            "writeable": false, 
            "wusr": true, 
            "xgrp": true, 
            "xoth": true, 
            "xusr": true
        }
    }
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter adc853 in the box below so that we can be sure you are a human.