Question

I have a Fabric script I'm in the process of creating, and I want it to change the following line in a config file:

Hostname=localhost

I want it to include the actual host name, for example:

Hostname=mypc

I tried to do so with the following line:

fabric.contrib.files.sed(
        '/etc/zabbix/zabbix_agentd.conf',
        before='Hostname=localhost',
        after='Hostname='"$HOSTNAME",
        use_sudo=True, backup='')

But unfortunately, it won't work, and I've tried various combinations of quotes and double quotes. It will literally put "Hostname=$HOSTNAME" into the config file.

Était-ce utile?

La solution

It won't work like that. The sed stuff is all inside single quotes. Just bind a name to the hostname value, and use Python interpolation to send it to the sed function. You might even be able to use the env.host setting. But an example of using this would look like this:

hostname = run("hostname -f")
fabric.contrib.files.sed(
        '/etc/zabbix/zabbix_agentd.conf',
        before='Hostname=localhost',
        after='Hostname=%s' % hostname,
        use_sudo=True, backup='')
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top