Server Fault Asked by sam1975 on December 20, 2020
I have to create a pipeline in order to automate creation-deployment and update configuration file for network devices (switch-routers-firewall, mostly cisco).
There’s many ways i think to do the job but i hesitate.
The process would be to read a data file, search a string (example:hostname contains "NR") and if it’s OK, choose the good template in a library in order to create the configuration file.
I’ve tried with ansible but things are not clear for me.
The other way is jinja library and i’ve tried something like this
#! /usr/bin/python
import sys
from jinja2 import Template
template = """ hostname {{hostname}}
no ip domain lookup
ip domain name local.lab
ip name-server {{name_server_pri}}
ip name-server {{name_server_sec}}
ntp server {{ntp_server_pri}} prefer
ntp server {{ntp_server_sec}} """
data= {
"hostname": "core-test-01",
"name_server_pri": "1.1.1.1",
"name_server_sec": "8.8.8.8",
"ntp_server_pri": "0.pool.ntp.org",
"ntp_server_sec": "1.pool.ntp.org",
}
j2_template = Template(template)
print(j2_template.render(data))
In this case, how could i load a template file from a library (and in regarding of the string i want to find in my data file)?
Q: "Read a data file, search a string (example:hostname contains "NR") ... choose the good template in a library in order to create the configuration file."
For example, given the data files
> ssh admin@test_11 cat /tmp/hostname
hostname-NR
> ssh admin@test_12 cat /tmp/hostname
hostname-NS
> ssh admin@test_13 cat /tmp/hostname
hostname-NX
and the templates
> cat templates/nr.j2
# template nr.j2
> cat templates/ns.j2
# template ns.j2
> cat templates/nt.j2
# template nt.j2
> cat templates/default.j2
# template default.j2
the play below
- hosts: test_11,test_12,test_13
vars:
templates_lib:
- {contains: "{{ my_hostname is search('NR') }}", template: nr.j2}
- {contains: "{{ my_hostname is search('NS') }}", template: ns.j2}
- {contains: "{{ my_hostname is search('NT') }}", template: nt.j2}
tasks:
- command: cat /tmp/hostname
register: result
- template:
src: "{{ my_template) }}"
dest: /tmp/test.conf
vars:
my_hostname: "{{ result.stdout }}"
my_template: "{{ templates_lib|
selectattr('contains')|
map(attribute='template')|
first|default('default.j2') }}"
gives
> ssh admin@test_11 cat /tmp/test.conf
# template nr.j2
> ssh admin@test_12 cat /tmp/test.conf
# template ns.j2
> ssh admin@test_13 cat /tmp/test.conf
# template default.j2
Answered by Vladimir Botka on December 20, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP