Ansible – using with_items and when conditional to

Question:

I have a bunch of servers that have four physical drives on them (/dev/sda, sdb, sdc, and sdd). sda has the OS installed on it.

I need to format each drive except sda. I need to check if each drive has data on it. If it does, then I shouldn’t format it.

There’s clearly more steps involved into formatting these drives but it fails at the last task when creating the GPT partition table.

Here’s what it looks like when it runs. You’ll see that it fails at the last task:

Any idea how I can check the conditional base_secondary_partition_{{item}}.stat.exists? I need to make sure that if there’s data on the drive, it will not format it.

Answer:

You do not need to register your result with the item salt. When you register the result of a loop (e.g. with_items) the registered value will contain a key results which holds a list of all results of the loop. (See docs)

Instead of looping over your original device list, you can loop over the registered results of the first task then:

The condition not item | skipped takes care of that elements which have been filtered in the original loop (sda) will not be processed.

While that might be a solution to your problem, your question is very interesting. There seems to be no eval feature in Jinja2. While you can concatenate strings you can not use that string as a variable name to get to its value…

Leave a Reply