Vim Ultisnips is a very cool snippet plugin for VIM. Yes I’m a VIM user. Still not an expert, but getting better every day.
Cumulus Linux, as a programmatic way of entering repeatable config using Mako.
But I though, nah, I don’t want do to that. I wanted to use the Cumulus Ansible modules and wrote variable config in the host_vars/[hostname]
location
So I wanted to configure 100 bridges, and I wanted to do it on a switch to switch basis. So each switch gets a different set of 100 vlans. (its for a test for a real network)
cl_bonds:
bond0:
slaves: ['swp17-18']
ipv4: '10.1.1.0/31'
cl_interfaces:
swp52:
ipv4: ['10.1.3.1/24']
cl_bridges:
br0:
ports: ['swp49-50', 'swp51']
br1:
ports: ["swp49-50.1", "swp51.1"]
...
....
......
br99:
ports:["swp49-50.99, "swp51.99"]
So I managed to get the Ultisnip snippet for this section of the code.
br1:
ports: ["swp49-50.1", "swp51.1"]
...
....
......
br99:
ports:["swp49-50.99, "swp51.99"]
The Ultisnip snippet below creates 100 entries of “br” entry config, iterated from 1 to 99.
snippet br
`!p
for i in range(1,100):
snip.rv += 'br%(foo)s:\n' % {'foo': i}
snip.rv += ' ports: ["swp49-50.%(foo)s", "swp51.%(foo)s"]\n' % {'foo': i}
`
endsnippet
Just typed br[tab]
, at the beginning of the line under the br0
port config.
Then from the end of the document
Ctrl-Shift-V
- Visual Line. Start the highlight line at the bottom of the document/br1[enter]
- Search to line withbr11
and Vim selection highlighting will move from the bottom of the page to thatbr1
line1>x
to shift the selection one “tab” over so its inline with the rest of the YAML config
I then modified the snippet as I changed each switch configuration YAML file. I’m sure ultisnips has a better way of handling this. I’ll get on their IRC channel or mailer, or whatever they have to figure out a better way and hopefully update this blog post. So many keystrokes to remember. But with VIM, the more you practise, the more it becomes natural.