FreeKB - Ansible file module (create a symbolic link symlink soft link)
Ansible - file module (create a symbolic link symlink soft link)

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 on a managed node (e.g. target system). 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.

- 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 does not exist, the following will 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" }

 

As the error suggests, you can use force: yes to create the symbolic link.

- 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.

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

 

The stat module can be used to store the states of the files in a variable.

- 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

 

The debug module can be used to print the output.

- name: print the 'foo' variable
  debug:
    var: foo

- name: print the 'bar' variable
  debug:
    var: 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
        }
    }
}

 

Optional Parameters

The file module accepts the following optional parameters.

  • owner (e.g. owner: "root")
  • group (e.g. group: "wheel")
  • mode (e.g. mode: "2775")
  • seuser - SELinux user (e.g. seuser: "unconfined_u")
  • serole - SELinux role (e.g. serole: "object_r")
  • setype - SELinux type (e.g. setype: "httpd_sys_content_t")
  • selevel - SELinux level (e.g. selevel: "s0")

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |