Srimad-Bhagavatam: Canto 9 - Chapter 1 - Verse 6
शुक्रो बृहस्पतेर्द्वेषादग्रहीत् सासुरोडुपम् ।हरो गुरुसुतं स्नेहात् सर्वभूतगणावृत: ॥ ६ ॥
śukro bṛhaspater dveṣādagrahīt sāsuroḍupamharo guru-sutaṁ snehātsarva-bhūta-gaṇāvṛtaḥ
Because of enmity between Bṛhaspati and Śukra, Śukra took the side of the moon-god and was joined by the demons. But Lord Śiva, because of affection for the son of his spiritual master, joined the side of Bṛhaspati and was accompanied by all the ghosts and hobgoblins.
The moon-god is one of the demigods, but to fight against the other demigods he took the assistance of the demons. Śukra, being an enemy of Bṛhaspati, also joined the moon-god to retaliate in wrath against Bṛhaspati. To counteract this situation, Lord Śiva, who was affectionate toward Bṛhaspati, joined Bṛhaspati. The father of Bṛhaspati was Aṅgirā, from whom Lord Śiva had received knowledge. Therefore Lord Śiva had some affection for Bṛhaspati and joined his side in this fight. Śrīdhara Svāmī remarks, aṅgirasaḥ sakāśāt prāpta-vidyo hara iti prasiddhaḥ: “Lord Śiva is well known to have received knowledge from Aṅgirā.”