saltstack之系统初始化状态编写

目录结构

[root@master base]# pwd
/srv/salt/base

[root@master init]# tree .
.
├── basepkg
│   └── main.sls
├── chrony
│   ├── files
│   │   └── chrony.conf
│   └── main.sls
├── firewalld
│   └── main.sls
├── history
│   └── main.sls
├── kernel
│   ├── files
│   │   ├── limits.conf
│   │   └── sysctl.conf
│   └── main.sls
├── salt-minion
│   ├── files
│   │   └── minion.j2
│   └── main.sls
├── selinux
│   ├── files
│   │   └── config
│   └── main.sls
├── server
│   └── main.sls
├── sshd
│   ├── files
│   │   └── sshd_config
│   └── main.sls
├── timeout
│   └── main.sls
├── yum
│   ├── files
│   │   ├── centos-7.repo
│   │   ├── centos-8.repo
│   │   ├── epel-7.repo
│   │   ├── epel-8.repo
│   │   ├── salt-7.repo
│   │   └── salt-8.repo
│   └── main.sls
└── zabbix
    ├── files
    │   ├── install.sh
    │   ├── zabbix-5.4.4.tar.gz
    │   └── zabbix_agentd.conf.j2
    └── main.sls

关闭selinux

[root@node1 selinux]# pwd
/etc/selinux
//将修改好的selinux的配置文件传到对端
[root@master selinux]# cat main.sls 
/etc/selinux/config:
  file.managed:
    - source: salt://init/selinux/files/config
    - user: root
    - group: root
    - mode: 644

'setenforce 0':
  cmd.run

[root@node1 selinux]# cat config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing  //此处为开启状态
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted


[root@master selinux]# salt node1 state.sls init.selinux.main
node1:
----------
          ID: /etc/selinux/config
    Function: file.managed
      Result: True
     Comment: File /etc/selinux/config updated
     Started: 18:49:26.626348
    Duration: 53.341 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -4,7 +4,7 @@
                   #     enforcing - SELinux security policy is enforced.
                   #     permissive - SELinux prints warnings instead of enforcing.
                   #     disabled - No SELinux policy is loaded.
                  -SELINUX=enforcing
                  +SELINUX=disabled
                   # SELINUXTYPE= can take one of these three values:
                   #     targeted - Targeted processes are protected,
                   #     minimum - Modification of targeted policy. Only selected processes are protected. 

Summary for node1
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  53.341 ms


[root@node1 selinux]# cat config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled  //关闭状态
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

关闭防火墙

[root@node1 selinux]# systemctl enable --now firewalld.service

[root@node1 selinux]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendo>
   Active: active (running) since Fri 2021-11-12 19:11:20 CST; 16s ago
     Docs: man:firewalld(1)
 Main PID: 3384 (firewalld)
    Tasks: 2 (limit: 11301)

[root@master init]# pwd
/srv/salt/base/init
[root@master init]# ls
fire.sls  selinux
[root@master init]# cat fire.sls 
firewalld.service:
  service.dead:
    - enable: false


[root@master init]# salt node1 state.sls init.fire
node1:
----------
          ID: firewalld.service
    Function: service.dead
      Result: True
     Comment: Service firewalld.service has been disabled, and is dead
     Started: 19:12:25.215859
    Duration: 1044.361 ms
     Changes:   
              ----------
              firewalld.service:
                  True

Summary for node1
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   1.044 s


[root@node1 selinux]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vend>
   Active: inactive (dead)
     Docs: man:firewalld(1)

时间同步(chrony)

用阿里云的服务器来进行时间同步
[root@master chrony]# ls
files  main.sls
[root@master chrony]# cat main.sls 
chrony:
  pkg.installed

/etc/chrony.conf:
  file.managed:
    - source: salt://init/chrony/files/chrony.conf
    - user: root
    - group: root
    - mode: 644

chronyd.service:
  service.running:
    - enable: true

[root@master chrony]# ls files/
chrony.conf

[root@master chrony]# head -5 files/chrony.conf 
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
pool time1.aliyun.com iburst   //此处写阿里云的服务器的地址

# Record the rate at which the system clock gains/losses time.

[root@master chrony]# salt node1 state.sls  init.chrony.main

内核优化与文件描述

[root@master kernel]# pwd
/srv/salt/base/init/kernel
[root@master kernel]# ls
files


