Best practice for "Watch key changes" in etcd v3

1.1k views Asked by At

we want to use etcd with the "watch" function to change our "/etc/hosts" file. A node should change the value and the watch should then write the change in "/etc/hosts".

Node1 Input:

etcdctl put master-server "192.168.10.1 master-server.int.server.io"

Node2 Watch:

etcdctl watch master-server | head -3 | tail -1 | tee  >> hosts.file

Node2 Output:

192.168.10.1 master-server.int.server.io

This works fine but now i want a output with multiple lines like this: Node 2 Output:

192.168.10.1 master-server.int.server.io
192.168.10.2 slave1-server.int.server.io
192.168.10.3 slave2-server.int.server.io

My thought was:

etcdctl put master-server "192.168.10.1 master-server.int.server.io \n192.168.10.2 slave1-server.int.server.io \n192.168.10.3 slave2-server.int.server.io"

But it doesnt work.

The Output is:

192.168.10.1 master-server.int.server.io \n192.168.10.2 slave1-server.int.server.io \n192.168.10.3 slave2-server.int.server.io

Does anyone know the best way to handle my problem? And how do I get multiple lines into a value?

Thanks for your Help.

1

There are 1 answers

1
Ephemeral On

Does the following do what you want?

export ETCDCTL_API=3
echo -e "hello\nworld" | etcdctl put mykey

and to read it back:

etcdctl --write-out=json get mykey | jq .kvs[0].value | sed 's/"//g'  | base64 -d