Ansible stat.islnk in playbook fails with error: dict object has no attribute islnk

Written by - 0 comments

Published on - Listed in Ansible Linux


When running a self-written Ansible playbook, I came across the following error:

TASK [DNS 18.04 - Delete symlink /etc/resolv.conf] **************************************************************************************
fatal: [target]: FAILED! => {"msg": "The conditional check 'resolvconf.stat.islnk == true' failed. The error was: error while evaluating conditional (resolvconf.stat.islnk == true): 'dict object' has no attribute 'islnk'\n\nThe error appears to be in '/srv/ansible/playbooks/allgemein/ubuntu1804-dns.yaml': line 13, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: DNS 18.04 - Delete symlink /etc/resolv.conf\n  ^ here\n"}

The relevant part of the playbook first uses stat to find out whether or not /etc/resolv.conf exists. In the task after, /etc/resolv.conf is deleted if this is a symbolic link:

 - name: DNS 18.04 - Check if /etc/resolv.conf exists
  stat: path=/etc/resolv.conf
  ignore_errors: true
  register: resolvconf

- name: DNS 18.04 - Delete symlink /etc/resolv.conf
  file: path=/etc/resolv.conf state=absent
  when: resolvconf.stat.islnk == true

  register: resolvconfdelete

This has worked well in the past, but since Ansible was upgraded (currently at 2.9.x) this doesn't seem to work anymore. Issue #24296 in Ansible's GitHub repository mentions this behaviour, too.

In this particular situation, the file actually does not exist on the target server:

root@target:~# stat /etc/resolv.conf
stat: cannot stat '/etc/resolv.conf': No such file or directory

Although this has (somehow) worked in a past Ansible version, Ansible now falls on its nose trying to see whether this file is a symbolic link (resolvconf.stat.islnk) because the file actually does not exist.

To fix this, make sure that the islnk check is only executed if the file exists:

 - name: DNS 18.04 - Delete symlink /etc/resolv.conf
  file: path=/etc/resolv.conf state=absent
  when: resolvconf.stat.exists and resolvconf.stat.islnk == true
  register: resolvconfdelete

The playbook now skipped this task:

TASK [DNS 18.04 - Delete symlink /etc/resolv.conf] **************************************************************************************
skipping: [target]


Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.