SPI device act as a Master and as a slave
Not with what you show there, no. SPI is composed of 4 unidirectional signals and has no protocol to deal with them being bidirectional.

Some suggestions for achieving your goal...

You add two control lines, an AtoB line and a BtoA line, between A and B to indicate who is the bus master. There are several road-tested schemes for handling this based upon whether one master always decides, if both can request mastership but one always has priority, time-based fairness and more. (Don't know your situation so will leave that there.)

You could switch to I2C which is bidirectional and can do what you need with appropriate software to meet your priority scheme. But it runs a lot slower than SPI, which may or may not be a problem.