条件判断
[root@master base]
test_nginx_install:
pkg.installed:
{% if grains['os'] == 'CentOS Stream' %}
- name: httpd
{% elif grains['os'] == 'Ubuntu' %}
- name: apache2
{% endif %}
[root@master base]
minion:
----------
ID: test_nginx_install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 08:42:48.498098
Duration: 6862.691 ms
Changes:
Summary for minion1
------------
Succeeded: 1
Failed: 0
------------
Total states run: 1
Total run time: 7.691 s
此处省略......
循环语句
[root@master base]
{% for user in ['tom','tom1'] %}
{{ user }}:
user.present
{% endfor %}
[root@master base]
minion:
----------
ID: tom
Function: user.present
Result: True
Comment: New user tom created
Started: 08:55:27.673216
Duration: 348.994 ms
Changes:
----------
fullname:
gid:
1000
groups:
- tom
home:
/home/tom
homephone:
name:
tom
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
1000
workphone:
----------
ID: tom1
Function: user.present
Result: True
Comment: New user tom1 created
Started: 08:55:28.022478
Duration: 357.295 ms
Changes:
----------
fullname:
gid:
1001
groups:
- tom1
home:
/home/tom1
homephone:
name:
tom1
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
1001
workphone:
Summary for minion
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 706.289 ms
minion端查看用户
[root@minion ~]
uid=1000(tom) gid=1000(tom) 组=1000(tom)
[root@minion ~]
uid=1001(tom1) gid=1001(tom1) 组=1001(tom1)
masterless配置
- 修改配置文件minion
- 注释master行
- 取消注释file_client并设其值为local
- 设置file_roots
- 设置pillar_roots
[root@minion ~]
......
......
file_client: remote
......
file_roots:
base:
- /srv/salt/base
使用 masterless 模式时是不需要启动任何服务的,包括salt-master和salt-minion。
[root@minion ~]
[root@minion ~]
Removed /etc/systemd/system/multi-user.target.wants/salt-minion.service.
masterless模式执行模块或状态时需要使用salt-call命令,而不再是salt或者salt-ssh。需要注意的是要使用salt-call的--local选项。
[root@minion ~]
local:
09:07:40 up 34 min, 2 users, load average: 0.25, 0.22, 0.24
[root@minion ~]
local:
Wed Jul 21 09:10:46 EDT 2021
[root@minion ~]
[root@minion ~]
[root@minion base]
test_wget:
pkg.installed:
- name: wget
[root@minion base]
local:
----------
ID: test_wget
Function: pkg.installed
Name: wget
Result: True
Comment: All specified packages are already installed
Started: 09:13:12.047074
Duration: 1122.569 ms
Changes:
Summary for local
------------
Succeeded: 1
Failed: 0
------------
Total states run: 1
Total run time: 1.123 s
[root@minion base]
/usr/bin/wget
salt-master高可用 涉及到高可用时,数据的同步是个永恒的话题,我们必须保证高可用的2个master间使用的数据是一致的,包括:
- /etc/salt/master配置文件
- /etc/salt/pki目录下的所有key
- /srv/下的salt和pillar目录下的所有文件
保障这些数据同步的方案有:
- nfs挂载
- rsync同步
- 使用gitlab进行版本控制
安全相关: 为保证数据的同步与防止丢失,可将状态文件通过gitlab进行版本控制管理。 环境说明:
主机 | ip |
---|
master | 192.168.149.132 | master1 | 192.168.149.137 | minion | 192.168.149.134 |
修改minion端的minion配置文件
[root@minion base]
......
master:
- 192.168.149.132
- 192.168.149.137
......
[root@master ~]
[root@master ~]
[root@master ~]
[root@master ~]
[root@master1 ~]
[root@minion ~]
....
master_type: failover
....
master_alive_interval: 3
[root@minion ~]
[root@master ~]
minion:
True
[root@master1 ~]
minion:
Minion did not return. [No response]
[root@master ~]
[root@master1 ~]
minion:
True
|