Setting up configs for .ini file through cli

Hi, I’m a newbie to all this so please bear with me. I’m trying to setup alerts for Grafana and I’ve managed to changed smtp configs in grafana.ini and have working alerts but is there a way to have the same thing done via the CLI? I’m trying to automate this step by having a script that would change variables in .ini file instead of manually opening up the .ini file and changing things.
I would really appreciate the help on this.

Are you familiar with sed (as a rough and ready but probably workable
solution) or a configuration management tool such as puppet or ansible (which
will definitely do what you want, but has more of a learning curve)?


I am familiar with sed and I tried it but I’m having trouble with \n in the .ini file. For example, if I want to change enabled = false to true under smtp I’ve tried using sed -i 's/[smtp]\nenabled = false/[smtp]\nenabled = true/' /etc/grafana/grafana.ini but this doesnt make any changes to it.

sed is inherently a line editor - in other words, you can only match and
modify things which exist on a single line. You can substitute multiple lines
into the output for one line in the input, but you cannot match multiple lines
of input.

awk is an alternative which can match multiple lines, or there are workarounds
such as choosing a character which you know cannot possibly exist in your
input file (I like ¬ for example), and using tr to convert all \n to ¬, use sed
to perform the substitutions you need, and then tr to convert ¬ back into \n

If you find yourself having to do this sort of thing, though, it’ll probably
work until you realise some files don’t have the “enabled” line directly under
the “[smtp]” heading, but a bit further down, and then you wish you were using
a configuration management tool such as puppet or ansible to start with :slight_smile:


Maybe using environment variables would be a more straightforward solution?

would you know how I can use environment variables specifically for this? I’ve read the documentation for it e.g I tried to set [smtp] enabled=false to true using GF_SMTP_ENABLED=true and it does not work

When you look at the Grafana log, during start-up it will list the config options that it’s picked up from the environment. Do you see this option listed there?

If not, you need to ensure the variable is actually present in Grafana’s environment. Which brings me to the question, how are you running Grafana? If it’s as a systemd service for example, then you need to ensure the variable is set for that service specifically. If you’re running the executable from the command line, make sure you use export when setting the variable, etc.

I guess it does go back to a more general question though…in your first post talk about automating the configuration. But what’s the actual deployment environment that you’re automating? If you need automated deployment across several Linux hosts then as @pooh says you’re probably better off with ansible or puppet or something like that. If you’re deploying in a containerized environment, then environment variables are the way to go.