I need some help to adjust the volume of the amplifier. It is a 2.5 W amplifier and it has to be connected to a 2 W speaker. I want to reduce the gain of the amplifier in order to avoid damage to the speaker. How can I do it? Or can I just connect the amplifier to the speaker without anything between them?
Don't bother. 2.5 W is unlikely to hurt a 2 W speaker. It will just cause distortion. Somewhere in your audio chain there must surely be a volume control. Turn down the volume to the point where the sound from the speaker isn't distorted.
The 2.5 W rating of a power amp is what it can put out, given the right signal and load. Put in lower volume, and you get lower volume out, which also means less power out.
Your assumption that a 2.5 W amplifier can damage a 2 W speaker is false.
You can just use them together and it will work just fine.
The power rating of speakers is "weird" anyway as it depends on frequency and how the speaker is used (in a case, air-tight case or no case at all).
When the speaker is about to damaged you will hear it as you will be overdriving the speaker and it will distort. That's not pleasant to hear. Then just lower the volume.
Now that you've said what the product is you're using https://www.adafruit.com/product/2130 in a comment on Olin's answer, you can see from the product page it has a trim pot for adjusting the gain.
The page says
"There's even a volume trim pot so you can adjust the volume on the board down from the default 24dB gain." This seems like a direct answer to your question of how to reduce the gain of your amplifier.
Adjusting the gain is not so easy. You could put a voltage divider at the input but that works only if you know the maximum voltage coming in and the sensitivity of your amplifier.
Much simpler would be to add an extra resistor in series with your speaker with a value of ~1/10 of your speaker impedance. Thus an 8 ohm speaker would require a ~0.8 ohm series resistor. (Nearest E12 value is 0.82 Ohm)
The resistor should be rated for 0.5 watts and will get warm. (Or hot if the heat can't go away).
Corrected after re-calculation because of comment from Olin Lathrop.
In the audio production world, it is said that you need amplifiers with a power level above the nominal power level of a speaker. A beefier, similar quality amp will reproduce transients with better fidelity, at the cost of possibly accelerated wear to the speaker element itself.
On the other hand, when using an under-powered amp, while it will "preserve" the life of the speaker, the reproduced signal will be of lower quality.
Using an amplifier several times the power rating of the speakers it is driving is not an issue, as long as the impedance is properly matched: the amplifier should never drive a load that is lower (in ohms) than its own rating, while most amps will happily drive a higher resistance load (not necessarily without some audio distortion).
A limiter (an audio device) can limit the signal sent to a power amplifier in order to protect a speaker. Common processes involve either signal ducking (turning down the volume at predetermined rates) or clipping of the signal itself (soft or hard) thereby preventing the speakers from being overdriven, but such processes are not reliable and always involve audio distortion.