idempotent way to convert directory into a link?

43 views
Skip to first unread message

sunnz

unread,
Feb 16, 2017, 8:32:05 PM2/16/17
to Ansible Project
hi,

can ansible force a path to be a link even when it is a directory with files? currently i am deleting the directory before making it a link and it is not idempotent.

on the first run it deletes the directory. but it is also deleting and re-creating the link in every run after that.

for example of what i am trying to do:

    - name: remove existing link or directory
      file:
        path: /var/lib/mysql
        state: absent
    - name: link mysql to datastore
      file:
        src: /mnt/datastore/mysql
        dest: /var/lib/mysql
        state: link
        force: true

Kai Stian Olstad

unread,
Feb 17, 2017, 2:39:09 AM2/17/17
to ansible...@googlegroups.com
On 17.02.2017 01:51, sunnz wrote:
> can ansible force a path to be a link even when it is a directory with
> files? currently i am deleting the directory before making it a link
> and it
> is not idempotent.

You can't force it, you need to delete and create link as you do now.

>
> on the first run it deletes the directory. but it is also deleting and
> re-creating the link in every run after that.

Before you delete and create link use the stat module to check if the
path is a directory and then do a delete and create the link.

https://docs.ansible.com/ansible/stat_module.html

--
Kai Stian Olstad

sunnz

unread,
Mar 3, 2017, 12:24:53 AM3/3/17
to Ansible Project, ansible-pr...@olstad.com
thanks kai, i got the stat to work in this way:

---
# make /tmp/link-dir--test a link.
# delete it if it already exists.

- hosts: localhost
  become: false
  vars:
    path__test_dir_link: /tmp/link-dir--test
  tasks:
    - name: check if dest is a link
      stat:
        path: "{{ path__test_dir_link }}"
      register: stat__path__test_dir_link
    - name: remove dest
      file:
        path: "{{ path__test_dir_link }}"
        state: absent
      when:
        - stat__path__test_dir_link['stat']['islnk'] is defined
        - stat__path__test_dir_link['stat']['islnk'] == false
    - name: create link
      file:
        dest: "{{ path__test_dir_link }}"
        src: /usr
        state: link
Reply all
Reply to author
Forward
0 new messages