yum -y install salt-ssh
修改/etc/salt/roster的配置文件
roster配置文件的配置选项有:
# target的信息
host: # 远端主机的ip地址或者dns域名
user: # 登录的用户
passwd: # 用户密码,如果不使用此选项,则默认使用秘钥方式
# 可选的部分
port: #ssh端口
sudo: #可以通过sudo
tty: # 如果设置了sudo,设置这个参数为true
priv: # ssh秘钥的文件路径
timeout: # 当建立链接时等待响应时间的秒数
minion_opts: # minion的位置路径
thin_dir: # target系统的存储目录,默认是/tmp/salt-
cmd_umask: # 使用salt-call命令的umask值
我使用os.system修改roster文件:
views.py
def serverAdd(request):
result = ''
if request.method == "POST":
form = autoArrMinionForm(request.POST)
if form.is_valid():
ip = request.POST.get('add_ip') # 需要安装minion端的ip
username = request.POST.get('add_username') # 需要安装minion端的用户名
password = request.POST.get('add_password') # 需要安装minion端的密码
try:
os.system("echo '"+ip+":'>> /etc/salt/roster && \
echo ' host: " +ip+ "'>> /etc/salt/roster && \
echo ' user: " +username+ "'>> /etc/salt/roster && \
echo ' passwd: " +password+ "'>> /etc/salt/roster && \
echo ' sudo: True'>> /etc/salt/roster && \
echo ' tty: True'>> /etc/salt/roster && \
echo ' timeout: 10'>> /etc/salt/roster")
os.system("salt-ssh '" + ip + "' -ir 'easy_install certifi'") # 安装cretifi模块
(status_gethostname, resultgethostname) = subprocess.getstatusoutput("salt-ssh -ir '" + ip + "' 'hostname'") # 获取hostname
os.system("salt-ssh '" + ip + "' -ir 'echo ''"+ip+"' '"+resultgethostname+"''>> /etc/hosts'") # 添加hosts
(status, result) = subprocess.getstatusoutput("salt-ssh -i '"+ip+"' state.sls minions.install") # 执行安装命令,并返回结果
except:
result = "无法连接该主机,请检查ip和用户密码是否正确!"
else:
result = "请填写正确的ip和用户名密码"
else:
form = autoArrMinionForm()
re = {
"form": form,
"result": result
}
return render(request, "serveradd.html", re)
mkdir -p /srv/salt/minions
mkdir -p /srv/salt/minions/conf
mkdir -p /srv/salt/minions/yum.repos.d
vim install.sls
minion_yum:
file.recurse:
- name: /etc/yum.repos.d
- source: salt://minions/yum.repos.d ##提前准备的yum文件路径
- user: root
- group: root
- file_mode: 644
- dir_mode: 755
- include_empty: True
minion_install:
pkg.installed:
- pkgs:
- salt-minion
- require:
- file: minion_yum
- unless: rpm -qa | grep salt-minion
minion_conf:
file.managed:
- name: /etc/salt/minion
- source: salt://minions/conf/minion ##minion端需要配置的minion主配置文件
- user: root
- group: root
- mode: 640
- template: jinja
- defaults:
minion_id: {{grains['fqdn_ip4'][0]}} ##这里grains是收集minion端/etc/hosts文件IP和主机名的
- require:
- pkg: minion_install
minion_service:
service.running:
- name: salt-minion
- enable: True
- require:
- file: minion_conf
master: 10.62.83.211
id: {{ minion_id }} #这里的id会被解析成install.sls中的minion_id
可从/etc/yum.repos.d拷贝
cp CentOS-Base.repo /srv/salt/minions/yum.repos.d/
cp epel.repo /srv/salt/minions/yum.repos.d/
[root@mykurol /]# tree /srv
/srv
`-- salt
`-- minions
|-- conf
| `-- minion
|-- install.sls
`-- yum.repos.d
|-- CentOS-Base.repo
`-- epel.repo
4 directories, 4 files