I'm having trouble with a conditional in Ansible. What I'm specifically trying to do is "stat" for the existence of a directory before using the "file" module to create it.
I realize that this process is idempotent, but I have a specific need to check on the existence of the directory first. Reason being, I need the directory to be created and owned by root, before fstab auto-mounts on top of that directory with a different UID/GID.
So, I'm using the code below that I cobbled together from some Googled examples, and another playbook that I run for checking if a file exists before taking another action:
---
- name: Create a directory if it does not exist.
hosts: server1
connection: ssh
tasks:
- block:
- name: Check if directory exists.
ansible.builtin.stat:
path: '/my_directory'
register: directory_status
- debug: var=directory_status.stat.path
- name: Create the directory if it does not exist.
ansible.builtin.file:
path: /my_directory
state: directory
owner: root
group: root
mode: '0775'
when:
- not directory_status.stat.exists
Unfortunately, I keep getting the following error:
TASK [Check if directory exists.] ******************************************************************************************
task path: /etc/ansible/playbooks/directory.yml:9
fatal: [server1]: FAILED! => {"msg": "The conditional check 'not directory_status.stat.exists' failed. The error was: error while evaluating conditional (not directory_status.stat.exists): 'directory_status' is undefined. 'directory_status' is undefined\n\nThe error appears to be in '/etc/ansible/playbooks/directory.yml': line 9, column 11, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - block:\n - name: Check if directory exists.\n ^ here\n"}
This is driving me crazy b/c I am following HOWTO's to the letter, and comparing to my other working playbook, and I can't figure out what is going wrong.
I greatly appreciate any help in advance.
THANKS!
Followed this example online: https://italchemy.wordpress.com/2021/10/01/ansible-check-if-a-directory-exists-first-and-then-create-a-new-directory-if-it-does-not-exist/
-ALSO-
Here is some sample code I have that I was using to compare against (I have removed some identifiable pieces to protect my organization):
- name: Register mdatp_onboard.json
stat:
path: /etc/opt/microsoft/mdatp/mdatp_onboard.json
register: mdatp_onboard
- name: Extract WindowsDefenderATPOnboardingPackage.zip into /etc/opt/microsoft/mdatp
unarchive:
src: /etc/ansible/files/mdatp/WindowsDefenderATPOnboardingPackage.zip
dest: /etc/opt/microsoft/mdatp
mode: 0600
owner: root
group: root
when:
- "not mdatp_onboard.stat.exists"
- name: Register mdatp_managed.json
stat:
path: /etc/opt/microsoft/mdatp/managed/mdatp_managed.json
register: mdatp_managed
- name: Create Microsoft mdatp_managed.json file from template.
template:
src: /etc/ansible/templates/mdatp_managed.json.j2
dest: /etc/opt/microsoft/mdatp/managed/mdatp_managed.json
mode: 0600
when:
- not mdatp_managed.stat.exists
#- ansible_distribution_major_version == "8"
- ansible_os_family == "RedHat"
You have set the
whencondition on the block, but you're setting the variable inside the block -- so it has not been set at the time you're trying to use it. That is, your logic currently looks like the following pseudocode:It sounds like you want the following instead; note that the indentation of the
whencondition has changed so that it is now attached to theansible.builtin.filetask instead of theblock:I think things become clearer if you move the
whencondition to the start of the task: