python jinjia2模板使用

it2022-05-05  157

python jinjia2模板使用

https://gist.github.com/wrunk/1317933

#!/usr/bin/env python # -*- coding: utf-8 -*- from jinja2 import Environment, FileSystemLoader import os if __name__ == '__main__': j2_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'jinja2') kickstart_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'kickstart') pxe_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'pxe') j2_env = Environment(loader=FileSystemLoader(j2_path)) host = { 'root_password': '123456', 'os': 'centos-6', 'install_host': 'install.localhost', 'name': 'bjtn-g18-10-11', 'interface': [ {'dev': 'em1', 'mac': 'aa:aa:aa:aa:aa:01', 'ip': '10.0.10.11', 'mask': '255.255.255.0'}, {'dev': 'em2', 'mac': 'aa:aa:aa:aa:aa:02', 'ip': '200.0.10.11', 'mask': '255.255.255.192'} ], 'gateway': '10.0.10.1' } with open(os.path.join(kickstart_path, host['interface'][0]['mac']), 'w') as f: f.write(j2_env.get_template('ks-centos-6').render(host=host)) with open(os.path.join(pxe_path, host['interface'][0]['mac']), 'w') as f: f.write(j2_env.get_template('pxe-centos-6').render(host=host))

pxe-centos-6

LABEL {{ os }} MENU DEFAULT MENU LABEL {{ mac }} KERNEL {{ os }}/vmlinuz APPEND initrd={{ os }}/initrd.img ks=http://{{ install_host }}/kickstart/{{ mac }} ksdevice=link ramdisk_size=102400 console=tty0 console=ttyS1,115200

ks-centos-6

# kickstart lang en_US.UTF-8 keyboard us timezone Asia/Shanghai auth --enableshadow --passalgo=sha512 #python -c 'import crypt; print(crypt.crypt("MyPassword", "$6$MySalt"))' rootpw --iscrypted {{ host.root_password }} text install skipx url --url http://{{ host.install_host }}/{{ host.os }} bootloader --location=mbr zerombr clearpart --drives=sda --all #--initlabel part swap --fstype='swap' --ondisk=sda --size=8000 part / --fstype='ext4' --ondisk=sda --size=50000 part /opt --fstype='ext4' --ondisk=sda --grow --size=1 network --device={{ host.interface[0].mac }} --bootproto=dhcp --activate firewall --disabled selinux --disabled services --disabled=NetworkManager,ip6tables,iptables,postfix,cpuspeed services --enabled=network firstboot --disabled reboot %packages @Base wget %end %pre clearpart --drives=sda --all /usr/sbin/parted -s /dev/sda mklabel gpt %end %post cat > /etc/sysconfig/network << _EOF_ NETWORKING=yes HOSTNAME={{ host.name }} GATEWAY={{ host.gateway }} _EOF_ {% for nic in host.interface %} cat > /etc/sysconfig/network-scripts/ifcfg-{{ nic.dev }} << _EOF_ BOOTPROTO=static DEVICE={{ nic.dev }} IPADDR={{ nic.ip }} NETMASK={{ nic.mask }} ONBOOT=yes _EOF_ {% endfor %} %end posted on 2016-04-07 14:17 北京涛子 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/liujitao79/p/5363378.html


最新回复(0)