Ansible - when directory is empty or not empty

Let's say you have tasks in a playbook that should only be executed when the /tmp/foo directory is not empty.

The stat module and the register parameter can be used to determine if the directory exists.

---
- hosts: all
  tasks:
    - name: store the statistics of /tmp/foo in the 'out' variable
      stat:
        path: /tmp/foo
      register: out

 

The debug module can be used to displays the statistics of the out variable.

- debug: 
    var: out

 

Which should return something like this. Notice in this example the "exists" is false.

TASK [debug] 
ok: [server1.example.com] => {
    "msg": {
        "changed": false, 
        "failed": false, 
        "stat": {
            "exists": false
        }
    }
}

 

The find module and the register parameter can be used to store the statistics of the /tmp/foo directory in a variable. However, this module will only be invoked when the /tmp/foo directory exists.

- name: store the statistics of /tmp/foo in the 'files' variable
  find:
    paths: /tmp/foo
    file_type: directory
  register: files
  when:
    - out.stat.exists == true

 

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

TASK [debug] 
ok: [server1.example.com] => {
    "msg": {
        "changed": false, 
        "examined": 0, 
        "failed": false, 
        "files": [], 
        "matched": 0, 
        "msg": "/tmp/foo was skipped as it does not seem to be a valid directory or it cannot be accessed\n"
    }
}

 

If the directory exists but is empty, something like this should be returned.

ok: [server1.example.com] => {
    "msg": {
        "changed": false,
        "examined": 0,
        "failed": false,
        "files": [],
        "matched": 0,
        "msg": ""
    }
}

 

Notice in this example that "examined" is 8, which means that the /tmp/foo directory contains 8 files or directories.

TASK [debug] 
ok: [server1.example.com] => {
    "msg": {
        "changed": false, 
        "examined": 8, 
        "failed": false, 
        "files": [], 
        "matched": 0, 
        "msg": ""
    }
}

 

The fail module can now be used to cease execution of the playbook when the /tmp directory is or is not empty.

- name: fail when /tmp is not empty
  fail: 
   msg: the /tmp directory is not empty
  when: out.examined != 0

 

Invoking this playbook should return something like this. The ansible-doc fail command can be used to show the Ansible documention on the fail module.

When fail evaluates to true, all of the subsequent tasks are skipped.

PLAY [all]

TASK [store the statistics of the /tmp directory in the 'tmp' variable]
ok: [server1.example.com]

TASK [fail when /tmp is not empty]
fatal: [server1.example.com]: FAILED => {"changed": false, "msg": "the /tmp directory is not empty"}

PLAY RECAP
server1.example.com : ok=1  changed=0  unreacable=0  failed=1

 


Multiple managed nodes

Let's consider the scenario where there are two (or more) managed nodes. Let's say the /tmp directory is not empty on server1.example.com and is empty on server2.example.com. In this scenario, invoking the playbook will return the following.

The important takeaway here is that when fail evaluated to true for server1.example.com, all of the subsequent tasks are skipped for only server1.example.com. All of the tasks were performed on server2.example.com. When fail evaluates to true, the fail is isolated to the managed node in question, and does not impact the other managed nodes.

PLAY [all]

TASK [store the statistics of the /tmp directory in the 'tmp' variable]
ok: [server1.example.com]
ok: [server2.example.com]

TASK [fail when /tmp is not empty]
fatal: [server1.example.com]: FAILED => {"changed": false, "msg": "the /tmp directory is not empty"}

TASK [fail when /tmp is not empty]
skipping: [server2.example.com]

PLAY RECAP
server1.example.com : ok=2  changed=0  unreacable=0  failed=1
server2.example.com : ok=3  changed=0  unreacable=0  failed=0

 



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