[root@master kernel]# cp /etc/security/limits.conf files/
[root@master kernel]# cp /etc/sysctl.conf files/

[root@master files]# tail -4 limits.conf   //在最后添加两行内容
#@student        -       maxlogins       4
*               soft     nofile           65535  //这个是软限制,最大的65535个文件打开数
*               hard     nofile          65535  //这个是硬限制,这个也是65535个文件数
# End of file

[root@master files]# tail -1 sysctl.conf   //在最后一行添加此内容,也可以添加一些其他的内核参数。
net.ipv4.ip_forward = 1


[root@master kernel]# cat main.sls 
/etc/security/limits.conf:
  file.managed:
    - source: salt://init/kernel/files/limits.conf
    - user: root
    - group: root
    - mode: 644

/etc/sysctl.conf:
  file.managed:
    - source: salt://init/kernel/files/sysctl.conf
    - user: root
    - group: root
    - mode: 644
  cmd.run:
    - name: sysctl -p  //这条命令在反复执行的情况下也不会报错或警告,所以不用考虑幂等性的问题。

ssh服务优化

[root@master sshd]# ls
files  main.sls
[root@master sshd]# ls files/
sshd_config

[root@master files]# head -17 sshd_config 
#   $OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port 1024  //将远程连接的默认的22端口号改为1024,这样为了安全防止别人攻击,这里最大可改的端口号为65535

[root@master sshd]# cat main.sls 
/etc/ssh/sshd_config:
  file.managed:
    - source: salt://init/sshd/files/sshd_config

start-sshd:
  service.running:
    - name: sshd
    - reload: true
    - watch:  //这个参数的意思是如果下面的文件的内容发生改变就重新加载/重新启动sshd服务
      - file: /etc/ssh/sshd_config

精简开机系统服务

//就是开机启动时可以将不必要的服务停止,可以只留一个ssh服务,写法如下:

[root@master server]# cat main.sls 
stop-service:
  service.dead:
    - name:   //这个地方就写你想要关闭的服务的名称就可以了。
    - enable: false

历史记录优化、设置终端超时时间

[root@master history]# pwd
/srv/salt/base/init/history

[root@master history]# cat main.sls 
/etc/profile:
  file.append:
    - text: 
      - 'export HISTTIMEFORMAT="%F %T `whoami` "'
[root@master history]# salt node1 state.sls init.history.main

[root@master timeout]# pwd
/srv/salt/base/init/timeout

[root@master timeout]# cat main.sls 
/etc/profile:
  file.append:
    - text: export TMOUT=300
[root@master init]# salt node1 state.sls init.timeout.main

yum源

salt源的下载地址

// 到阿里云镜像站下载centos7和8,epel7和8的网络源。
然后删除网络源中的不必要的内容。

[root@master files]# pwd
/srv/salt/base/init/yum/files
[root@master files]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' *.repo  //这条命令在只有centos7和8的情况下执行,确保你的files目录下只有7和8的源

