二是不须要代理配置工具的,能够直接基于SSH服务来完成管理功能,如ansible,fabric等。mysql
工具 | 开发语言 | 结构 | 配置文件格式 | 运行任务 |
---|---|---|---|---|
Ansible | Python | 无 | YAML | 支持命令行 |
SaltStack | Python | C/S | YAML | 支持命令行 |
Puppet | Ruby | C/S | Ruby语法格式 | 经过模块实现 |
服务器 | IP地址 | 操做系统 | 组名 |
---|---|---|---|
控制主机 | 192.168.144.112 | centos7.3 x86_64 | 无 |
被控制主机1 | 192.168.144.111 | centos7.3 x86_64 | webserver |
被控制主机2 | 192.168.144.114 | centos7.3 x86_64 | mysql |
yum install epel-releaseweb
yum install ansible -y
yum install tree -ysql
tree /etc/ansibleshell
/etc/ansible/ ├── ansible.cfg //主配置文件 ├── hosts //管控主机文件 └── roles //角色目录
vim /etc/ansible/hostsvim
[webserver] //主机分类组名 192.168.144.111 //主机IP地址或者是域名 [mysql] 192.168.144.114
ssh-keygen -t rsa
ssh-copy-id root@192.168.144.111 //发送公匙给被控服务器
ssh-copy-id root@192.168.144.114centos
ssh-agent bash
ssh-add //输入私钥密码便可安全
ansible 192.168.144.111 -m command -a 'date' //指定ip执行date
ansible webserver -m command -a 'date' //指定分类执行date
ansible mysql -m command -a 'date'
ansible all -m command -a 'date' //全部hosts主机执行date命令
ansible all -a 'ls -l /' 若是不加-m模块,则默认运行command模块bash
ansible-doc -s cron //查看cron模块信息
ansible webserver -m cron -a 'minute="*/1" job="/bin/echo heihei" name="test cron job"'服务器
ansible webserver -m cron -a 'hour="23" job="/bin/echo heihei" name="test cron job"' //天天23点执行,若想每隔23个小时执行须要改为hour="*/23" ansible webserver -m cron -a 'weekday="6" job="/bin/echo heihei" name="test cron job"' ansible-doc -s cron //结合查看详细用法
ansible webserver -a 'crontab -l'
ansible webserver -m cron -a 'name="test cron job" state=absent' //移除计划任务,假如该计划任务没有取名字,name=None便可运维
ansible webserver -m user -a 'name="test1"'
ansible webserver -m user -a 'name="test2" shell=/sbin/nologin' //添加用户指定shell登陆方式
ansible webserver -m command -a 'tail /etc/passwd' //查看用户
ansible webserver -m user -a 'name="test1" state=absent' //删除用户test01
ansible-doc -s group //查看group模块帮助文档
ansible mysql -m group -a 'name=mysql gid=306 system=yes' //建立mysql组,指定gid,设置为系统组
ansible mysql -a 'tail /etc/group'
ansible mysql -m user -a 'name=test01 uid=306 system=yes group=mysql' //使用user模块添加用户,并添加到mysql组
ansible mysql -a 'tail /etc/passwd'
ansible mysql -a 'id test01'
ansible-doc -s copy
ansible mysql -m copy -a 'dest=/opt/123.txt content="heihei" owner=test01 group=test01 mode=600' //新建文件且指定内容
ansible mysql -a 'ls -l /opt'
ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.back owner=root mode=640' //复制文件
ansible-doc -s file
ansible mysql -m file -a 'owner=root group=root mode=755 path=/opt/123.txt' //更改文件的属主属组
ansible mysql -m file -a 'src=/opt/123.txt dest=/opt/123.txt.bk state=link' //建立软链接
ansible mysql -m file -a 'path=/opt/test.txt state=touch' //新建一个空文件,若须要指定内容须要copy模块,content指定内容
ansible all -m ping
ansible-doc -s yum
ansible mysql -m yum -a 'name=httpd'
ansible mysql -m yum -a 'name=httpd state=absent'
ansible mysql -m command -a 'rpm -q httpd'
ansible-doc -s service
ansible mysql -m service -a 'name=httpd enabled=true state=started' //设置httpd开启自启动,且状态为开启
ansible mysql -m command -a 'systemctl status httpd'
ansible-doc -s shell
ansible mysql -m shell -a 'echo abc123 | passwd --stdin test' //为test用户建立面交互式密码
ansible-doc -s script
vi test.sh
#!/bin/bash echo "hello ansible from script"> /opt/script.txt
chmod +x test.sh
ansible mysql -m script -a 'test.sh'
ansible-doc -s setup
ansible mysql -m setup