Yes, your example is how you would do it -- set up the number of layers you want, press record and add a layer mask to each layer one at a time, then stop. If you run the action on a file with fewer than the 5 layers in your example, a message will appear and you can either click Continue or Stop.
There is a a post, a couple of years old now, that presents a script for doing what you want:
Script to add layer mask to all layers but background?
So you might give that a whirl.
Thank you again for the response, Barbara. With your help, I figured out how to do it!!!
I used one of the scripts in the post to which you linked, and it works fine.
Now, I wanted to first Align all the layers (it is kind of like an HDR image where there are several frames at different exposures), so I created an action, used the Select All Layers, then Edit -> Auto Align Layers
Next thing was to add a step in the action which selects the TOP layer (this was important for some reason - without it, the masking script didn't work). So - while still recording - press your alt and your . keys at the same time (I guess that is option + . on a Mac)
Then final step was to click the flyout menu of the action pallet, select the Insert Menu Item, and then navigate to the script that will mask all the layers.
That middle part where you have to select the top layer needs to be run otherwise it won't mask the top layer.
Thanks again. I am a happy camper!!!