Devas(Gods) are considered the positive , life sustaining forces of nature(like Sun,Moon,Vayu or Wind,Agni or Fire ,Varuna or Water etc) and it is usually the Dharmic behavior(lawful.legitimate as defined in Hindu Shastras) that is expected of them .
However, that is not always the case,as we have seen many a times,including in the Indra-Ahalya episode which you have mentioned in the question.
Being Devas, they are more prone to Dharmic behavior but deviations from that can not be entirely ruled out.
Simply put,Devas are never defined strictly as ones who only follows Dharma.
The Apastamba Sutras says,for example:
Nadharmaadharmou charatah aavaasyah iti na devaah, na gandharvaah, sa pitarah ityaachakshate ayam dharmo amayadharma iti,
Aapastamba states that those high practioners of Dharma have no
special considerations as being ‘mine’ or ‘thine’, be they Devas,
Gandharvas, Pitru Devas and so on
Because, they are fully within the bounds of Maya(illusion) where as enlightened saints are beyond that.
Just like the Veda versed Brahmins ,& those who know the difference between Dharma -Adharma can even commit misdeeds at times,so can the Devas.
The Parashara Smriti says:
Veda vedaanga vidushaam Dharma shastram vijaana - taam,Svakarfma rata
Vipraanaam svakam paapam nivedayet, Yatpaapam kartru vyatiriktena kena
chidapinajnaatam,Tadrahasyam tasya praayaschittamapirahasya kartavyam
Veda vedaanga vidas and experts in Dharma Shastras too are at some
points of time might perform misdeeds surreptitiously
How to explain the children of Rama, Krishna. while completely celibacy is demanded of hindu sannyasins?
Krishna and Rama were both Kshatriya kings.They were not Sannyasis,so maintaining celibacy is never a must for them(except of course during the time when they were students).
Maintaining celibacy is a must for only students(brahmacharis) and for those who have taken Sannyasa vows,i.e the Sannyasis.
and since Knowledge of Brahman is the goal, what is the role of gods in that?
Obtaining the knowledge of the Brahman ,which is pervaded by Maya(illusion), is the goal for all beings as per Hindu philosophies.It is said that only Brahaman exists whereas we perceive it differently or in many varied forms only because of that Maya.