saltstack的jinja模

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了saltstack的jinja模相关的知识,希望对你有一定的参考价值。

[[email protected] ~]# cd /srv/salt/base/
[[email protected] base]# ll
总用量 12
-rw-r--r-- 1 root root 172 11月 14 21:26 apache.sls
-rw-r--r-- 1 root root 128 11月 16 00:12 dns.sls
drwxr-xr-x 2 root root  25 11月 16 00:23 files
-rw-r--r-- 1 root root  28 11月 16 00:22 top.sls
[[email protected] base]# vim dns.sls 
[[email protected] base]# cat dns.sls 
/etc/resolv.conf:
  file.managed:
     - source: salt://files/resolv.conf
     - user: root
     - group: root
     - mode: 777
     - template: jinja
     - defaults:    #定义变量
       DNS_SERVER: 192.168.43.118

[[email protected] base]# vim files/resolv.conf 
[[email protected] base]# cat files/resolv.conf
#jjjjjjjjjjjjjjjjjj
nameserver {{ DNS_SERVER }}   #2个大括号表示变量

[[email protected] base]# salt ‘*‘  state.highstate
192.168.43.118:
----------
          ID: /etc/resolv.conf
    Function: file.managed
      Result: True
     Comment: File /etc/resolv.conf updated
     Started: 00:43:27.222821
    Duration: 21.809 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,2 +1,3 @@
                   #jjjjjjjjjjjjjjjjjj
                  -nameserver 192.168.43.1
                  +nameserver 192.168.43.118
                  +

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0

[[email protected] base]# vim files/resolv.conf 
[[email protected] base]# cat files/resolv.conf
#jjjjjjjjjjjjjjjjjj
#  {{ grains[‘fqdn_ip4‘] }}
nameserver {{ DNS_SERVER }}
[[email protected] base]# salt ‘*‘  state.highstate
192.168.43.118:
----------
          ID: /etc/resolv.conf
    Function: file.managed
      Result: True
     Comment: File /etc/resolv.conf updated
     Started: 00:47:03.799795
    Duration: 32.514 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,3 +1,3 @@
                   #jjjjjjjjjjjjjjjjjj
                  +#  [‘192.168.43.118‘]
                   nameserver 192.168.43.118
                  -

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0

系统初始化配置:

[[email protected] base]# pwd
/srv/salt/base
[[email protected] base]# mkdir init
[[email protected] base]# mv apache.sls dns.sls files/ /tmp/
[[email protected] base]# tree
.
├── init
└── top.sls

1 directory, 1 file

[[email protected] base]# cp /tmp/dns.sls init/
[[email protected] base]# ll
总用量 4
drwxr-xr-x 2 root root 21 11月 16 00:55 init
-rw-r--r-- 1 root root 28 11月 16 00:22 top.sls
[[email protected] base]# cd init/
[[email protected] init]# ls
dns.sls

[[email protected] init]# vim dns.sls 
[[email protected] init]# cat dns.sls
/etc/resolv.conf:
  file.managed:
     - source: salt://init/files/resolv.conf
     - user: root
     - group: root
     - mode: 777

[[email protected] init]# mkdir files
[[email protected] init]# cp /etc/resolv.conf files/
[[email protected] init]# cp /etc/resolv.conf files/
[[email protected] init]# vim history.sls
[[email protected] init]# cat history.sls
/etc/profile:
  file.append:  #file模块的追加方法
    - text:
      - export HISTTIMEFORMAT="%F %T ‘whoami‘"

[[email protected] init]# export  PROMPT_COMMAND=‘ { msg=$(history 1 | { read x y; echo $y; });logger "[euid=$(whoami)]":$(who am i):[‘pwd‘]"$msg";} ‘
[[email protected] init]# who
root     pts/0        2018-11-16 00:29 (desktop-4a0ohej)
root     pts/1        2018-11-16 00:38 (desktop-4a0ohej)
[[email protected] init]# tail -f /var/log/messages
Nov 16 01:01:01 master systemd: Starting Session 34 of user root.
Nov 16 01:10:01 master systemd: Started Session 35 of user root.
Nov 16 01:10:01 master systemd: Starting Session 35 of user root.
Nov 16 01:14:00 master root: [euid=root]:root pts/0 2018-11-16 00:29 (desktop-4a0ohej):[pwd]export PROMPT_COMMAND=‘ { msg=$(history 1 | { read x y; echo $y; });logger "[euid=$(whoami)]":$(who am i):[‘pwd‘]"$msg";} ‘
Nov 16 01:14:04 master root: [euid=root]:root pts/0 2018-11-16 00:29 (desktop-4a0ohej):[pwd]uptime
Nov 16 01:14:39 master root: [euid=root]:root pts/0 2018-11-16 00:29 (desktop-4a0ohej):[pwd]tail -f /var/log/messages
Nov 16 01:14:42 master root: [euid=root]:root pts/0 2018-11-16 00:29 (desktop-4a0ohej):[pwd]uptime
Nov 16 01:15:06 master root: [euid=root]:root pts/0 2018-11-16 00:29 (desktop-4a0ohej):[pwd]tail -f /var/log/messages
Nov 16 01:15:12 master root: [euid=root]:root pts/0 2018-11-16 00:29 (desktop-4a0ohej):[pwd]tail -f /var/log/messages
Nov 16 01:15:17 master root: [euid=root]:root pts/0 2018-11-16 00:29 (desktop-4a0ohej):[pwd]who

[[email protected] init]# ll
总用量 12
-rw-r--r-- 1 root root 172 11月 16 01:17 audit.sls
-rw-r--r-- 1 root root 137 11月 16 00:57 dns.sls
drwxr-xr-x 2 root root  25 11月 16 00:58 files
-rw-r--r-- 1 root root  88 11月 16 01:03 history.sls
[[email protected] init]# cat audit.sls 
/etc/bashrc:
  file.append:
    - text:
      - export  PROMPT_COMMAND=‘ { msg=$(history 1 | { read x y; echo $y; });logger "[euid=$(whoami)]":$(who am i):[‘pwd‘]"$msg";} ‘

[[email protected] init]# cat sysctl.sls
vm.swappinese:   #尽量不适用swap分区
  sysctl.present:
    - value: 0

net.ipv4.ip_local_port_range:
  sysctl.present:
    - value: 10000 65000

fs.file-max:    #最大打开文件数
  sysctl.present:
    - value: 100000

[[email protected] init]# vim env_init.sls
[[email protected] init]# cat env_init.sls
include:
  - init.dns
  - init.history
  - init.audit
  - init.sysctl

[[email protected] init]# cd ..
[[email protected] base]# ls
init  top.sls
[[email protected] base]# vim top.sls 
[[email protected] base]# cat top.sls
base:
  ‘*‘:
    - init.env_init  #在base路径下去init目录找env_init文件并执行

[[email protected] base]# salt ‘*‘ state.highstate test=True  测试不执行
[[email protected] base]# salt ‘*‘ state.highstate

以上是关于saltstack的jinja模的主要内容,如果未能解决你的问题,请参考以下文章

SaltStack的配置管理--jinja

SaltStack配置管理-jinja模板

SaltStack实战之配置管理-Jinja2模板

SaltStack 使用 Jinja2 模板

saltstack之jinja,解决生产发布问题

SaltStack-----安装,远程执行,grains,pillar,jinja模板