[root@master yum.repos.d]# pwd
/etc/yum.repos.d
[root@master yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm  //执行此命令后会有很多epel源,将epel.repo这个源移到files目录下,并改名为epel-8.repo
下面两条命令只针对epel8
[root@master files]# sed -i 's|^metalink|#metalink|' epel-8.repo  //使用此命令注释epel源中的metalink

[root@master files]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' epel-8.repo  //取消注释

[root@master yum]# cat main.sls 
{% if grains['os'] == 'RedHat' %}
/etc/yum.repos.d/centos-{{ grains['osmajorrelease'] }}.repo:
  file.managed:
    - source: salt://init/yum/files/centos-{{ grains['osmajorrelease'] }}.repo
    - user: root
    - group: root
    - mode: 644
{% endif %}

/etc/yum.repos.d/epel-{{ grains['osmajorrelease'] }}.repo:
  file.managed:
    - source: salt://init/yum/files/epel-{{ grains['osmajorrelease'] }}.repo
    - user: root
    - group: root
    - mode: 644


/etc/yum.repos.d/salt-{{ grains['osmajorrelease'] }}.repo:
  file.managed:
    - source: salt://init/yum/files/salt-{{ grains['osmajorrelease'] }}.repo
    - user: root
    - group: root
    - mode: 644


[root@master yum]# salt node1 state.sls init.yum.main

安装zabbix_agentd

[root@master zabbix]# pwd
/srv/salt/base/init/zabbix

[root@master zabbix]# ls
files  main.sls

[root@master files]# mv zabbix_agentd.conf{,.j2}  //这里将zabbix_agentd的配置文件改为j2结尾,用于模板使用

[root@master files]# ls
install.sh  zabbix-5.4.4.tar.gz  zabbix_agentd.conf.j2

[root@master files]# cat install.sh 
#!/bin/bash

cd /usr/local/zabbix-5.4.4
./configure --enable-agent
make install


[root@master zabbix]# cat main.sls 
one:
  pkg.installed:
    - pkgs:
      - make
      - gcc
      - gcc-c++
      - openssl
      - openssl-devel
      - pcre
      - pcre-devel

two:
  archive.extracted:
    - name: /usr/local
    - source: salt://init/zabbix/files/zabbix-5.4.4.tar.gz
    - user: root
    - group: root
    - mode: 755

three:
  user.present: 
    - shell: /sbin/nologin
    - createhome: false
    - system: true

salt://init/zabbix/files/install.sh:
  cmd.script:
    - unless: test -f /usr/local/etc/zabbix_agentd.conf  //判断如果此路径下没有文件件就执行此脚本这样就具有幂等性
   
zabbix_file:
  file.managed:
    - name: /usr/local/etc/zabbix_agentd.conf
    - source: salt://init/zabbix/files/zabbix_agentd.conf.j2
    - template: jinja

[root@master base]# pwd
/srv/pillar/base

[root@master base]# cat salt-minion.sls   //这个是变量文件
master_ip: 192.168.182.141
[root@master base]# cat top.sls  //这个文件就是告诉对端到那个地方去找这个变量文件
base:
  'node1':
    - salt-minion


[root@master base]# vim /etc/salt/master  //在master文件里面添加这三行是为了定义这个/srv/pillar/base文件
865pillar_roots:
866  base:
867   - /srv/pillar/base
[root@master base]# systemctl restart salt-master.service  //修改配置文件需要重启服务

安装salt-minion

[root@master base]# pwd
/srv/pillar/base
[root@master base]# cat salt-minion.sls 
master_ip: 192.168.182.141

[root@master base]# pwd
/srv/pillar/base
[root@master base]# cat salt-minion.sls 
master_ip: 192.168.182.141

[root@master base]# cat top.sls 
base:
  'node1':
    - salt-minion

[root@master salt-minion]# pwd
/srv/salt/base/init/salt-minion

[root@master files]# mv minion{,.j2}将此配置文件改为j2结尾的模板
[root@master salt-minion]# ls files/
minion.j2


[root@master base]# vim /etc/salt/master  //添加这三行
865 pillar_roots:
 866   base:
 867     - /srv/pillar/base
[root@master base]# systemctl restart salt-master.service  //重启服务

[root@master salt-minion]# cat main.sls 
include:
  - init.yum.main

salt-minion:
  pkg.installed

/etc/salt/minion:
  file.managed:
    - source: salt://init/salt-minion/files/minion.j2
    - user: root
    - group: root
    - mode: 644
    - template: jinja

salt-minion.service:
  service.running:
    - enable: true

[root@master files]# vim minion.j2
17 master: {{ pillar['master_ip'] }}
[root@master salt-minion]# salt node1 state.sls init.salt-minion.main

基础命令

[root@master basepkg]# pwd
/srv/salt/base/init/basepkg
[root@master basepkg]# ls
main.sls

[root@master basepkg]# cat main.sls 
include:
  - init.yum.main
install-base-pkgages:
  pkg.installed:
    - pkgs:
      - screen
      - tree
      - psmisc
      - openssl
      - openssl-devel
      - telnet
      - iftop
      - iotop
      - sysstat
      - wget
      - dos2unix
      - lsof
      - net-tools
      - vim-enhanced
      - zip
      - unzip
      - bzip2
      - bind-utils
      - gcc
      - gcc-c++
      - glibc
      - make
      - autoconf

[root@master basepkg]# salt node1 state.sls init.basepkg.main
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,937评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,503评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,712评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,668评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,677评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,601评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,975评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,637评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,881评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,621评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,710评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,387评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,971评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,947评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,189评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,805评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,449评论 2 342

推荐阅读更多精彩内容