How to edit the run list of multiple nodes?

I have a list of about 40 nodes that I want to edit the run list on, replacing one role with another role. Using knife exec, I can delete the old role with

knife exec -E 'nodes.transform("name:mynode") {|n| puts; n.run_list.remove("role[oldrole]")}'

and I can add the new role with

knife exec -E 'nodes.transform("name:mynode") {|n| puts; n.run_list.add("role[newrole]")}'

But this puts the new role at the end of the run list, and I want it at the beginning of the run list, where the old role was. Is there a way with knife exec and nodes.transform that I can just replace the old role with the new one?

So admittedly this is old and might not be the current best way but thought you might like a reply.

The general idea would be to just rename the role vs removing and adding

It’s also an old post on here

From the looks of it the scripts on mobile look similar maybe identical.

Thanks. That's an interesting approach. It's not exactly what I was thinking of, but it looks like with some changes it could accomplish the same thing.