With a bit of searching I discovered the Secure Settings plugin which can be installed to /system/app and thereby provide Tasker with the functionality. However, it seems that Secure Settings does not work on the Galaxy S2 because Samsung have their own pattern lock and method of controlling it.
I started doing a bit of digging (mainly using logcat to see what the system was doing when the pattern lock was enabled/disabled via the Location and security settings menu). I discovered that the Samsung lock system is controlled by some files in /efs and with a bit of experimentation I was able to enable/disable the pattern lock via a root shell. I contacted the author of Secure Settings who suggested I use the Locale Execute plugin to run shell commands via Tasker.
So I present here the commands I am now using on my phone and hopefully they will be of some use to others. The commands include a check that should ensure that it does not attempt to enable the pattern lock when no pattern has been set, but this has not been fully tested!
WARNING: These commands require root access and you use them at your own risk!
Make sure to install the Locale Execute plugin first and then to enable the pattern lock with a task simply add a new "Plugin/Execute" action and set the contents to:
@! if [ "`cat /efs/cryptprop_lockscreen.patterneverchosen`" = "true" ]; then echo -n true > /efs/cryptprop_lock_pattern_autolock; fi
@! if [ "`cat /efs/cryptprop_lockscreen.patterneverchosen`" = "true" ]; then echo -n false > /efs/cryptprop_lock_pattern_autolock; fi